-2
public class TestString {
public static void main(String[] args) {
    String str = "AaaaABBBBcc&^%adfsfdCCOOkk99876 _haHA";
    String[] upStr = str.split("[a-z0-9&^% _]");
    System.out.println("Printout uppercase");

    for (String outUp : upStr){
        System.out.print(outUp);
    }
    System.out.println("\n" + upStr.length);

The "length" is wrong, so, where does the value comes from? How could we get real length?

    System.out.println("\n Printout lowercase");
    String[] lowStr = str.split("[A-Z0-9&^% _]");
    for (String outLow : lowStr){
        System.out.print(outLow);
    }
    System.out.println("\n" + lowStr.length);

    System.out.println("\n non-English");
    String[] nonEng = str.split("[A-Za-z]");
    for (String outNonEng : nonEng){
        System.out.print(outNonEng);
    }
    System.out.println("\n" + nonEng.length);

So my question is:

  1. The value of the length is incorrect, but where is is comes from?
  2. How could I get correct length of String[]?
Kaspar Lee
  • 5,446
  • 4
  • 31
  • 54
  • length seems correct – riteshtch Mar 31 '16 at 09:52
  • I will suggest you to read :- http://stackoverflow.com/a/22259885/1996394 – rock321987 Mar 31 '16 at 09:53
  • Printout uppercase AABBBBCCOOHA in fact the current length is 12; Printout lowercase aaaccadfsfdkkha non-English &^%99876 _ – WilliamZhu Mar 31 '16 at 09:53
  • 1
    `length` in this case is the number of items in the array. How come you're talking about "length of new string"? – SamWhan Mar 31 '16 at 09:54
  • Base on the Java API: String[] split(String regex) Splits this string around matches of the given regular expression. After we use the "split", it will return a String[] back. – WilliamZhu Mar 31 '16 at 09:57
  • 1
    Do not split, use `replaceAll("[^A-Z]+", "").length` to check uppercase letter count and `replaceAll("[^a-z]+", "").length` to check lowercase letter count. – Wiktor Stribiżew Mar 31 '16 at 09:59
  • Yes, and is it the *length* of the array you're talking about? (Please add the output of your code to make it easier to help you + use `@username` to alert people when you reply ;) – SamWhan Mar 31 '16 at 10:01

1 Answers1

0

You may want to do it like the following:

public static void main(String[] args) {
        String str = "AaaaABBBBcc&^%adfsfdCCOOkk99876 _haHA";
        String[] upStr = str.replaceAll("[^A-Z]", "").split("(?!^)");
        String[] lowStr = str.replaceAll("[^a-z]", "").split("(?!^)");
        String[] nonEng = str.replaceAll("[A-Za-z]", "").split("(?!^)");
        System.out.println("Printout uppercase");
        printResult(upStr);
        System.out.println("\nPrintout lowercase");
        printResult(lowStr);
        System.out.println("\nPrintout non-English");
        printResult(nonEng);   
    }
public static void printResult(String[] array) {
        for (String outChar : array){
        System.out.print(outChar);
    }
    System.out.println("\n" + array.length);
}

Output:

Printout uppercase
AABBBBCCOOHA
12

Printout lowercase
aaaccadfsfdkkha
15

Printout non-English
&^%99876 _
10
Quinn
  • 4,394
  • 2
  • 21
  • 19