Java: Extract characters after a specific character
When the case is as simple as just getting anything after a given character, you don't really need regular expressions.
Example
String test = "http://www.example.com/abc?page=6";
String number = test.substring(test.lastIndexOf("=") + 1);
System.out.println(number);
Output
6
Note
If your String
does not contain the =
character, the result will be the whole String
.
That'll happen because method lastIndexOf
will return - 1
, which is summed with +1
in the example, hence returning 0
.
In short, it would return a sub-string of your whole String
starting at 0
and extending to the whole length of the original String
.
Getting a substring from a string after a particular word
yourString.substring(yourString.indexOf("no") + 3 , yourString.length());
Java : Getting a substring from a string after certain character
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);
}
}
Check for a Substring after a particular occurrence of string which is separated by dots
You can do something like this :
String givenStr = "com.web.rit.entity.TestName.create"; // begin str
String wording = "entity"; // looking for begin
String[] splitted = givenStr.split("\\."); // get all args
for(int i = 0; i < splitted.length; i++) {
if(splitted[i].equalsIgnoreCase(wording)) { // checking if it's what is required
System.out.println("Output: " + splitted[i + 1]); // should not be the last item, else you will get error. You can add if arg before to fix it
return;
}
}
how to find before and after sub-string in a string
You can use String.split(String regex)
. Just do something like this:
String s = "123dance456";
String[] split = s.split("dance");
String firstSubString = split[0];
String secondSubString = split[1];
Please note that if "dance"
occurs more than once in the original string, split()
will split on each occurrence -- that's why the return value is an array.
Java: Getting a substring from a string in Text file starting after a special word
please try below code.
public static void main(String[] args)throws Exception
{
File file = new File("/root/test.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
if(st.lastIndexOf("Name:") >= 0 || st.lastIndexOf("Age:") >= 0) {
System.out.println(st.substring(st.lastIndexOf(":")+1));
}
}
}
In java how to get substring from a string till a character c?
look at String.indexOf
and String.substring
.
Make sure you check for -1 for indexOf
.
How to grab substring after a specific word in java
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/")) {
// ...
}
}
Related Topics
Getting Data from Incoming Json in a Java Servlet
Method Does Not Override Method from Its Superclass . Android Fragment
How to Loop Through List of Webelements and Select One Webelement With a Condition
Spring Webclient: How to Stream Large Byte[] to File
Recyclerview Not Calling Oncreateviewholder or Onbindview
Changing Scene When a Button Is Pressed Fxml
Remove Keys from Anywhere in a Json String Without Deserialization
Json to Pojo With Nested Values
How to Get File Extension from Content Type
Broadcast Receiver Is Not Working When Application Is Removed from Background
How to Check Hikaricp Connection Pooling Is Working or Not in Java
Error: Could Not Find or Load Main Class in Intellij Ide
How to Restart a Java Program Here
Jackson Serializes a Zoneddatetime Wrongly in Spring Boot
Round Off a Double While Maintaining the Trailing Zero