yourString.substring(yourString.indexOf("no") + 3 , yourString.length());
Answer from Juned Ahsan on Stack OverflowString example = "/abc/def/ghfj.doc";
System.out.println(example.substring(example.lastIndexOf("/") + 1));
A very simple implementation with String.split():
String path = "/abc/def/ghfj.doc";
// Split path into segments
String segments[] = path.split("/");
// Grab the last segment
String document = segments[segments.length - 1];
java - Best method to get substring after a given word in a String? - Stack Overflow
How to grab substring after a specific word in java - Stack Overflow
Java - How to extract a substring from start until the end of a particular word in a string? - Stack Overflow
Java : Getting a substring from a string after certain character - Stack Overflow
You can use RegExp: "\s*totalWinAmount\s*"\s*:\s*(?<totalWinAmount>\d*)
This is Demo at regex101.com
This is the most simpliest way to get couple of values (I think that using json converter is overhead for this reason). But if you want to find more values, then definitely, you have to use json converter (e.g. Jackson).
You could also try to extract the value with the use of regex. Something like this should work:
resultTotalWinAmount = str.split(".*\"totalWinAmount\"\\s*:\\s*")[1].split("\\D+")[0];
That's a bit messy, but works without additional libraries.
However, I agree with Sun. For proper analysis of json you should always use something like Jackson or GSON.
You can use indexOf and substring. First get the start of the link by getting the index of "https://twitter.com/". Then you look for a space after the beginning of the link, if one exists link ends there, otherwise it ends at the end of the message. Then we can use the substring method to get the link:
int startIndex = message.indexOf("https://twitter.com/");
int endIndex = message.indexOf(" ", startIndex);
if (endIndex == -1) {
endIndex = message.length();
}
String link = message.substring(startIndex, endIndex);
Another easy way, split everything by space and check if they match the requirements:
String[] words = message.split(" ");
for (String word : words) {
if (word.startsWith("https://twitter.com/")) {
// ...
}
}
You can use String's indexOf(String str) method to find where the http://etc is. You can then use indexOf(String str, int fromIndex) method to find where the first space after the URL is. Lastly, use substring(int beginIndex, int endIndex) with those two values.
I won't give you the full code; you'll learn by writing it yourself.
indexOf is what you need. Try:
sentence.substring(0, sentence.indexOf(“Administrator”) + ”Administrator”.length())
Regex to the rescue:
String start = sentence.replaceAll("(?<=Administrator).*", "");
This works by matching everything after Administrator and replacing it with nothing (effectively "deleting" it).
The key part of the regex is the look behind (?<=Administrator), which matches immediately after Administrator - between the r and the space.
Assuming that "temp_username_current_timestamp" is not known and is expected to be different every time but you know the word or specific character that precedes what you want to extract, you should use indexOf(String str):
String input = "create table temp_username_current_timestamp other params"
String precedes = "table";
String extracted;
//Get the index of the start of the word to extract
int startIndex = input.indexOf(precedes) + precedes.length;
//Check if the word we are looking for is even there
if(startIndex > -1){
//Get the index of the next space character
int endIndex = input.indexOf(" ", startIndex);
//If there are more parameters following ignore them
if(endIndex > -1){
//Extract the parameter given the indexes found
extracted = input.substring(startIndex, endIndex);
} else {
//If we are at the end of the string just extract what remains
extracted = input.substring(startIndex);
}
}
If you really want to work with substrings:
String s = "create table temp_username_current_timestamp";
int start = s.indexOf("temp");
String t = s.substring(start, s.length()); // temp_username_current_timestamp
int start2 = t.indexOf("_current");
String u = t.substring(0, start2); // temp_username
int start3 = t.indexOf("_timestamp");
String v = t.substring(start3,t.length()); // _timestamp
String result = u + v; // temp_username_timestamp
System.out.println(result);
Output:
temp_username_timestamp
Assuming you are trying to find parent location of specified file simplest way would be using File class or Path instead of String methods. Your code will be more readable and probably safer.
Using java.io.File:
String location = "/abc/def/ghfj.doc";
File f = new File(location);
String parentName = f.getParentFile().getName();
System.out.println(parentName);
Using java.nio.file.Path:
String location = "/abc/def/ghfj.doc";
Path p = Paths.get(location);
String parent = p.getParent().getFileName().toString();
System.out.println(parent);
Output in both cases: def
In case of selecting def in /abc/def/ghfj/ijk/lmn.doc you could use Path#getName(N) where N is zero-based index of elements from farthermost ancestor to selected file like abc is 0, def is 1,...
So your code can look like:
String location = "/abc/def/ghfj/ijk/lmn.doc";
Path p = Paths.get(location);
String parent = p.getName(1).getFileName().toString();
System.out.println(parent);// Output: def
Quick and dirty solution using a regular expression and groups:
public class AClass {
private static final String TEXT = "/abc/def/ghfj/ijk/lmn.doc";
private static final String REGULAR_EXPRESSION = "(/[^/]*){2}/([^/]*)/.*";
public static void main(final String[] args) {
final Pattern pattern = Pattern.compile(REGULAR_EXPRESSION);
final Matcher matcher = pattern.matcher(TEXT);
if (matcher.matches()) {
// the following variable holds "ghfj"
String value = matcher.group(2);
System.out.println(value);
}
}
}
Now you need to be more precise in order to allow us to fine tune the regular expression to your concrete needs.
Edit: I edited the solution according to your own edit. The regular expression has to be understood as follows:
(/[^/]*){2}: two times the character/followed by any character except//: an additional/character([^/]*): a group of characters not containing//.*: a/character followed by any other character
Then matcher.group(2) returns the String held by the group represented by the ([^/]*) part of the above regular expression.