user3889972
user3889972

Reputation: 13

How can I remove characters around words?

I need to remove all characters that are not alphabetical from the beginning and end of each word. For example:

--Hello& World-@ 1234...

Should look like:

Hello World 1234

I tried replaceAll, but I have no idea how many different characters I need to remove or what those characters are. I tried the following, but it didn't work.

word = resultString.replaceAll("[^a-zA-Z_0-9|$a-zA-Z_0-9]|^-|$-|^--|$--|^---|$---|\\$", "");

There were still words that showed up with dashes.

Is there another way to do this without using replaceAll?

Upvotes: 1

Views: 120

Answers (4)

DenisFLASH
DenisFLASH

Reputation: 742

To be strict, all the answers don't solve exactly what the author asked. The problem is that they all will delete special characters even inside words, and not only "from the beginning and end of each word".

Here is the code which fixes it:

String str = "--Hello& World-@ 1234...  my email is [email protected]";

// Analyzing every word     
String[] words = str.split("\\s+");
String regex = "^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$";
String result = "";

for (String word : words) {
    result += word.replaceAll(regex, "") + " ";             
}

System.out.println(result); // gives "Hello World 1234 my email is [email protected] "

Regex "^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$" explanation:

^[^a-zA-Z0-9]+ matches one or more special characters at the beginning of the word

| OR

[^a-zA-Z0-9]+$ one or more special characters at the end of the word.

You can modify the regex in order NOT TO DELETE the ,.!?:; or other meaningful characters at the end of a word.

Upvotes: 1

Rengasamy
Rengasamy

Reputation: 1043

Try this, It replaces the characters other than letters or words,

word = resultString.replaceAll("[^\\w]", " ");

and output will be

Hello World 1234

Upvotes: 0

shree.pat18
shree.pat18

Reputation: 21757

Try this:

resultString = resultString.replaceAll("[^A-Za-z0-9_\\s]","");

This will replace everything except letters, numbers, underscores and spaces.

Input: "--Hello& World-@ 1234..."

Output: "Hello World 1234"

If you don't want to keep underscores in the string change the pattern to [^A-Za-z0-9\\s].

Upvotes: 1

SahuKahn
SahuKahn

Reputation: 109

It should be:

str.replaceAll("[^A-Za-z0-9\\s]","") 

without the underscore, as the OP mentioned he wanted to remove all characters that are not alphabetical from the beginning and end of each word

Input:

--Hello& World-@ 1234...

Output:

Hello World 1234

This answer fails when there is an underscore in the string.

Upvotes: 0

Related Questions