0

I am new to java, i have a string

"rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23"

What I want is to get the Driving Test word. The word is dynamically changes so what I want to happen is get the word between the rdl_mod_name: and the \n.

user2951348
  • 113
  • 1
  • 1
  • 7

4 Answers4

0

I would look into java regular expressions (regex). The String matches method uses a regex to determine if there's a pattern in a string. For what you are doing, I would probably use 'matches(rdl_mod_.*\n)'. The '.*' is a wildcard for strings, so in this context it means anything between rdl_mod and \n. I'm not sure if the matches method can process forward slashes (they signify special text characters), so you might have to replace them with either a different character or remove them altogether.

Sam
  • 598
  • 4
  • 16
0
 Use java's substring() function with java indexof() function.
keshav
  • 3,235
  • 1
  • 16
  • 22
0

Try the following.. It will work in your case..

        String str = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
        Pattern pattern = Pattern.compile("rdl_mod_name:(.*?)\n");
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }

Also you can make use of regex,matcher,pattern to get your desired result..

The following links will also give you a fair idea:

Extract string between two strings in java

Java- Extract part of a string between two special characters

How to get a string between two characters?

Community
  • 1
  • 1
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

Try this code :

String s = "rdl_mod_id:0123456789\n\nrdl_mod_name:Driving Test\n\nrdl_mod_type:PUBL\n\nrdl_mod_mode:Practice\n\nrdl_mod_date:2013-04-23";
String sArr[] = s.split("\n\n");
String[] sArr1 = sArr[1].split(":");
System.out.println("sArr1[1] : " + sArr1[1]);

The s.split("\n\n");will split the string on basis of \n\n. The second split i.e. sArr[1].split(":"); will split the second element in array sArr on basis of : i.e split rdl_mod_name:Driving Test into rdl_mod_name and Driving Test. sArr1[1] is your desired result.

Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46