I have an Array
of Strings
that was split from a buffer string. Now each item in the array has a {value, offset, count, & hash}
. Ho开发者_Python百科w can I get the offset of the item in the array?
Example:
String buffer = aVeryLongString;
String[] splitStringArray = buffer.split(regex);
for(String s: splitStringArray) {
// Get the offset of each item
// Do something
}
String buffer = aVeryLongString;
String[] splitStringArray = buffer.split(regex);
int offset = -1;
for(String s: splitStringArray) {
offset = buffer.indexOf(s, offset + 1); // avoid duplicates
System.out.println(offset);
}
Using String.indexOf(String str, int offset)
you can find out the offset of a string. It starts searching for the string at the given offset. So using the offset of the previous string will solve the problem with the duplicates.
String.indexOf(String str) should work.
for(String s: splitStringArray) {
System.out.println(buffer.indexOf(s));
}
You might want to use the regex Matcher/Pattern classes instead of the String.split function. With the Matcher class you can iterate through matches with find() and get the current position via end().
String.split() doesn't really provide a way to recover this information (without looping through the array and adding previous lengths). If you need extra information like this about the resulting substrings, you might try java.util.Scanner.
Or, as one of the other posters suggested, use the java.util.regex classes, Pattern and Matcher.
If the regex always matches a fixed length, then the offset would be the sum of the lengths of the preceding strings plus the length of the split string.
But if the regex length isn't fixed ... hmm, not an easy problem. You'd have to basically repeat the logic that split uses to find the pieces, I would think.
Say, you want to split a buffer
by whitespace characters. (\S+
stands for non-whitespace characters)
String buffer = aVeryLongString;
Pattern p = Pattern.compile("\\S+");
Matcher m = p.matcher(buffer);
while(m.find()) {
String matchStr = m.group();
int startOffset = m.start();
int endOffset = m.end();
System.out.println("[ " + matchStr + " " + Integer.toString(startOffset) + " " + Integer.toString(endOffset) + " ]");
}
精彩评论