Java : Remove strange special characters from String [duplicate] - java

This question already has answers here:
How can non-ASCII characters be removed from a string?
(10 answers)
Closed 6 years ago.
I want to remove all strange special characters from a string in Java.
Those strange special characters are appearing in form of ?(Question mark) in MS Word.The image of sample string is given below.

You can use
String newString = my_string.replaceAll("\\p{C}", "");
more information about Java Unicode Regular expression Java Unicode Regular expression here

This will work:
String string = yourString.replaceAll("[^\\x00-\\x7F]", "");

Related

Split by : but not :: [duplicate]

This question already has answers here:
Regexp to remove specific number of occurrences of character only
(2 answers)
Closed 2 years ago.
I was wondering how I could split a String by : but not :: using String#split(String)
I am using Java if it makes a difference.
I looked around a lot and I couldn't find anything, and I'm not familiar with Regex...
Example:
coolKey:cool::value should return ["coolKey", "cool::value"]
cool::key:cool::value should return ["cool::key", "cool::value"]
You could try splitting on (?<!:):(?!:):
String input = "cool::key:cool::value";
String[] parts = input.split("(?<!:):(?!:)");
System.out.println(Arrays.toString(parts));
This prints:
[cool::key, cool::value]
The regex used here says to split when:
(?<!:) the character which precedes is NOT colon
: split on colon
(?!:) which is also NOT followed by colon

Regular Expressions in java trying to match a string in specific format (format consists of | sysbol) [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
Regular Expressions in java
String s1="Anil-anilorg|anotherorg";
String s2="Anil-anilorg|";
I want to find weather s2 is present or sub-string of s1 by using regular expressions, but while I am doing that it is considering this symbol "|" as logical OR
I am using hbaseStringRegexComparator to compare
You need to escape the | as \|, and within a String it becomes "\\|".
You can use String.contains method. No regex needed:
String s1="Anil-anilorg|anotherorg";
String s2="Anil-anilorg|";
System.out.println(s1.contains(s2));

Convert regex into java regex [duplicate]

This question already has answers here:
Regexp Java for password validation
(17 answers)
Closed 5 years ago.
string should not be longer than 26 alphanumeric characters
string should not begin with www OR api OR admin
string may contain hyphens
I have this regular expression that works:
^(?!www)(?!admin)(?!api)[a-zA-Z0-9.]{1,26}
Can you help me convert that regex into a java style string regex?
I found the answer by changing my regex to the following:
^(www|api|admin)\w{1,26}$

How to split a string in java by a non printable ascii character (Example - Record Seperator) [duplicate]

This question already has answers here:
How to split a string with any whitespace chars as delimiters
(13 answers)
Closed 6 years ago.
In C# this would work -
String[] elements = sample.Split((char)30);
What is the java equivalent?
String.split() takes a regex string. For your purpose, you just need to convert the char to a String:
String[] elements = sample.split(Character.toString((char)30));

Android Spliting the string to array [duplicate]

This question already has answers here:
Splitting a Java String by the pipe symbol using split("|")
(7 answers)
Closed 7 years ago.
I want to split an android string to smaller ones with any | char.
Just imagine I have this long string :
This|is|a|long|string|in|java
So, I wanna split it. I need to get a array in output with this values :
[1]=>"This"
[2]=>"is"
[3]=>"a"
[4]=>"long"
[5]=>"string"
[6]=>"in"
[7]=>"java"
I have tried :
separated = oldstring.split("|");
But, i didn't give me the thing i need!
How can i do that? Any code can do that?
Note that String's split() method take regex as a param. Not string.
public String[] split(String regex)
Since | is a meta character, and it's have a special meaning in regex.
It works when you escape that.
String separated[] = oldstring.split("\\|");

Categories