搜索通配符(';';';';';),对其进行计数并获取java中的位置

搜索通配符(';';';';';),对其进行计数并获取java中的位置,java,android,string,wildcard,Java,Android,String,Wildcard,我想搜索字符串中的通配符(“”),对它们进行计数并获得它们在java中的位置。我的绳子如下 彼得,约翰 我应该使用哪个函数?谢谢。一个解决方案是使用String.indexOf()。您可以这样做: String s = "Peter <5554>, John <5556>"; List<Integer> posGt = new ArrayList<Integer>(); int i = 0; while((i = s.indexOf('>', i))

我想搜索字符串中的通配符(“”),对它们进行计数并获得它们在java中的位置。我的绳子如下

彼得,约翰


我应该使用哪个函数?谢谢。

一个解决方案是使用String.indexOf()。您可以这样做:


String s = "Peter <5554>, John <5556>";
List<Integer> posGt = new ArrayList<Integer>();
int i = 0;
while((i = s.indexOf('>', i)) != -1) {
   posGt.add(i++);
}
...
//the same for <

String s=“彼得,约翰”;
List posGt=new ArrayList();
int i=0;
而((i=s.indexOf('>',i))!=-1){
posGt.add(i++);
}
...
//同样的<

一种解决方案是使用String.indexOf()。您可以这样做:


String s = "Peter <5554>, John <5556>";
List<Integer> posGt = new ArrayList<Integer>();
int i = 0;
while((i = s.indexOf('>', i)) != -1) {
   posGt.add(i++);
}
...
//the same for <

String s=“彼得,约翰”;
List posGt=new ArrayList();
int i=0;
而((i=s.indexOf('>',i))!=-1){
posGt.add(i++);
}
...
//同样的<

您可以使用重复的
索引和
子字符串来实现它:

String s = "Peter <5554>, John <5556>,"
int count = 0;
ArrayList<Integer> positions = new ArrayList<Integer>();
int cut = 0;
while(true) {
  // search for <
  int index = s.indexOf('<');
  if (index < 0)
    break;
  // search for >
  int index2 = s.indexOf('>');
  if (index2 < 0)
    break; // or throw exception

  // update count and positions
  count++;
  positions.add(index+cut);

  s = s.substring(index2+1);
  cut += index2+1; // used to compute the initial position since we're cutting the string
}
String s=“彼得,约翰,”
整数计数=0;
ArrayList位置=新的ArrayList();
int-cut=0;
while(true){
//寻找<
int index=s.indexOf(“”);
if(index2<0)
break;//或抛出异常
//更新计数和位置
计数++;
位置。添加(索引+剪切);
s=s.子串(index2+1);
cut+=index2+1;//用于计算初始位置,因为我们正在剪切字符串
}

您可以使用重复的
索引和
子字符串来实现它:

String s = "Peter <5554>, John <5556>,"
int count = 0;
ArrayList<Integer> positions = new ArrayList<Integer>();
int cut = 0;
while(true) {
  // search for <
  int index = s.indexOf('<');
  if (index < 0)
    break;
  // search for >
  int index2 = s.indexOf('>');
  if (index2 < 0)
    break; // or throw exception

  // update count and positions
  count++;
  positions.add(index+cut);

  s = s.substring(index2+1);
  cut += index2+1; // used to compute the initial position since we're cutting the string
}
String s=“彼得,约翰,”
整数计数=0;
ArrayList位置=新的ArrayList();
int-cut=0;
while(true){
//寻找<
int index=s.indexOf(“”);
if(index2<0)
break;//或抛出异常
//更新计数和位置
计数++;
位置。添加(索引+剪切);
s=s.子串(index2+1);
cut+=index2+1;//用于计算初始位置,因为我们正在剪切字符串
}
您应该使用和:

Pattern=Pattern.compile(“]*>”);
Matcher=pattern.Matcher(“彼得,约翰”);
while(matcher.find()){
System.out.println(“index=“+matcher.start()+”-“+matcher.group());
}
输出:

index=6 - <5554>
index=19 - <5556>
index=6-
指数=19-
您应该使用和:

Pattern=Pattern.compile(“]*>”);
Matcher=pattern.Matcher(“彼得,约翰”);
while(matcher.find()){
System.out.println(“index=“+matcher.start()+”-“+matcher.group());
}
输出:

index=6 - <5554>
index=19 - <5556>
index=6-
指数=19-

使用
fromIndex
重复
indexOf
看起来是个不错的解决方案。另一种选择是在字符串上迭代并使用
charAt
(如果java有健全的字符串索引,这可能是显而易见的解决方案):

String s=“彼得,约翰,”;
对于(int i=0;i
使用
fromIndex
重复
indexOf
看起来是个不错的解决方案。另一种选择是在字符串上迭代并使用
charAt
(如果java有健全的字符串索引,这可能是显而易见的解决方案):

String s=“彼得,约翰,”;
对于(int i=0;i
很好的解决方案,虽然OP不是要求这种模式的职位,而是要求
角色的职位(但也许你解决了他真正想看的问题;))谢谢大家。我使用模式=D真软,我可以知道更多如何制作正则表达式吗?请参阅。在这种情况下,
<代码>*
表示零个或多个字符,
匹配
。如果您的内容由一个或多个数字组成,则可以使用
。在java中,您必须避开反斜杠:
。这是一个很好的解决方案,尽管OP并不是要求这个模式的职位,而是要求
角色的位置(但也许您解决了他真正看到的;))谢谢大家。我使用模式=D真软,我可以知道更多如何制作正则表达式吗?请参阅。在这种情况下,
<代码>*
表示零个或多个字符,
匹配
。如果您的内容由一个或多个数字组成,则可以使用
。在java中,必须转义反斜杠:
“”