android 分割字符 指定长度_Android的分割字符串

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值