Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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_String_Match - Fatal编程技术网

Java:获取字符串中匹配项位置的方法?

Java:获取字符串中匹配项位置的方法?,java,string,match,Java,String,Match,使用string.indexOf获取起始索引 String match = "hello"; String text = "0123456789hello0123456789"; int position = getPosition(match, text); // should be 10, is there such a method? 请参见执行此操作的方法系列包括: 返回指定子字符串第一次(或最后一次)出现的字符串内的索引[从指定索引开始向前(或向后)搜索] 您

使用string.indexOf获取起始索引

String match = "hello";
String text = "0123456789hello0123456789";

int position = getPosition(match, text); // should be 10, is there such a method?

请参见

执行此操作的方法系列包括:

返回指定子字符串第一次(或最后一次)出现的字符串内的索引[从指定索引开始向前(或向后)搜索]



您只需指定inside while loop,即可获得文件中的所有匹配项,酷:

String text = "0123hello9012hello8901hello7890";
String word = "hello";

System.out.println(text.indexOf(word)); // prints "4"
System.out.println(text.lastIndexOf(word)); // prints "22"

// find all occurrences forward
for (int i = -1; (i = text.indexOf(word, i + 1)) != -1; i++) {
    System.out.println(i);
} // prints "4", "13", "22"

// find all occurrences backward
for (int i = text.length(); (i = text.lastIndexOf(word, i - 1)) != -1; i++) {
    System.out.println(i);
} // prints "22", "13", "4"
查找单个索引 正如其他人所说,使用
text.indexOf(match)
查找单个匹配项

int match_position=text.indexOf(match);
查找多个索引 由于代码的可维护性和我自己理解上的困难,我想找到另一种方法来获取文本字符串中匹配项的所有索引。以下代码(从中修改)执行此操作:


如果要扫描搜索字符串的“n”个匹配项,我建议使用。
他们有一个陡峭的学习曲线,但当涉及到复杂的搜索时,他们会节省你几个小时。

我有一些大代码,但工作很好

String text = "0123hello9012hello8901hello7890";
String match = "hello";

