Searching an String array for a sub-string?
I am trying to write a little method in java, but I cannot figure it out. What I want to be able to do is enter a string, and then the value of an 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 set at 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 a开发者_如何学Ct the moment is completely off the wall and wrong, is there any simple way of doing this ? I already have a function called get() defined which returns the value of the current line (i.e get(0) in this case would return 'hi guys'). Can anyone help me out please ?
Thanks a lot :)
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++;
}
}
}
Usually I dont do this, but today is Saturday and I am happy and probably going to get drunk
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
}
}
You should note if this is homework.
One way to do this is:
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++;
}
Or if you prefer using regex, then change s.contains(searchTerm)
with s.matches(searchTerm)
.
If this is not homework, but interview question or work problem, this would be vastly more complex. For example: aminoacid sequence is search term and need to find locations in DNA/RNA where it is located. In that case you need more complex solution.
Examples:
- Brute-Force String Matching
- Suffix Trees String Matching Algorithm ( wiki )
- Boyer-Moore String Matching Algorithm ( wiki )
- Knuth-Morris-Pratt String Matching ( wiki )
- Sunday String Matching algorithm ( wiki )
- Horspool String Matching algorithm ( wiki )
- Rabin-Karp string matching algorithm ( wiki )
- Aho–Corasick string matching algorithm ( wiki )
If understood your task correctly, I would do something like:
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;
}
This would return an index of the first element in the array that contains the substring passed as line parameter.
He says this is not homework, so here it is:
(this actually compiles and works)
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;
}
}
}
}
would’t it be more sensible to search in the actual string[] instead of each line?
then you loop over the array and return the current index if the string at this position contains the substring.
精彩评论