java获取字符串的单词,从Java中的字符串中提取第一个单词的最佳方法是什么?...

Trying to write a short method so that I can parse a string and extract the first word. I have been looking for the best way to do this.

I assume I would use str.split(","), however I would like to grab just the first first word from a string, and save that in one variable, and and put the rest of the tokens in another variable.

Is there a concise way of doing this?

解决方案

The second parameter of the split method is optional, and if specified will split the target string only N times.

For example:

String mystring = "the quick brown fox";

String arr[] = mystring.split(" ", 2);

String firstWord = arr[0]; //the

String theRest = arr[1]; //quick brown fox

Alternatively you could use the substring method of String.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值