Sam Sonnell
Sam Sonnell

Reputation: 43

How to remove extra spaces and new lines in a string?

I have a string variable s which is like a combination of passages. For example,

Passages provides funeral and burial products.

Our products are meant to align with your values and bring you comfort.

Our products allow you to offer personalization , flexibility and innovative choices, helping you provide services to a wider range of customers.

I have to make that string variable of this form:

Passages provides funeral and burial products. Our products are meant to align with your values and bring you comfort. Our products allow you to offer personalization, flexibility and innovative choices, helping you provide services to a wider range of customers.

Plus, extra spaces between words are to be removed(or between a '.' and the first line of the word) an converted to a single space and any number of spaces before ',','.' or ';' is to be removed.

I am a newbie in java. Can anybody tell me how can it be done?

Upvotes: 4

Views: 3439

Answers (6)

Jano Janahan
Jano Janahan

Reputation: 101

The only problem with Regexs is they can be quite slow. IF you are willing to use external libraries, try the Google Guava Library and its CharMatcher

CharMatcher.whitespace().collapseFrom("Hello There\nMy name is Fred   ", ' '))

This will convert the whitespace to a single space, AND collapse multiple sequences of whitespace into a single sequence.

Upvotes: 3

Pankaj Singhal
Pankaj Singhal

Reputation: 16043

string.replaceAll("\n", "").replaceAll("\\s+", " ")

Upvotes: 1

David Pérez Cabrera
David Pérez Cabrera

Reputation: 5048

Try with this: (@Criti's way)

    String s = "Passages provides funeral and burial products.\n"
            + "Our products are meant to align with your values and bring you comfort.\n"
            + "Our products allow you to offer personalization , flexibility and innovative choices, helping you provide services to a wider range of customers.";

    s = s.replaceAll("\\s*\\.\\s*\n\\s*", ". ");
    s = s.replaceAll("\\s*,\\s*", ", ");
    s = s.replaceAll("\\s*;\\s*", "; ");
    System.out.println(s);

Output:

Passages provides funeral and burial products. Our products are meant to align with your values and bring you comfort. Our products allow you to offer personalization, flexibility and innovative choices, helping you provide services to a wider range of customers.

Upvotes: 2

Paul
Paul

Reputation: 20061

I'm a big fan of the Apache Commons Lang library - the StringUtils class (with its null-safe functions) has saved me countless hours over the years. Not surprisingly, StringUtils has a function that does what you're looking for: StringUtils.normalizeSpace(String str)

From the API:

The function returns the argument string with whitespace normalized by using trim(String) to remove leading and trailing whitespace and then replacing sequences of whitespace characters by a single space.

Upvotes: 4

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136022

try this

str = str.replaceAll("\\.\\s+(\\w)", ". $1");

Upvotes: 0

cristi
cristi

Reputation: 361

One way is to parse the String variable character by character. For example

StringBuilder sb = new StringBuilder();
String toBeParse = "...";
for (int i = 0; i < toBeParse.length(); i++) {
    if (toBeParse.charAt(i) == condition) {
        sb.append(toBeParse.charAt(i));
    }
}
String result = sb.toString();

Another way is to use regular expresions:

toBeParse.replaceAll(yourRegexString, replacement);

Upvotes: 0

Related Questions