我有 CurrentString
称为一个字符串,是在像这样的形式
水果:吃起来味道不错。
我想分手了 CurrentString
使用:
作为分隔符这样一来的话水果
将被分割为自己的字符串和吃起来味道不错
将是另一个字符串。照片然后,我只是想用的setText()
2个不同 TextViews
来显示该字符串。
什么是接近这一目标的最佳方式是什么?
解决方案 字符串CurrentString =水果:吃起来味道不错;
的String []分隔= CurrentString.split(:);
分离[0]; //这将包含水果
分离[1]; //这将包含它们的味道不错
您可能需要删除的空间,第二个字符串:
分离[1] =分离[1] .trim();
有其他的方法来做到这一点。例如,你可以使用的StringTokenizer
类(从 java.util中的
):
StringTokenizer的令牌=新的StringTokenizer(CurrentString:);
串首先= tokens.nextToken(); //这将包含水果
字符串秒= tokens.nextToken(); //这将包含它们的味道不错
//上面我所承担的字符串始终是语法的情况下(FOO:巴)
//但你可能要检查是否有标记或不使用hasMoreTokens方法
![如何在Android开发中分割Array数组中的字符串](https://p.xsw88.cn/allimgs/daicuo/20230911/4188.png)
I have a string called CurrentString
and is in the form of something like this
"Fruit: they taste good"
. I would like to split up the CurrentString
using 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 TextViews
to display that string.
What would be the best way to approach this?
解决方案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();
There are other ways to do it. For instance, you can use the StringTokenizer
class (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
相关推荐
最新文章