Below function
convert a string to Character array.
private
static Character[] toCharArray(String str) {
if(str == null) return null;
int length = str.length();
Character[] charArray = new
Character[length];
for(int i = 0; i < str.length(); i++) {
charArray[i] = str.charAt(i);
}
return charArray;
}
App.java
package com.sample.app; public class App { private static Character[] toCharArray(String str) { if(str == null) return null; int length = str.length(); Character[] charArray = new Character[length]; for(int i = 0; i < str.length(); i++) { charArray[i] = str.charAt(i); } return charArray; } private static void printCharArray(Character[] charArray) { System.out.print("["); for(Character ch : charArray) { System.out.print(ch + ","); } System.out.println("]"); } public static void main(String args[]) { String str = "Hello World"; Character[] charArray = toCharArray(str); printCharArray(charArray); } }
Output
[H,e,l,l,o,
,W,o,r,l,d,]
You may
like
No comments:
Post a Comment