Java Android 拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3732790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:03:51  来源:igfitidea点击:

Android Split string

javaandroidstring

提问by zaid

I have a string called CurrentStringand is in the form of something like this "Fruit: they taste good".
I would like to split up the CurrentStringusing the :as the delimiter.
So that way the word "Fruit"will be split into its own string and "they taste good"will be another string.
And then i would simply like to use SetText()of 2 different TextViewsto display that string.

我有一个名为的字符串CurrentString,它的形式是这样的 "Fruit: they taste good"
我想拆分CurrentString使用:作为分隔符。
这样这个词"Fruit"就会被拆分成它自己的字符串,"they taste good"并成为另一个字符串。
然后我只想使用SetText()2 个不同的TextViews来显示该字符串。

What would be the best way to approach this?

解决这个问题的最佳方法是什么?

采纳答案by Cristian

String currentString = "Fruit: they taste good";
String[] separated = currentString.split(":");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

You may want to remove the space to the second String:

您可能想要删除第二个字符串的空间:

separated[1] = separated[1].trim();

If you want to split the string with a special character like dot(.) you should use escape character \ before the dot

如果你想用像 dot(.) 这样的特殊字符分割字符串,你应该在点之前使用转义字符 \

Example:

例子:

String currentString = "Fruit: they taste good.very nice actually";
String[] separated = currentString.split("\.");
separated[0]; // this will contain "Fruit: they taste good"
separated[1]; // this will contain "very nice actually"

There are other ways to do it. For instance, you can use the StringTokenizerclass (from java.util):

还有其他方法可以做到。例如,您可以使用StringTokenizer类 (from java.util):

StringTokenizer tokens = new StringTokenizer(currentString, ":");
String first = tokens.nextToken();// this will contain "Fruit"
String second = tokens.nextToken();// this will contain " they taste good"
// in the case above I assumed the string has always that syntax (foo: bar)
// but you may want to check if there are tokens or not using the hasMoreTokens method

回答by Silas Greenback

.split method will work, but it uses regular expressions. In this example it would be (to steal from Cristian):

.split 方法可以工作,但它使用正则表达式。在这个例子中,它是(从 Cristian 那里窃取的):

String[] separated = CurrentString.split("\:");
separated[0]; // this will contain "Fruit"
separated[1]; // this will contain " they taste good"

Also, this came from: Android split not working correctly

另外,这来自: Android split not working正常

回答by mahasam

android split string by comma

android用逗号分割字符串

String data = "1,Diego Maradona,Footballer,Argentina";
String[] items = data.split(",");
for (String item : items)
{
    System.out.println("item = " + item);
}

回答by gardarh

You might also want to consider the Android specific TextUtils.split()method.

您可能还想考虑 Android 特定的TextUtils.split()方法。

The difference between TextUtils.split() and String.split() is documented with TextUtils.split():

TextUtils.split() 和 String.split() 之间的区别用 TextUtils.split() 记录:

String.split() returns [''] when the string to be split is empty. This returns []. This does not remove any empty strings from the result.

当要拆分的字符串为空时,String.split() 返回 ['']。这将返回 []。这不会从结果中删除任何空字符串。

I find this a more natural behavior. In essence TextUtils.split() is just a thin wrapper for String.split(), dealing specifically with the empty-string case. The code for the methodis actually quite simple.

我发现这是一种更自然的行为。本质上 TextUtils.split() 只是 String.split() 的一个瘦包装器,专门处理空字符串的情况。该方法的代码实际上非常简单。

回答by Faakhir

     String s = "having Community Portal|Help Desk|Local Embassy|Reference Desk|Site News";
     StringTokenizer st = new StringTokenizer(s, "|");
        String community = st.nextToken();
        String helpDesk = st.nextToken(); 
        String localEmbassy = st.nextToken();
        String referenceDesk = st.nextToken();
        String siteNews = st.nextToken();

回答by Faakhir

String s = "String="

字符串 s = "字符串="

String[] str = s.split("="); //now str[0] is "hello" and str[1] is "goodmorning,2,1"

String[] str = s.split("="); //现在str[0]是“hello”,str[1]是“goodmorning,2,1”

add this string

添加这个字符串