Runesr4nerds
Runesr4nerds

Reputation: 19

Split Java string with quotes into args with no quotes

I need to split my string into 3 args with each arg being what is within the quotes and store them in separate variables.

Here is what I have. The code below takes all command line arguments and combines them into one large String.

Example of a string I need to convert:

“METRO Blue Line” “Target Field Station Platform 1” “south”

It should become:

var1 = METRO Blue Line
var2 = Target Field Station Platform 1
var3 = south

I've tried a lot with split("\"") but for whatever reason it doesn't even remove the quotes for me.

// Construct a string to hold the whole args. 
// Implemented it this way because args is separated by spaces
String combine = "";
for(int i = 0; i < args.length; i++)
{
    combine = combine.concat(args[i]);
    combine = combine.concat(" ");
}
System.out.println(combine);

Upvotes: 0

Views: 286

Answers (4)

user681574
user681574

Reputation: 553

In keeping with the RegEx theme of the original answer, I've rewritten my answer to find either normal quotes or smart quotes.

String text = "“METRO Blue Line” “Target Field Station Platform 1” “south”";

String regex = "\"([^\"]*)\"|“([^”]*)”";

ArrayList<String> listOfStrings = new ArrayList<>();

Matcher m = Pattern.compile(regex).matcher(text);
while (m.find()) {
    if (m.group(1) != null) {
        listOfStrings.add(m.group(1));
    } 
    if (m.group(2) != null) {
        listOfStrings.add(m.group(2));
    }
}
for (String temp : listOfStrings) {
    System.out.println(temp);
}

Upvotes: 0

Md khirul ashik
Md khirul ashik

Reputation: 153

You can use String.join() from java 8. Code example

String[] strArray = { "How", "To", "Do", "In", "Java" };
String joinedString = String.join(", ", strArray);System.out.println(joinedString);

Output:

How, To, Do, In, Java

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240870

You are using curved quote (aka Smart Quotes) marks and not accounting it in code

Pattern pattern = Pattern.compile("[“”]");
    String text = "“METRO Blue Line” “Target Field Station Platform 1” “south” ";
    String arr[] = text.split("\\”");
    for (int i = 0; i < arr.length; i++) {
        System.out.println(pattern.matcher(arr[i]).replaceAll("").trim());
    }

Upvotes: 1

Zabuzard
Zabuzard

Reputation: 25903

The symbols and are different to the symbol ". If you split with split("\"") you obviously search for " but not for the other quote symbols and .

You could easily extract them with a Matcher and its find method. Or alternatively use your splitting approach with the correct delimiter: split("” “"). Note that the first and last element will then have a single quote, just remove it.


String input = "“METRO Blue Line” “Target Field Station Platform 1” “south”";
String[] elements = input.split("” “");

// Remove first quote
elements[0] = elements[0].substring(1);
// Remove last quote
String lastElement = elements[elements.length - 1];
elements[elements.length - 1] = lastElement.substring(0, lastElement.length() - 1);

// Output all results
for (String element : elements) {
    System.out.println(element);
}

The output is:

METRO Blue Line
Target Field Station Platform 1
south

An advantage of this approach is that it is very efficient, no extra replacements or stuff like that, just one iteration over the input, nothing more.

Upvotes: 1

Related Questions