Java: method to get position of a match in a String?
The family of methods that does this are:
int indexOf(String str)
indexOf(String str, int fromIndex)
int lastIndexOf(String str)
lastIndexOf(String str, int fromIndex)
Returns the index within this string of the first (or last) occurrence of the specified substring [searching forward (or backward) starting at the specified index].
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"
How to get the positions of all matches in a String?
Your first approach was a good idea, but String.indexOf does not support regular expressions.
Another easier way which uses a similar approach, but in a two step method, is as follows:
List<Integer> positions = new ArrayList();
Pattern p = Pattern.compile(queryPattern); // insert your pattern here
Matcher m = p.matcher(documentText);
while (m.find()) {
positions.add(m.start());
}
Where positions will hold all the start positions of the matches.
Find string matching position?
You can start from end of string and go backwards, in pseudo code:
char[] str = input.toCharArray()
for i = str.length-1 down to 1:
if str[i] is lowercase:
if toLowerCase(str[i-1]) == str[i] and str[i-1] is uppercase:
return i-1
return -1 // means no match is found
Java code:
char[] str = input.toCharArray();
for(int i=str.length-1; i>=1; --i) {
char c = str[i];
char p = str[i-1];
if (c == Character.toLowerCase(c) && p == Character.toUpperCase(p)){
if (Character.toLowerCase(p) == c)
return i-1;
}
}
return -1;
Find position of a substring in a string(not indexOf)
How about something like this?
int getIndex(String str, String substring)
{
return Arrays.asList(str.split("\\s+")).indexOf(substring)+1;
}
Disclaimer: This isn't at all efficient. It splits the whole string from scratch every time the function is called.
Test code:
String str = "abc def ghi";
System.out.println(getIndex(str, "abc"));
System.out.println(getIndex(str, "def"));
Prints:
1
2
Explanation:
str.split("\\s+")
splits the string by white-space and puts each part into a position in an array.
Arrays.asList
returns an ArrayList
for the array.
indexOf(substring)
finds the position of the string in the ArrayList
.
+1
since Java uses 0-indexing and you want 1-indexing.
Regex to find the position of a character
Use String.indexOf
:
int pos = str.indexOf('*');
if (pos == 0) {
// Found at beginning.
} else if (pos == str.length() - 1) {
// Found at end.
} else if (pos > 0) {
// Found in middle.
}
An alternative would be to use startsWith
/endsWith
/contains
:
if (str.startsWith('*')) {
// Found at beginning.
} else if (str.endsWith('*')) {
// Found at end.
} else if (str.contains('*')) {
// Found in middle.
}
which might be marginally more efficient, since it avoids having to check the entire string in the case that it ends with *
. However, readability of the code should be the primary concern in selecting between these two, since the performance difference would be negligible in many cases.
And, of course, you don't get the actual position of the *
if you use the latter approach. It depends upon what you are really trying to do as to whether that matters.
How to get position of the first letter in a string
If I read the Java docs correctly, you're looking for the start
method of the match object:
String line = "000 1 This is my message";
Pattern p = Pattern.compile("\\p{L}");
Matcher m = p.matcher(line);
if (m.find()) {
System.out.println(m.start());
}
Related Topics
Write a File in Hdfs with Java
Best Way to Format Multiple 'Or' Conditions in an If Statement
What Are the Differences Between Arraylist and Vector
What Is the Significance of Load Factor in Hashmap
How to Open a File with the Default Associated Program
How Is Countdownlatch Used in Java Multithreading
Maven Is Not Working in Java 8 When Javadoc Tags Are Incomplete
What's the Difference Between Spring Data's Mongotemplate and Mongorepository
Intellij Idea Java Classes Not Auto Compiling on Save
Windows Shortcut (.Lnk) Parser in Java
Reverse Each Individual Word of "Hello World" String with Java
Can the Jvm Recover from an Outofmemoryerror Without a Restart
Finding the Second Highest Number in Array
Java Regex Replace with Capturing Group
Java:Why Should We Use Bigdecimal Instead of Double in the Real World