How to get a sub string from string using word starts with some text

getting a substring from a string after a particular word in java
java substring
string substring c#
get substring at index java
string substring python
substring of string
how to extract a substring from a string in java using regex
how to get substring after character in java

I am trying to read the word from a string using some starting text. For example

String str = " Ads : 234983 ABCD2987423 availabe"

In the above example, the value will change every time. So I want to read the string which starts with the text "ABCD". i.e (ABCD2987423)

Note: Sometimes the extra text will be added in the string, So index won't work in this example.

You could use String#replaceAll for a one-liner using regex:

String str = " Ads : 234983 ABCD2987423 available";
String text = str.replaceAll(".*\\b(ABCD\\w*)\\b.*", "$1");
System.out.println(text);

This prints:

ABCD2987423

Get Substring from String in Java, Get Substring from String in Java String text = "Julia Evans was born on 25-09-​1984. " Getting a Substring Starting at a Specific Character. First words. To get the first words from a String, we can count word separators (like spaces) in a For-loop. Then we take a Substring to get just those words. Here: The FirstWords method counts spaces in the input string. Once the required number is reached, a Substring is returned. Words: The correct number of words are present in the output. There are 5 and 3 words in the result strings.

In case you plan to use this frequently, you could use RegExp:

public final class AbcdExtractor implements Function<String, String> {

    private final Pattern pattern = Pattern.compile("(?<abcd>ABCD\\d+)");

    public String apply(String str) {
        Matcher matcher = pattern.matcher(str);
        return matcher.find() ? matcher.group("abcd") : null;
    }

}

Then you code could look like this:

AbcdExtractor extractor = new AbcdExtractor();
System.out.println(extractor.apply("Ads : 234983 ABCD2987423 availabe"));                       //  ABCD2987423
System.out.println(extractor.apply("This is another string with ABCD2987423 and with suffix")); //  ABCD2987423
System.out.println(extractor.apply("This is string without data"));                             //  null

Demo: https://regex101.com

String.Substring Method (System), The zero-based starting character position of a substring in this instance. The IndexOf method is used to get the position of the equals character in the string. The call to the the end of the substring. The following example extracts a block of text that contains an XML <definition> element. Any additional feedback? Skip Aside from these obvious gaffes, I'll assume you want the text that follows the FIRST (if it's there) occurrence of the pattern in the string. SUBSTR( columnName, LOCATE( '123', columnName ) + 3 ) The reason for the "+ 3" is that we want to start searching three characters past the beginning of the '123'.

Java - String substring() Method example, Java String substring() method is used to get the substring of a given string In this guide, we will see how to use this method with the help of examples. Returns the substring starting from the specified index i.e beginIndex and I thought it would be ” jump” if we consider the space before the word “jumps” as a character. To extract the substring from the string we have some of the built-in text functions and some of the important functions are LEFT, RIGHT, INSTR and MID in excel. The function Instr will serve as the supporting function for the other three functions. We will see how to use these functions to extract the substrings practically.

An alternative solution. Using streams but no regexes.

Split the string into an array of words, then pick the first word that starts with ABCD.

String[] words = " Ads : 234983 ABCD2987423 available".split(" ");
String foundWord = Stream.of(words).filter(w -> w.startsWith("ABCD")).findFirst().get();

Note: If no word in the input starts with "ABCD", the optional produced by findFirst throws NoSuchElementException when calling get().

String.prototype.substring(), The substring() method returns the part of the string between the start and to know the starting and ending indices as you would in the above examples. the substring() and substr() methods, so you should be careful not to get them confused. let text = 'Mozilla' console.log(text.substring(2,5)) // => "zil"  Let’s say, you want to get a substring of the first 12 characters from a string. We can use the Substring method and pass it starting index 0 and length of the substring 12 to get the first 12 char substring from a string. The following code snippet is an example of retrieving a 12 chars substring from a string. // A long string

Excel substring functions to extract text from cell, How to use Substring functions in Excel to extract text from cell, get a substring before If you are looking to extract a substring starting in the middle of a string, at the To get text following a specific character, you use slightly different a certain substring - one more non-trivial MID formula that pulls a word  The SUBSTRING() function extracts some characters from a string. Syntax. SUBSTRING(string, start, length) Parameter Values. Parameter Description; string: Required

Excel 2013/2016: Get Your Hands Dirty, SUBSTITUTE function is used to replace part of a text string with some other text. Use this function when you know the character(s) to be replaced but not the position. a string: • FIND finds a substring within another text and returns the starting The following formula searches for the six-letter word “Profit” and replaces it  String substring(int beginIndex) Returns the substring starting from the specified index i.e beginIndex and extends to the character present at the end of the string. For example – "Chaitanya".substring (2) would return "aitanya". The beginIndex is inclusive, that is why the character present at the index 2 is included in the substring.

Extract substrings between start and end points, Create string arrays and select text that occurs between substrings. Starting in R2017a, you can create strings using double quotes. str = "The quick brown fox". We can use the powershell’s like operator with wildcard character to check if a string contains a word or another string with case-sensitive and case-insensitive.. Note: You can not use the comparison operator contains to check the contains string, because it’s designed to tell you if a collection of objects includes (‘contains’) a particular object.

Comments
  • Thank you Tim. Giving following error message for the expression - Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  • @daithashankar Maybe reload your page. The code I have in my answer does work (I tested it).
  • It is working as expected. Thank you
  • @daithashankar Can you accept this answer, by clicking the green checkmark to the left?
  • Why not replace .findFirst().get() with .findFirst().orElse(null) to avoid exception?
  • It's an alternative. Depends on what should happen if that string is not found. Is that an error? Then handle it in an exception. Or is null expected return value in that case?