Android的分割字符串 [英] Android Split string

查看:622
本文介绍了Android的分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 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方法
 

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

这篇关于Android的分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