-1

I need to extract the specific content from below string

String textValue = "Id:1, type: text, textValue : xx, document: pdf"

How to get only TextValue and its value using java

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • `split` on `,`, then find the `String` starting with `textValue` in the resulting array and split on ` : ` to seperate them again – MadProgrammer Jul 24 '23 at 04:26
  • You could also use a regular expression (like `, textValue.*,`) and [this](https://stackoverflow.com/questions/4662215/how-to-extract-a-substring-using-regex) – MadProgrammer Jul 24 '23 at 04:29

4 Answers4

0

If you want only textValue : xx.

You can split it by comma "," for above String to array and get by index.

String textArray[]=textValue.split(",");
String yourValue=textValue[2];

//if you want whole textValue : xx
System.out.println(yourValue);

//if you want only xx,you can again split by ":"
System.out.println(yourValue.split(":")[1]);


Ajaj Ali
  • 86
  • 4
0

Use regex:

String x = str.replaceAll(".*(textValue : [^,]+).*", "$1");

This matches the entire string but captures the part you want (as group 1), and the replacement is (a back reference to) group 1.

The only interesting part is [^,]+ which means "all following characters that are not a comma"

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

In case you want to retrive data from the string, you can use either str.substring(start, end) - but only if you know the exact position of the data, or you can use regular expression - this is more flexible.

regexp101.com

String str = "Id:1, type: text, textValue : xx, document: pdf";
String regexp = "(?i)id\\s*:\\s*(?<id>\\d+)\\s*,\\s*type\\s*:\\s*(?<type>\\S+)\\s*textValue\\s*:\\s*(?<textValue>\\S+),\\s*document\\s*:\\s*(?<document>\\S+)";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(str);

if (matcher.find()) {
    System.out.println("id: " + matcher.group("id"));
    System.out.println("textValue: " + matcher.group("textValue"));
    System.out.println("document: " + matcher.group("document"));
}

Output:

id: 1
textValue: xx
document: pdf
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
-1

Use Substring: To extract a substring from a string, you can use the substring() method. It takes two parameters: the starting index (inclusive) and the ending index (exclusive) of the desired substring.

String originalString = "Hello, world!";
int startIndex = 7;
int endIndex = 12;
String extractedSubstring = originalString.substring(startIndex, endIndex);
System.out.println(extractedSubstring); // Output: "world"

OR

Splitting by Delimiter: If you want to extract specific parts of a string based on a delimiter, you can use the split() method. It splits the original string into an array of substrings based on the specified delimiter.

String originalString = "apple,orange,banana,grape";
String[] fruitsArray = originalString.split(",");
System.out.println(fruitsArray[2]); // Output: "banana"

OR

Using Regular Expressions (Regex): Regex provides a powerful way to extract specific content from a string based on patterns. You can use the Pattern and Matcher classes from the java.util.regex package to achieve this.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

String originalString = "The price is $19.99";
Pattern pattern = Pattern.compile("\\$(\\d+\\.\\d{2})"); // Matches dollar amount in the format $xx.xx
Matcher matcher = pattern.matcher(originalString);

if (matcher.find()) {
    String extractedAmount = matcher.group(1);
    System.out.println(extractedAmount); // Output: "19.99"
}
Arivazhagan L
  • 39
  • 1
  • 13