Home > Java > javaTutorial > body text

How to Split Strings on Spaces While Preserving Quoted Substrings in Java?

Mary-Kate Olsen
Release: 2024-11-18 08:22:02
Original
327 people have browsed it

How to Split Strings on Spaces While Preserving Quoted Substrings in Java?

Splitting Strings on Spaces, Preserving Quoted Substrings in Java

Java's string splitting is a versatile tool, but it can stumble upon challenges when encountering quoted substrings. To efficiently split a string based on spaces while treating quoted substrings as a single word, consider the following method:

Using regular expressions, the solution leverages a specific pattern that identifies tokens as either non-quoted sequences of non-whitespace characters or quoted sequences containing any number of characters. The result is a list of tokenized elements that accurately preserve the quoted substrings.

Pattern pattern = Pattern.compile("([^\"]\S*|\".+?\")\s*");
Matcher matcher = pattern.matcher(str);
List<String> tokens = new ArrayList<>();

while (matcher.find()) {
    String token = matcher.group(1);
    tokens.add(token.replace("\"", "")); // Remove surrounding quotes if needed
}
Copy after login

In this example, the string "Location "Welcome to india" Bangalore Channai "IT city" Mysore" will be tokenized as:

Location
Welcome to india
Bangalore
Channai
IT city
Mysore
Copy after login

This method elegantly handles the preservation of quoted substrings, ensuring that meaningful phrases like "Welcome to india" or "IT city" are maintained as single tokens.

The above is the detailed content of How to Split Strings on Spaces While Preserving Quoted Substrings in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template