How to split the line before the first comma?

I have an override method with String that returns a String in the format:

"abc,cde,def,fgh" 

I want to split the contents of a string into two parts:

  1. Line before the first comma and

  2. Line after the first comma

My main method:

 @Override protected void onPostExecute(String addressText) { placeTitle.setText(addressText); } 

Now, how do I split a string into two parts so that I can use them to set text to two different TextView ?

+10
java string delimiter
source share
10 answers

You can use the following code snippet

 String str ="abc,cde,def,fgh"; String kept = str.substring( 0, str.indexOf(",")); String remainder = str.substring(str.indexOf(",")+1, str.length()); 
+27
source share
 String splitted[] =s.split(",",2); // will be matched 1 times. splitted[0] //before the first comma. `abc` splitted[1] //the whole String after the first comma. `cde,def,fgh` 

If you want only cde as a string after the first comma. Then you can use

 String splitted[] =s.split(",",3); // will be matched 2 times 

or without limitation

 String splitted[] =s.split(","); 

Remember to check length to avoid ArrayIndexOutOfBound .

+7
source share

Below you will find the following:

 public String[] split(",", 2) 

This will give 2 string arrays. Split has two versions. What you can try is

 String str = "abc,def,ghi,jkl"; String [] twoStringArray= str.split(",", 2); //the main line System.out.println("String befor comma = "+twoStringArray[0]);//abc System.out.println("String after comma = "+twoStringArray[1]);//def,ghi,jkl 
+3
source share
  String s =" abc,cde,def,fgh"; System.out.println("subString1="+ s.substring(0, s.indexOf(","))); System.out.println("subString2="+ s.substring(s.indexOf(",") + 1, s.length())); 
+2
source share
 // Note the use of limit to prevent it from splitting into more than 2 parts String [] parts = s.split(",", 2); // ...setText(parts[0]); // ...setText(parts[1]); 

See the documentation for more information.

+1
source share

Use split with regex:

 String splitted[] = addressText.split(",",2); System.out.println(splitted[0]); System.out.println(splitted[1]); 
+1
source share

From jse1.4 String - Two split methods are new. A subSequence method has been added, as required by the CharSequence interface, which now implements String. Three additional methods have been added: matches , replaceAll and replaceFirst .

Using Java String.split(String regex, int limit) with Pattern.quote(String s)

The string "boo: and: foo", for example, gives 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" } 
 String str = "abc?def,ghi?jkl,mno,pqr?stu,vwx?yz"; String quotedText = Pattern.quote( "?" ); // ? - \\? we have to escape sequence of some characters, to avoid use Pattern.quote( "?" ); String[] split = str.split(quotedText, 2); // ["abc", "def,ghi?jkl,mno,pqr?stu,vwx?yz"] for (String string : split) { System.out.println( string ); } 

I have the same problem in URL parameters. To solve this problem I need to split based on the first ? . Thus, the remaing String contains parameter values, and they need to be split based on & .

 String paramUrl = "https://www.google.co.in/search?q=encode+url&oq=encode+url"; String subURL = URLEncoder.encode( paramUrl, "UTF-8"); String myMainUrl = "http://example.com/index.html?url=" + subURL +"&name=chrome&version=56"; System.out.println("Main URL : "+ myMainUrl ); String decodeMainURL = URLDecoder.decode(myMainUrl, "UTF-8"); System.out.println("Main URL : "+ decodeMainURL ); String[] split = decodeMainURL.split(Pattern.quote( "?" ), 2); String[] Parameters = split[1].split("&"); for (String param : Parameters) { System.out.println( param ); } 

Run Javascript on the JVM with Rhino / Nashorn "With JavaScripts String.prototype.split :

 var str = "abc?def,ghi?jkl,mno,pqr?stu,vwx?yz"; var parts = str.split(','); console.log( parts ); // (5) ["abc?def", "ghi?jkl", "mno", "pqr?stu", "vwx?yz"] console.log( str.split('?') ); // (5) ["abc", "def,ghi", "jkl,mno,pqr", "stu,vwx", "yz"] var twoparts = str.split(/,(.+)/); console.log( parts ); // (3) ["abc?def", "ghi?jkl,mno,pqr?stu,vwx?yz", ""] console.log( str.split(/\?(.+)/) ); // (3) ["abc", "def,ghi?jkl,mno,pqr?stu,vwx?yz", ""] 
0
source share

: In this case, you can use replaceAll with some regex to get this input so you can use:

  System.out.println("test another :::"+test.replaceAll("(\\.*?),.*", "$1")); 

If the key is only a String , you can use (\\D?),.* ,. (\\D?),.*

 System.out.println("test ::::"+test.replaceAll("(\\D?),.*", "$1")); 
0
source share
 public static int[] **stringToInt**(String inp,int n) { **int a[]=new int[n];** int i=0; for(i=0;i<n;i++) { if(inp.indexOf(",")==-1) { a[i]=Integer.parseInt(inp); break; } else { a[i]=Integer.parseInt(inp.substring(0, inp.indexOf(","))); inp=inp.substring(inp.indexOf(",")+1,inp.length()); } } return a; } 

I created this function. The arguments are the input string (StringInp, here) and the integer value (int n, here) , which is the size of the array that contains the values ​​in the string, separated by commas, you can use another special character to extract values ​​from the string containing this character. This function will return an array of integers of size n.

To use

 String inp1="444,55"; int values[]=stringToInt(inp1,2); 
0
source share

I have this line. Can anyone suggest how I can get the value of a specific key "Location": "Latitude: 40.377, Longitude: -105.147, Name: SB I-25 North Ditch"

0
source share

All Articles