How can split a string which contains only delimiter?

后端 未结 5 716
难免孤独
难免孤独 2020-12-10 12:11

I am using the following code:

String sample = \"::\";
String[] splitTime = sample.split(\":\");
// extra detail omitted
System.out.println(\"Value 1 :\"+spl         


        
相关标签:
5条回答
  • 2020-12-10 12:22

    Alnitak is correct that trailing empty strings will be discarded by default.

    If you want to have trailing empty strings, you should use split(String, int) and pass a negative number as the limit parameter.

    The limit parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.

    Note that split(aString) is a synonym for split(aString, 0):

    This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

    Also, you should use a loop to get the values from the array; this avoids a possible ArrayIndexOutOfBoundsException.

    So your corrected code should be (assuming you want the trailing empty strings):

    String sample = "::";
    String[] splitTime = sample.split(":", -1);
    for (int i = 0; i < splitTime.length; i++) {
        System.out.println("Value " + i + " : \"" + splitTime[i] + "\"");
    }
    

    Output:

    Value 0 : ""
    Value 1 : ""
    Value 2 : ""
    
    0 讨论(0)
  • 2020-12-10 12:23

    From the J2SE API manual:

    Trailing empty strings are therefore not included in the resulting array.

    So, if you pass in "::" you'll get an empty array because all of the delimiters are trailing.

    If you want to make sure that you get no more than three entries you should use:

    String[] splitTime = sample.split(":", 3);
    

    With an input of "::" that would indeed give you three empty strings in the output array.

    However if the input only happens to have one ":" in it then you'll still only get two elements in your array.

    0 讨论(0)
  • 2020-12-10 12:23

    you should check the length of the splitTime array.

    0 讨论(0)
  • 2020-12-10 12:46

    Like this perhaps?

    int ndx = 0;
    StringTokenizer t = new StringTokenizer(": : ::::",":");
    while (t.hasMoreElements())
    {
        System.out.println(String.format("Value %d : %s", ++ndx,t.nextElement()));
    }
    
    0 讨论(0)
  • 2020-12-10 12:47

    Use the function StringTokenizer in which u pass the string and the second argument as delimiter

    use splittime.length function to find the length

    0 讨论(0)
提交回复
热议问题