简体   繁体   English

如何通过搜索某些字符从另一个字符串生成subString

[英]How to make a subString from another string by searching for certain characters

String x = "ID:12 Patient Name:...";    
String z = = x.substring(3, x.indexOf(' P'));

I want to get the ID number 我想获取ID号

You're almost there. 你快到了。 Just use something like this:- 只需使用以下内容:

String z = x.substring(x.indexOf("ID:")+3, x.indexOf("Patient")-1);

+3 - Because you don't need the ID: +3因为您不需要ID:

-1 - Because you don't need the space before the Patient -1因为您不需要患者前的空格

This is taking into account that there is a SINGLE space before Patient , if not, as @BuhakeSindi suggested use the trim() method like this:- 这是考虑到有一个空格前Patient ,如果没有,则@BuhakeSindi建议使用trim()方法是这样的: -

String z = x.substring(x.indexOf("ID:")+3, x.indexOf("Patient"));
z = z.trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM