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);
}