如果找不到Java拆分中的字符串,该怎么办?

Arm*_*ada 26 java string split

    String incomingNumbers[ ] = writtenNumber.split("\\-");
Run Code Online (Sandbox Code Playgroud)

该程序接受自然语言编号,如三十二或五.
那么如果输入五个,那我的incomingNumbers数组中有什么东西?

pax*_*blo 34

你得到一个大小为1的数组,保持原始值:

Input       Output
-----       ------
thirty-two  {"thirty", "two"}
five        {"five"}
Run Code Online (Sandbox Code Playgroud)

您可以在以下程序中看到此操作:

class Test {
    static void checkResult (String input) {
        String [] arr = input.split ("\\-");
        System.out.println ("Input   : '" + input + "'");
        System.out.println ("    Size: " + arr.length);
        for (int i = 0; i < arr.length; i++)
            System.out.println ("    Val : '" + arr[i] + "'");
        System.out.println();
    }

    public static void main(String[] args) {
        checkResult ("thirty-two");
        checkResult ("five");
    }
}
Run Code Online (Sandbox Code Playgroud)

哪个输出:

Input   : 'thirty-two'
    Size: 2
    Val : 'thirty'
    Val : 'two'

Input   : 'five'
    Size: 1
    Val : 'five'
Run Code Online (Sandbox Code Playgroud)

  • @ArmandoMoncada似乎你写过你还没试过.下次使用Eclipse和调试器查看内部数组 (3认同)