split string only on first instance - java

前端 未结 6 621
情话喂你
情话喂你 2020-12-07 12:52

I want to split a string by \'=\' charecter. But I want it to split on first instance only. How can I do that ? Here is a JavaScript example for \'_\' char but it doesn\'t w

相关标签:
6条回答
  • 2020-12-07 13:13

    As many other answers suggest the limit approach, This can be another way

    You can use the indexOf method on String which will returns the first Occurance of the given character, Using that index you can get the desired output

    String target = "apple=fruit table price=5" ;
    int x= target.indexOf("=");
    System.out.println(target.substring(x+1));
    
    0 讨论(0)
  • 2020-12-07 13:23
    String[] func(String apple){
    String[] tmp = new String[2];
    for(int i=0;i<apple.length;i++){
       if(apple.charAt(i)=='='){
          tmp[0]=apple.substring(0,i);
          tmp[1]=apple.substring(i+1,apple.length);
          break;
       }
    }
    return tmp;
    }
    //returns string_ARRAY_!
    

    i like writing own methods :)

    0 讨论(0)
  • 2020-12-07 13:24
    string.split("=", 2);
    

    As String.split(java.lang.String regex, int limit) explains:

    The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string. If the expression does not match any part of the input then the resulting array has just one element, namely this string.

    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.

    The string boo:and:foo, for example, yields the following results with these parameters:

    Regex Limit    Result
    :     2        { "boo", "and:foo" }
    :     5        { "boo", "and", "foo" }
    :    -2        { "boo", "and", "foo" }
    o     5        { "b", "", ":and:f", "", "" }
    o    -2        { "b", "", ":and:f", "", "" }
    o     0        { "b", "", ":and:f" }
    
    0 讨论(0)
  • 2020-12-07 13:29
    String string = "This is test string on web";
    String splitData[] = string.split("\\s", 2);
    
    Result ::
    splitData[0] =>  This
    splitData[1] =>  is test string  
    
    
    String string = "This is test string on web";
    String splitData[] = string.split("\\s", 3);
    
    Result ::
    splitData[0] =>  This
    splitData[1] =>  is
    splitData[1] =>  test string on web
    

    By default split method create n number's of arrays on the basis of given regex. But if you want to restrict number of arrays to create after a split than pass second argument as an integer argument.

    0 讨论(0)
  • 2020-12-07 13:32

    Yes you can, just pass the integer param to the split method

    String stSplit = "apple=fruit table price=5"
    
    stSplit.split("=", 2);
    

    Here is a java doc reference : String#split(java.lang.String, int)

    0 讨论(0)
  • 2020-12-07 13:33

    This works:

    public class Split
    {
        public static void main(String...args)
        {
            String a = "%abcdef&Ghijk%xyz";
            String b[] = a.split("%", 2);
            
            System.out.println("Value = "+b[1]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题