0

Is there a better way to extract substring from a string ?

I want to extract characters that are surrounded with " and " in a string. I used substring() and indexOf() methods but I sure there is a better way.

For example, for this input:

I "learn" java "and" python.

The desired output would be:

learn

and

Here is what I tried:

public static void main(String[] args) {
    Scanner putIn = new Scanner(System.in);
    String input = putIn.nextLine();
    String subInput = input.substring(input.indexOf("\"") + 1);
    System.out.println(subInput);
    String strOne= subInput.substring(0, subInput.indexOf("\""));
    System.out.println(strOne);
    String strTwo = subInput.substring(strOne.length() + 4, subInput.length() - 2);
    System.out.println(strTwo);
}
Community
  • 1
  • 1
  • You're not really doing any "extraction" here. What is your goal with this code? – Hovercraft Full Of Eels May 30 '19 at 00:14
  • Extract two substring that are surrounded with " and ". – Reza Mazaheri May 30 '19 at 00:15
  • 2
    Rather than repeating what you've already posted, please consider explaining the details. Your output is simply repeating the second String in your "method" call. – Hovercraft Full Of Eels May 30 '19 at 00:16
  • Input of the program is one string (that have to word that are surrounded with " and ") like: Dreams "keep" me "alive" this program must print two strings that : keep and alive. – Reza Mazaheri May 30 '19 at 00:20
  • 2
    Can quotation marks be escaped like `Tom:"What movie do you like?". Emmy:"I like \"Titanic\""`? What should be matched in this case? Are there any other cases which we should know about? – Pshemo May 30 '19 at 00:22
  • any string in the input that are in quotation marks like this: I like "learning" and "swimming". strOne must be learning and strTwo must be swimming. – Reza Mazaheri May 30 '19 at 00:26

2 Answers2

3

Pattern and Matcher classes seem to fit your needs:

Pattern pattern = Pattern.compile("\"([a-zA-Z]*)\"");
Matcher matcher = pattern.matcher(input);

while(matcher.find()) {
    System.out.println(matcher.group(1));
}

Further explanation:

  • Pattern pattern = Pattern.compile("\"([a-zA-Z]*)\""); -> Create a Pattern that matches letters between quote characters
  • Matcher matcher = pattern.matcher(input); -> Create a Matcher to link the Pattern and the user input.
  • while(matcher.find()) while there are matches... Of course you could add the matched strings to an array, here I just print them.
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27
0

I think regular expressions are perfect for your cause here. With the Pattern class, you can compile a regular expression and return the matches. Here are the details:

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

For the specific regex you want for extracting between quotes, you can reference the discussion here:

RegEx: Grabbing values between quotation marks

The regex solution is nice, because you can easily generalize it beyond your current use case.

Jeremy Owens
  • 159
  • 6