我有一个带有String的覆盖方法,返回格式为String:
"abc,cde,def,fgh"
我想把字符串内容分成两部分:
- 第一个逗号之前的字符串
- 第一个逗号之后的字符串
@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
。
splitted[1]
将返回cde
,不是吗?@Arjit - Saifchar
зҡ„split()
ж–№жі•зҡ„йҮҚиҪҪзүҲжң¬гҖӮиҜ·дҪҝз”Ёs.split(",")
д»ЈжӣҝгҖӮ - Razibpublic String[] split(",", 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
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()));
// 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]);
从jse1.4
String
- 新增了两个split
方法。由于现在实现了CharSequence接口,因此添加了subSequence方法。另外还添加了三个方法:matches
、replaceAll
和replaceFirst
。
使用Java String.split(String regex, int limit)
和 Pattern.quote(String s)
。
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" }
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<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;
}
String inp1="444,55";
int values[]=stringToInt(inp1,2);
在这种情况下,您可以使用replaceAll和一些正则表达式来获取此输入,以便您可以使用:
System.out.println("test another :::"+test.replaceAll("(\\.*?),.*", "$1"));
(\\D?),.*
。System.out.println("test ::::"+test.replaceAll("(\\D?),.*", "$1"));