Java:获取某个字符之前的子字符串

huangapple go评论51阅读模式
英文:

Java: Getting a substring before a certain character

问题

例如:

patient1234567 GMT+ howtoget

我想要获取在 + 之前的信息,在这个例子中就是 GMT。我处理的字符串有超过1000个字符,我尝试过使用 indexof 但是无法使其工作。

有人能提供帮助吗?

英文:

For example:

patient1234567 GMT+ howtoget

I want to get the information preceding the + and in this case that would be GMT. The strings I'm working with have over 1000 characters and I've tried indexof and could not get it to work.

Can anyone please offer any assistance?

答案1

得分: 0

尝试一下:

String[] data = "patient1234567 GMT+ howtoget".split("\\+")[0].split(" ");
System.out.println(data[data.length - 1]);

输出:

GMT
英文:

Try this :

String[] data = "patient1234567 GMT+ howtoget".split("\\+")[0].split(" ");
System.out.println(data[data.length - 1]);

Output :

GMT

huangapple
  • 本文由 发表于 2020年9月17日 22:41:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定