如何在第一个逗号之前拆分字符串?

我有一个使用String的重写方法,它返回String格式为:

"abc,cde,def,fgh" 

我想将字符串内容分成两部分:

  1. 第一个逗号和之前的字符串

  2. 第一个逗号后的字符串

我的首要方法是:

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

现在我如何将字符串分成两部分,以便我可以使用它们在两个不同的TextView设置文本?

您可以使用以下代码段 –

 String str ="abc,cde,def,fgh"; String kept = str.substring( 0, str.indexOf(",")); String remainder = str.substring(str.indexOf(",")+1, str.length); 
 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` 

如果你想在第一个逗号之后只将cde作为字符串。 然后你可以使用

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

或没有限制

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

不要忘记检查length以避免ArrayIndexOutOfBound

  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())); 

以下是您要搜索的内容:

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

这将给出2个字符串数组。 斯普利特有两个版本。 你可以尝试的是

 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 
 // 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]); 

有关更多信息,请参阅此文档 。

使用正则表达式拆分:

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

:在这种情况下,您可以使用replaceAll和一些正则表达式来获取此输入,以便您可以使用:

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

如果键只是一个String,你可以使用(\\D?),.*

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

来自jse1.4 String两种split方法是新的。 根据String现在实现的CharSequence接口的要求,添加了subSequence方法。 添加了三个额外的方法: matchesreplaceAllreplaceFirst

使用带有Pattern.quote(String s) Java String.split(String regex, int limit) Pattern.quote(String s)

例如,字符串“boo:and:foo”会使用以下参数生成以下结果:

  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 ); } 

我在URL参数中面临同样的问题,为了解决它我需要先拆分? 因此,转换字符串包含参数值,需要根据&拆分它们。

 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 ); } 

使用Rhino / Nashorn在JVM上运行Javascript «使用JavaScript的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", ""] 
 public static int[] **stringToInt**(String inp,int n) { **int a[]=new int[n];** int i=0; for(i=0;i 

我创建了这个function。 参数是输入字符串 (String inp,here)整数值(int n,here) ,它是一个数组的大小,其中包含以逗号分隔的字符串中的值。 您可以使用其他特殊字符从包含该字符的字符串中提取值。 此函数将返回大小为n的整数数组。

要使用

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