Usagi
Usagi

Reputation: 3

How to make character array in string array?

The array example:

String wordList[] = {"pig", "dog", "cat", "fish", "bird"}

I have transform Str to Char , Half Width to Full Width and UpperCase. look like

for(int i = 0 ; i < str.length(); i++){
char cha = str.charAt(i);
cha = Character.toUpperCase(cha);
cha += 65248;
System.out.print(cha);
}

So, I have a question about how to make a new array such as {'p', 'i', 'g'};

Upvotes: 0

Views: 72

Answers (2)

Gorazd Rebolj
Gorazd Rebolj

Reputation: 825

The following will print [PIG, DOG, CAT, FISH, BIRD]

String[] words = new String[]{"pig", "dog", "cat", "fish", "bird"};
char[][] charWords = new char[words.length][];

for (int j = 0; j < words.length; j++) {
    String str = words[j];
    char[] chars = new char[str.length()];

    for (int i = 0; i < str.length(); i++) {
        char cha = str.charAt(i);
        cha = Character.toUpperCase(cha);
        cha += 65248;
        chars[i] = cha;
    }

    charWords[j] = chars;
}

System.out.println(Arrays.stream(charWords).map(String::new).collect(Collectors.toList()));

Upvotes: 0

El0din
El0din

Reputation: 3370

Maybe this can be usefull

for (String str : wordList){
   char[] charArray = str.toCharArray();
   System.out.println(charArray.toString());
}

Upvotes: 1

Related Questions