Looking for a String array for a substring?

I am trying to write a small method in java, but I cannot figure it out. What I want to do is enter a string, and then the value of the int variable is set to the index of this in the array, i.e. If I have an array consisting of

[0] 'hi guys'
[1] 'this'
[2] 'is'
[3] 'sparta'

The value of my integer is 0, and I want to find the first occurrence of "ta", which will be [3], so I want the function to set my integer to 3.

What I have at the moment is completely off the wall and not so, is there an easy way to do this? I already have a function called get () that returns the value of the current line (i.e. get (0) in this case will return "hello guys"). Can anyone help me out?

Many thanks:)

 public void find(String line ) {
   boolean found = false;
   int i = cursor + 1;
   while ( found = false && i!=cursor) {
   if ((doc.get(cursor).indexOf( line ) > 0)){
  cursor = i;
  found = true;
   }else {
    cursor++;
    cursor%=doc.size();
    i++;

   }
 }
 }
+3
source share
5

, - :

public int find(String line, int startPosition) {
    if (doc[startPosition].contains(line) {
        return startPosition;
    }
    for (int i = 0; i < Math.max(doc.size() - startPosition, startPosition); i++) {
        if (startPosition - i > 0 && doc[startPosition - i].contains(line)) {
            return startPosition - i;
        }
        if (startPosition + i < doc.size() && doc[startPosition + i].contains(line)) {
            return startPosition + i;
        }

    }
    return -1;
}

, , .

+1

, , , , .

public void find(String line ) {
   boolean found = false;
   int i = 0;;
   while (i < doc.size()) {
     if ((doc.get(i).indexOf( line ) > 0)){
       cursor = i;
       found = true;
       break;
     }else {
       i++;
     }
   }
   if (found) {
      // print cursor or do whatever
   }
 }
+2

, .

:

    int i = 0;
    String searchTerm = "ta";

    System.out.println("Following substrings contain search term:");
    for (String s : "hi guys,this,is,sparta".split(",")) {
        if (s.contains(searchTerm)) System.out.println(i++);
        else i++;
    }

, , s.contains(searchTerm) s.matches(searchTerm).

, , . : - /, . .

:

+2

, , :

( )

    import java.io.*;

    public class A {
            public static void main(String[] args) {
                    String[] arr = {"hi guys", "this", "is", "sparta"};
                    System.out.println("enter substring:");
                    String substr = "";
                    try {
                    substr = new BufferedReader(new InputStreamReader(System.in)).readLine();
                    } catch(IOException e) {System.exit(0);}
                    for(int i =0; i<arr.length; i++) {
                            int charPos = arr[i].indexOf(substr);
                            if(charPos!=-1) {
                                    System.out.println("found in string index " + i + " at "+charPos);
                                    break;
                            }
                    }
            }
    }
+1

[] ?

, .

0

Source: https://habr.com/ru/post/1794932/


All Articles