Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用数组或其他技术在java中使用字符串位置_Java_Arrays - Fatal编程技术网

如何使用数组或其他技术在java中使用字符串位置

如何使用数组或其他技术在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

我需要得到两个不同长度的注册号的位置,我重复使用我得到的位置

我需要瞄准的部分是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 / 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”)
    “ABS/MS/”。contains(“MS”)
    都返回true。如果需要检查“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(?:\\)”)

此处将打印什么?:System.out.println((msg.split(“/”[1]);您将得到字符串MSThanksz@Anil,+1的含义是什么?str1.indexOf(“/MS/”)将给出字符串“/MS/”中第一个字母的位置,即:“/”。但是我们只需要“MS”的位置,所以我在“/”in/MS/”的位置添加了1。如果您的问题得到解决,请接受任何合适的答案