如何使用数组或其他技术在java中使用字符串位置
我需要得到两个不同长度的注册号的位置,我重复使用我得到的位置 我需要瞄准的部分是MS字符所在的位置 我需要告诉系统如何知道如何使用数组或其他技术在java中使用字符串位置,java,arrays,Java,Arrays,我需要得到两个不同长度的注册号的位置,我重复使用我得到的位置 我需要瞄准的部分是MS字符所在的位置 我需要告诉系统如何知道regNo是否包含MS,以及它是否有作用 regNo : BCS/MS/13/09/0001 regNo : BCOM/MS/09/0149 if ( the position 5 and position 6 of regNo equals to S ){ .. do s.thing } or if (the part after / the
regNo
是否包含MS
,以及它是否有作用
regNo : BCS/MS/13/09/0001
regNo : BCOM/MS/09/0149
if ( the position 5 and position 6 of regNo equals to S ){
.. do s.thing
}
or
if (the part after / there is MS ){
... do something
}
使用正则表达式
final String msg = "BCS/MS/13/09/0001";
System.out.println((msg.split("/")[1]));
我用过:
String msg = "BCS/MS/13/09/0001";
if (ms.contains("MS"))
{
Do . sothing
}
它成功了
String str1 = "BCS/MS/13/09/0001";
int pos1= str1.indexOf("MS");
String str2 = "BCOM/MS/09/0149";
int pos2= str2.indexOf("MS");
现在,您可以在需要时使用值pos1和pos2
对于更具体的搜索,您可以执行以下操作:
String str1 = "BCS/MS/13/09/0001";
int pos1= str1.indexOf("/MS/")+1;
String str2 = "BCOM/MS/09/0149";
int pos2= str2.indexOf("/MS/")+1;
总而言之,有几个选项:
- -将显示字符串是否包含指定的字符值序列。重要的是要记住,
忽略位置。包含
和“MS”.contains(“MS”)
都返回true。如果需要检查“MS”是否位于字符串中的某个位置,则“ABS/MS/”。contains(“MS”)
可能不是最佳选项contains
- -返回指定子字符串第一次出现时该字符串内的索引李>
- -指示此字符串是否与给定的正则表达式匹配。它对于验证字符串的格式非常有用,尤其是当子字符串的位置可能会变化时。在您的情况下,“MS”可能位于不同的位置。所以我们可以使用类似的东西:
或更具体的System.out.println(“BCS/MS/13/09/0001.matches”([A-Z]+\\/MS.*)
System.out.println(“BCOM/MS/09/0149.matches”([A-Z]{3,4}\\\\/MS(?:\\)”)代码>