int index = text.indexOf(match);
int matchLength = match.length();
while (index >= 0) {  // indexOf returns -1 if no match found
    System.out.println(index);
    index = text.indexOf(match, index + matchLength);
}
类strDemo
{ 
公共静态void main(字符串参数[])
{
字符串s1=新字符串(“阿拉伯豆海的幽灵”);
字符串s2=新字符串(“新字符串”);
字符串s6=新字符串(“ehT”);
stringbuffers3;
StringBuffer s4=新的StringBuffer(s1);
StringBuffer s5=新的StringBuffer(s2);
字符c1[]=新字符[30];
字符c2[]=新字符[5];
字符c3[]=新字符[5];
s1.getChars(0,28,c1,0);
s2.getChars(0,3,c2,0);
s6.getChars(0,3,c3,0);s3=s4.反向();
int-pf=0,pl=0;
字符c5[]=新字符[30];
s3.getChars(0,28,c5,0);
for(int i=0;i
//在字符串中的任意位置查找特定单词,并打印其索引和出现时间
印度支那类
{
公共静态void main(字符串[]args)
{
String s=“这是海得拉巴市,这是”;
System.out.println(“给定字符串为”);
System.out.println(“------------”+s);
char ch[]=s.toCharArray();
System.out.println(“----单词位于”);
int j=0,noc=0;

对于(inti=0;i,它使用正则表达式工作

//finding a particular word any where inthe string and printing its index and occurence  
class IndOc
{
    public static void main(String[] args) 
    {
        String s="this is hyderabad city and this is";
        System.out.println("the given string is ");
        System.out.println("----------"+s);
        char ch[]=s.toCharArray();
        System.out.println(" ----word is found at ");
        int j=0,noc=0;
        for(int i=0;i<ch.length;i++)
        {
            j=i;

            if(ch[i]=='i' && ch[j+1]=='s')
            {
                System.out.println(" index "+i);
            noc++;  
            }

        }
        System.out.println("----- no of occurences are "+noc);

    }
}
输出:

在索引2-5中找到“爱”

一般规则:

  • 正则表达式从左到右搜索,一旦使用了匹配字符,就不能重复使用

对于多次出现和字符串中的字符??是或否

String text = "I love you so much";
String wordToFind = "love";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(text);

while (match.find()) {
     System.out.println("Found love at index "+ match.start() +" - "+ (match.end()-1));
}
String match=“hello”;
String text=“0123456789hello0123456789hello”;
int j=0;
字符串indxOfmatch=“”;
for(int i=-1;i=j&&j>-1){
indxOfmatch++=text.indexOf(“hello”,i)+”;
}
}
System.out.println(indxOfmatch);

lolz,刚刚在while循环中实现了一个赋值,然后在for循环中发布了一个赋值+1@polygenelubricants-您的“查找所有事件”示例很聪明。但是如果代码审查是这样的话,你会得到一个关于代码可维护性的讲座。你会如何编写它?我诚恳地问,因为我以前没有专业的代码审查经验。在“查找所有事件”中,我们可以编写I+=word.length()。应该快一点。如果匹配一个字符,第一个循环将找不到所有位置。第二个循环语句不需要+1,因为第三个语句不需要计数i++try for String text=“0011100”;匹配单词字符“1”它将打印2,4而不是2,3,4你用
+1
抵消
i
的方式工作,而是以一种相当迂回的方式。如你所示,它在
i==1
处报告第一个
hello
。如果你总是使用基于0的索引,它会更加一致……会窃取你的东西:P谢谢。你能解释一下为什么这样做以及g是什么吗在内部循环的保护中继续?一个解释可能对新手读者有用。int indexOf(String str,int fromIndex):返回指定子字符串第一次出现的字符串内的索引,从指定的索引开始。如果没有出现,则返回-1。此处while的内部循环将能够获取令牌的所有occource(此处由名为“key”的变量指定)。请解释您的操作did@FabiogetPosition(匹配,文本){int match_position=text.indexOf(match);return match_position;}在代码中添加一些解释/注释将使人们更容易理解您的代码,尤其是它的长代码:)虽然这段代码可以回答这个问题,但提供有关如何和/或为什么解决问题的附加上下文将提高答案的长期价值。建议:包括一个从正则表达式获取位置的示例。只需“尝试使用正则表达式”这是一个相当基本的评论,并没有回答OP的问题。这非常棒,但对于这句话,我得到的输出是“我有一个男朋友”:-)希望它能解决问题,但请用它添加对代码的解释,以便用户能够完全理解他/她真正想要的。
String text = "0123456789hello0123456789";
String match = "hello";
int position = text.indexOf(match); // position = 10
String text = "0123hello9012hello8901hello7890";
String match = "hello";

int index = text.indexOf(match);
int matchLength = match.length();
while (index >= 0) {  // indexOf returns -1 if no match found
    System.out.println(index);
    index = text.indexOf(match, index + matchLength);
}
   class strDemo
   { 
       public static void main(String args[])
       {
       String s1=new String("The Ghost of The Arabean Sea");
           String s2=new String ("The");
           String s6=new String ("ehT");
           StringBuffer s3;
           StringBuffer s4=new StringBuffer(s1);
           StringBuffer s5=new StringBuffer(s2);
           char c1[]=new char[30];
           char c2[]=new char[5];
           char c3[]=new char[5];
           s1.getChars(0,28,c1,0);
           s2.getChars(0,3,c2,0);
           s6.getChars(0,3,c3,0); s3=s4.reverse();      
           int pf=0,pl=0;
           char c5[]=new char[30];
           s3.getChars(0,28,c5,0);
           for(int i=0;i<(s1.length()-s2.length());i++)
           {
               int j=0;
               if(pf<=1)
               {
                  while (c1[i+j]==c2[j] && j<=s2.length())
                  {           
                    j++;
                    System.out.println(s2.length()+" "+j);
                    if(j>=s2.length())
                    {
                       System.out.println("first match of(The) :->"+i);

                     }
                     pf=pf+1;         
                  }   
             }                
       }       
         for(int i=0;i<(s3.length()-s6.length()+1);i++)
        {
            int j=0;
            if(pl<=1)
            {
             while (c5[i+j]==c3[j] && j<=s6.length())
             {
                 j++;
                 System.out.println(s6.length()+" "+j);
                 if(j>=s6.length())
                 {
                         System.out.println((s3.length()-i-3));
                         pl=pl+1;

                 }   
                }                 
              }  
           }  
         }
       }
//finding a particular word any where inthe string and printing its index and occurence  
class IndOc
{
    public static void main(String[] args) 
    {
        String s="this is hyderabad city and this is";
        System.out.println("the given string is ");
        System.out.println("----------"+s);
        char ch[]=s.toCharArray();
        System.out.println(" ----word is found at ");
        int j=0,noc=0;
        for(int i=0;i<ch.length;i++)
        {
            j=i;

            if(ch[i]=='i' && ch[j+1]=='s')
            {
                System.out.println(" index "+i);
            noc++;  
            }

        }
        System.out.println("----- no of occurences are "+noc);

    }
}
String text = "I love you so much";
String wordToFind = "love";
Pattern word = Pattern.compile(wordToFind);
Matcher match = word.matcher(text);

while (match.find()) {
     System.out.println("Found love at index "+ match.start() +" - "+ (match.end()-1));
}
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SubStringtest {

    public static void main(String[] args)throws Exception {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
     System.out.println("enter the string");
    String str=br.readLine();
    System.out.println("enter the character which you want");
    CharSequence ch=br.readLine();   
    boolean bool=str.contains(ch);
    System.out.println("the character found is " +bool);
    int position=str.indexOf(ch.toString());

    while(position>=0){
        System.out.println("the index no of character is " +position); 
        position=str.indexOf(ch.toString(),position+1);
    }


    }

}
    String match = "hello";
    String text = "0123456789hello0123456789hello";

    int j = 0;
    String indxOfmatch = "";

    for (int i = -1; i < text.length()+1; i++) {
        j =  text.indexOf("hello", i);
        if (i>=j && j > -1) {
            indxOfmatch += text.indexOf("hello", i)+" ";
        }
    }
    System.out.println(indxOfmatch);
public int NumberWordsInText(String FullText_, String WordToFind_, int[] positions_)
   {
    int iii1=0;
    int iii2=0;
    int iii3=0;
    while((iii1=(FullText_.indexOf(WordToFind_,iii1)+1))>0){iii2=iii2+1;}
    // iii2 is the number of the occurences
    if(iii2>0) {
        positions_ = new int[iii2];
        while ((iii1 = (FullText_.indexOf(WordToFind_, iii1) + 1)) > 0) {
            positions_[iii3] = iii1-1;
            iii3 = iii3 + 1;
            System.out.println("position=" + positions_[iii3 - 1]);
        }
    }
    return iii2;
}