英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论