Reputation: 1866
While I was programming I found weird behavior for the java String. I am trying to parse a string as command with arguments:
Below is a screenshot of the variables during debug.
This is my code to read the command:
public List<String> readCommand(Sender sender, String command) {
boolean isInQuote = false;
List<String> splits = new ArrayList();
String current = "";
char[] arr = command.toCharArray();
for (int i = 0; i < command.toCharArray().length; i++) {
char c = arr[i];
if (c == '"') {
isInQuote = !isInQuote;
if (!isInQuote) {
splits.add(current);
current = "";
}
}
if (isInQuote) {
current += c;
} else {
if (c == ' ' || i == arr.length - 1) {
if (i == arr.length - 1) {
current += c;
}
splits.add(current);
current = "";
} else {
current += c;
}
}
}
return splits;
}
As expected in the tests; the string should be parsed as:
instead it is parsed as:
Why don't the escaped quotes work and what am I doing wrong?
P.S.: I would try to research this subject but I don't know how to call this. Argument parsing with quotes...?
UPDATE: After your help, I discovered another bug which I fixed. The code is fully working now. All that is left now is to remake it :). The \" not working really confused me. http://pastebin.com/AdBUqJvH
Upvotes: 0
Views: 125
Reputation: 9041
First let's simplify your current attempt with:
public static void main(String[] args) throws Exception {
String data = "this \"is a test\" now \"hello\" goodbye";
List<String> splits = new ArrayList();
String current = "";
boolean inQuote = false;
for (int i = 0; i < data.length(); i++) {
if (data.charAt(i) == ' ' && !inQuote) {
// Add your current split word and move on to the next character
splits.add(current);
current = "";
continue;
} else if (data.charAt(i) == '\"') {
// Flip the flag whenever you run across a quotation mark
inQuote = !inQuote;
}
// Add current character to string, spaces never get added
current += data.charAt(i);
}
// Add remaining split data from hitting the end of data
if (!current.isEmpty()) {
splits.add(current);
}
// Display results
for (String split : splits) {
System.out.println(split);
}
}
Results:
this
"is a test"
now
"hello"
goodbye
Then you can shorten your code with Regex
with the following (and the results are the same):
public static void main(String[] args) throws Exception {
String data = "this \"is a test\" now \"hello\" goodbye";
Matcher matcher = Pattern.compile("[^\\s\"']+|\"[^\"]*\"|'[^']*'").matcher(data);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
Upvotes: 1
Reputation: 1452
Your code works fine for me . except that the out put is
this "is a test" " now
instead of
this "is a test" now
I made a small change for that
you said you don't the input doesn't always contains '"', but you are hard coding it with '"', so may be the answer by [Igor Sadovnikov] is better?
public static void main(String[] args) {
String s = "this \"is a test\" now";
List<String> commands = readCommand(null, s);
for (String command : commands) {
System.out.print(command + " ");
}
}
private static List<String> readCommand(Object sender, String command) {
boolean isInQuote = false;
List<String> splits = new ArrayList<String>();
String current = "";
char[] arr = command.toCharArray();
for (int i = 0; i < command.toCharArray().length; i++) {
char c = arr[i];
if (c == '"') {
isInQuote = !isInQuote;
if (!isInQuote) {
//CHANGE HERE ... added + c
splits.add(current + c);
current = "";
}
}
if (isInQuote) {
current += c;
} else {
if (c == ' ' || i == arr.length - 1) {
if (i == arr.length - 1) {
current += c;
}
splits.add(current);
current = "";
}
// --- CHANGE HERE
else if (c != '"') {
current += c;
}
}
}
return splits;
}
Upvotes: 1
Reputation: 751
This is solution for your task.
public static void main(String[] args) {
List<String> splits = readCommand("this \"is a\" test\" now");
for(String str : splits) {
System.out.println("_"+str+"_");
}
}
public static List<String> readCommand(String command) {
List<String> list = Arrays.asList(command.split("\""));
List<String> list2 = new ArrayList<>();
for(String str : list) {
str = checkFirst(str);
str = checkLast(str);
list2.add(str);
}
return list2;
}
private static String checkFirst(String str) {
if (str.charAt(0) == ' ') {
str = checkFirst(str.substring(1));
}
return str;
}
private static String checkLast(String str) {
if (str.charAt(str.length() - 1) == ' ') {
str = checkLast(str.substring(0, str.length() - 1));
}
return str;
}
Upvotes: 1