- How do I replace characters in every string in my list in java?
- 4 Answers 4
- replaceAll() in Java
- Syntax of replaceAll() in Java
- Parameters of replaceAll() in Java
- Return Values of replaceAll() in Java
- Exceptions of replaceAll() in Java
- Example:
- What is replaceAll() in Java?
- Example 1: Java String replaceAll()
- Example 2: Java String Remove White Spaces
- Example 3: Java String Replace the Word
- Escaping Characters in replaceAll()
- Conclusion
- Replacing all non-alphanumeric characters with empty strings
- Solution:
- Explanation:
How do I replace characters in every string in my list in java?
I know of the Collections.replaceAll(list, to replace, replace with) , but that only applies to Strings that are that exact value, not every instance in every String.
4 Answers 4
What you must is to apply the replace function to each of the string in your list.
And as the strings are immutable you will have to create another list where string with no space will be stored.
List result = new ArrayList<>(); for (String s : source)
Immutable means that object can not be changed, it must be created new one if you want to change the state of it.
String s = "how are you"; s = s.replaceAll("\\s+", "");
The function replaceAll returns the new string if you did not assign it to variable s then would still have spaces.
Sorry, I forgot to comment (didn’t know I could), thanks for the help, worked like a charm! I’m glad I’ve found a new helpful community for all my programming problems!
It doesn’t sound very useful.
import java.util.Arrays; import java.util.List; /** * http://stackoverflow.com/questions/20760578/how-do-i-replace-characters-in-every-string-in-my-list-in-java/20760659#20760659 * Date: 12/24/13 * Time: 7:08 AM */ public class SpaceEater < public static void main(String[] args) < ListstringList = Arrays.asList(args); System.out.println("before: " + stringList); for (int i = 0; i < stringList.size(); ++i) < stringList.set(i, stringList.get(i).replaceAll("\\s+", "")); >System.out.println("after : " + stringList); > >
disrvptor was correct — original snippet did not alter the list. This one does.
replaceAll() in Java
The replaceAll() in java is the method of String class that is used to replace each substring that matches the regex of the string with the specified text, another string passed as a parameter . It returns a new string with the changes done.
Syntax of replaceAll() in Java
Method signature for replaceAll() looks like:
Here String represents the String class of java, and regex and replacement are parameters.
Parameters of replaceAll() in Java
replaceAll() in Java takes two parameters:
regex : regular expression
replacement : replacement sequence of characters
Return Values of replaceAll() in Java
return type: string
The replaceAll method returns a new string with all the characters in the original string that match with regex replaced with the string passed as the parameter.
Exceptions of replaceAll() in Java
The replaceAll() method in java throws a PatternSyntaxException which is an Unchecked exception thrown to indicate a syntax error in a regular-expression pattern which is passed in the method as a parameter. This usually happens when the regex parameter that we’re passing is syntactically incorrect.
Following are the four major ways to handle this exception :
- public String getMessage() : It contains the description of the exception.
- public int getIndex() : It will return the index for the error.
- public String getPattern() : It will provide us with the regular expression which contains the error.
- public String getDescription() : It will provide us with decryption with respect to the error.
Example:
Explanation of the example:
In the above example, all the occurrences of the character i i i are replaced by the character z z z .
What is replaceAll() in Java?
Imagine we are writing an article on java, and after the completion of the article, someone tells us to replace all the words ‘ java ‘ with ‘ Java ‘. Now we can individually check all the words and replace them. But it will take a lot of time, right? To tackle this, most text editors have a feature to replace all the occurrences of a word with another word.
We know that in java, the text items are taken input as strings . Now, what if the above problem occurs with the java string? Well in such cases we can use the replaceall() in java.
The replaceAll() in java is the method present in the String class . It takes two parameters:
- A regular expression : A regular expression is a sequence of characters that is used to denote a search pattern in the given string. They can be used to search, edit, or manipulate text and data. Java provides the java.util.regex package for pattern matching with regular expressions. Following
Regex | Description |
---|---|
\w | Matches the word characters. |
\W | Matches the nonword characters. |
\s | Matches the whitespace. |
\S | Matches the non-whitespace. |
\d | Matches the digits. |
\D | Matches the non-digits. |
\A | Matches the beginning of the string. |
\z | Matches the end of the string. |
The replaceAll will match the string items with the parameter it takes. All the items that match the expression will get replaced by the input we pass to the method as a replacement string. Then the replaceAll in java will return a new string with all the characters of the string that match the first parameter replaced by the second parameter.
Following is a pictorial representation of the replaceAll function in java:
The replaceAll in java is present inside the String class (java.lang.String) package.
Example 1: Java String replaceAll()
Explanation of the Example
In the above example, the given two strings are «a string is traditionally a sequence of characters, either as a literal constant or as some kind of variable» and «My password is: 985630″. For the first replaceAll function, » a » is given as the first parameter, which is a string, which will be replaced by «the» (for all occurances). Thus the replaceAll method in java will return a new string as output.
Similarly, the second replaceAll function is given \\d+ as the first parameter, which is the regular expression for sequence of digits , which will be replaced by «*» . Thus the replaceAll method in java will return «My password is: «.
Example 2: Java String Remove White Spaces
Explanation of the Example
In the above example, the given string is » This is a sample string. «. The replaceAll function is given «\\s» as the first parameter which is the regular expression for whitespace , which will be replaced by «» (no space). Thus the replaceAll method in java will return » Thisisasamplestring .».
Example 3: Java String Replace the Word
Explanation of the Example:
In the above example, the given string is «Jva is a programming language. Jva h..». The replaceAll function is given Jva as the first parameter which is the string that will be replaced by «Java» . Thus the replaceAll method in java will return «Java is a programming language. Java has evolved from humble beginnings to power a large share of today’s digital world. Java was first released by Sun Microsystems in 1995.».
Escaping Characters in replaceAll()
As discussed earlier, the replaceAll in java takes regular expression as the first parameter. It is because a typical string in itself is a regex . These regular expressions have metacharacters.
A metacharacter is a character that has a special meaning during pattern processing of the regular expression. Metacharacters are used in regular expressions to define the search criteria and any text manipulations
Following are the metacharacters used in a regular expression :
Since these characters are not treated as general characters, thus In order to match the substring containing the metacharacters, we need to use either \ to escape these characters or use the replace() method in java.
Explanation of the Example:
In the above example, we are given the string » +first-+second «. Now since we want to replace + which is a metacharacter thus we would use escaping characters. Thus the replaceAll method will output » #first-#second «
Conclusion
- The replaceAll in java is a method used to replace all the occurrences of a particular character in a string with other characters.
- The replaceAll in java is present inside the String class.
- replaceAll() takes two parameters regex and replacement strings and returns a new string with all the characters in the original string that match with regex replaced with the string(replacement) passed as the parameter.
- The replaceAll in java throws a syntax error: PatternSyntaxException when there is an error in the regular expression.
- The replaceAll in java is generally used when we have to replace all occurrences of a text with some other text piece.
Replacing all non-alphanumeric characters with empty strings
Note: removed the space since that is not typically considered alphanumeric.
the reg exp is ok, just remove «/» from the regexp string from value.replaceAll(«/[^A-Za-z0-9 ]/», «»); to value.replaceAll(«[^A-Za-z0-9 ]», «»); you don’t need the «/» inside the regexp, I think you’ve confused with javascript patterns
note that this onl works with Latin alphabet and doesn’t works with accent characters or any «special» char set.
return value.replaceAll("[^A-Za-z0-9]", "");
You should be aware that [^a-zA-Z] will replace characters not being itself in the character range A-Z/a-z. That means special characters like é , ß etc. or cyrillic characters and such will be removed.
If the replacement of these characters is not wanted use pre-defined character classes instead:
PS: \p does not achieve this effect, it acts the same as [A-Za-z0-9] .
Thanks a lot for this post — it was very useful to me. Additionally, I believe this is the actual answer to the question. The Latin alphabet isn’t the only one in the world!
Actually, the stated regex will treat «^» as a valid character, since only the first occurrence of «^» is negating the meaning of the selection. [^\\p
@JakubTurcovsky docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html defines IsAlphabetic and IsDigit as binary properties. Alpha and Digit are POSIX character classes (US-ASCII only). Except the docs.oracle.com/javase/10/docs/api/java/util/regex/… flag is specified.
@AndreSteingress Correct, the reason
return value.replaceAll("[^A-Za-z0-9 ]", "");
This will leave spaces intact. I assume that’s what you want. Otherwise, remove the space from the regex.
You could also try this simpler regex:
Java’s regular expressions don’t require you to put a forward-slash ( / ) or any other delimiter around the regex, as opposed to other languages like Perl, for example.
Solution:
Explanation:
[^abc] When a caret ^ appears as the first character inside square brackets, it negates the pattern. This pattern matches any character except a or b or c.
Looking at the keyword as two function:
Moreover regarding a pattern:
- A-Z = all characters included from A to Z
- a-z = all characters included from a to z
- 0=9 = all characters included from 0 to 9
Therefore it will substitute all the char NOT included in the pattern
I made this method for creating filenames:
public static String safeChar(String input) < char[] allowed = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_".toCharArray(); char[] charArray = input.toString().toCharArray(); StringBuilder result = new StringBuilder(); for (char c : charArray) < for (char a : allowed) < if(c==a) result.append(a); >> return result.toString(); >
If you want to also allow alphanumeric characters which don’t belong to the ascii characters set, like for instance german umlaut’s, you can consider using the following solution:
String value = "your value"; // this could be placed as a static final constant, so the compiling is only done once Pattern pattern = Pattern.compile("[^\\w]", Pattern.UNICODE_CHARACTER_CLASS); value = pattern.matcher(value).replaceAll("");
Please note that the usage of the UNICODE_CHARACTER_CLASS flag could have an impose on performance penalty (see javadoc of this flag)