0

In Java ,i have this line from a log file :

    Hello "My name is Vader"

i need to take as a string all the words between " "

i only managed to read them as words(strings) separately, with the

    matcher.nextToken()

but i need to take the whole string between " "

Thank you in advance!

TheVader
  • 17
  • 1
  • 1
    regex can help you... – ΦXocę 웃 Пepeúpa ツ Apr 13 '17 at 18:47
  • Often before asking such questions it's better to first do a Google search for similar questions like this one:[Google Search: site:stackoverflow.com java extract string between quotes](https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=site:stackoverflow.com+java+extract+string+between+quotes), and then reading the best hits. There's nothing new under the sun, and this same type of question has been repeatedly asked previously. – Hovercraft Full Of Eels Apr 13 '17 at 18:49

1 Answers1

1
public String extractTextBetweenQuotes(String str) {
    int start = str.indexOf('"');
    if (start < 0) throw new IllegalStateException("no first quote");
    int end = str.indexOf('"', start + 1);
    if (end < 0) throw new IllegalStateException("no second quote");
    return str.substring(start + 1, end);
}

This assumes that you always have 2 double quotes in your input. If not, it's 'throw exception' policy has to be changed to something different.

Roman Puchkovskiy
  • 11,415
  • 5
  • 36
  • 72