use pattern and matcher classes with J\\Snos as regex. \\S matches any non-space character.
String str = "foo János bar Jxnos";
Matcher m = Pattern.compile("J\\Snos").matcher(str);
while(m.find())
{
System.out.println(m.group());
}
Output:
János
Jxnos
Answer from Avinash Raj on Stack Overflowuse pattern and matcher classes with J\\Snos as regex. \\S matches any non-space character.
String str = "foo János bar Jxnos";
Matcher m = Pattern.compile("J\\Snos").matcher(str);
while(m.find())
{
System.out.println(m.group());
}
Output:
János
Jxnos
A possible solution would be to strip the accent with the help of Apache Commons StringUtils.stripAccents(input) method:
String input = StringUtils.stripAccents("János");
System.out.println(input); //Janos
Make sure to also read upon the more elaborate approaches based on the Normalizer class: Is there a way to get rid of accents and convert a whole string to regular letters?
This:
public static void main(String[] args) throws Exception {
String REGEX = "\\(s\\)|s$";
System.out.println("Packs".replaceAll(REGEX, "")
.toLowerCase());
System.out.println("packs".replaceAll(REGEX, "")
.toLowerCase());
System.out.println("Pack(s)".replaceAll(REGEX, "")
.toLowerCase());
System.out.println("pack(s)".replaceAll(REGEX, "")
.toLowerCase());
System.out.println("pack".replaceAll(REGEX, "")
.toLowerCase());
}
Yields:
pack
pack
pack
pack
pack
So this should do it:
private static boolean sCompare(String s1, String s2) {
return discombobulate(s1).equals(discombobulate(s2));
}
private static String discombobulate(String s) {
String REGEX = "\\(s\\)|s$";
return s.replaceAll(REGEX, "")
.toLowerCase();
}
Hi I think that this answers your question :) just add another forbidden character to set and it will simply filter that char too.
Set<Character> forbiddenChars = Set.of('s', '{', '}', ' ');
String testString = "This Is{ Test} string";
String filteredString = testString
.toLowerCase()
.codePoints()
.filter(character -> !forbiddenChars.contains((char)character))
.collect(StringBuilder::new, StringBuilder::appendCodePoint,
StringBuilder::append)
.toString();
System.out.println(filteredString);
java - String Comparision without Special Character - Stack Overflow
mysql - Compare strings in java ignore special characters - Stack Overflow
java - compare 2 string in jave with special character - Stack Overflow
How to compare two Strings in java without considering spaces? - Stack Overflow
Filter strings with regexp like [^\\w\\d\\s] - not charachters, not digits, not spaces:
public static void main(String[] args) {
String s1 = "Madam,I'm Adam";
String s2 = "madam i m adam";
System.out.printf(" -> %s\n", normalizedEquals(s1, s2)); // false
}
static String normalize(String s) {
// here goes normalization
return s.replaceAll("[^\\w\\d\\s]", " ");
}
static boolean normalizedEquals(String s1, String s2) {
s1 = normalize(s1);
s2 = normalize(s2);
System.out.printf(" -> %s\n -> %s\n", s1, s2);
return s1.equalsIgnoreCase(s2);
}
Output:
-> Madam I m Adam
-> madam i m adam
-> true
Added the below line and should work
String str = s1.replaceAll("[,']"," ");
Best way to test your regex are http://regexr.com/
In C I'd use isalpha in a loop, removing the unwanted characters before treating the string.
#include <ctype.h>
/* ... */
loop {
if (isalpha((unsigned char)*src)) *dst++ = *src;
src++;
}
Java: str.toLowercase().replace("[^a-zA-Z]", "") will render them all in lowercase with special chars removed and therefore all equal()
Edited to cover everything not alpha as "special"
Use a Collator . See Performing Locale-Independent Comparisons
Most probably getting the Edit Distance/ Levenshtein distance should resolve your issue. This is not an ideal solution but you may use it with significant success.
Check it Your Self
Levenshtein Distance in Java
You can try to create a new string by replacing all empty spaces.
if(a.replaceAll("\\s+","").equalsIgnoreCase(b.replaceAll("\\s+",""))) {
// this will also take care of spaces like tabs etc.
}
then compare.
I think replacing all spaces with an empty string poses the danger of verifying the following situation (finding the two names equal):
String a = "ANN MARIE O'RIORDAN"
String b = "ANNMARIE O'RIORDAN"
I know I may be splitting hairs here, but I found this question while looking for a similar solution to verify SQL queries in a unit test. Because my queries are multi-line static final Strings, I wanted to make sure that I didn't miss a space anywhere.
To that end, I think replacing all whitespaces with a single space, or perhaps a special character is the safest solution - which then does require regex:
if (a.trim().replaceAll("\\s+", " ").equalsIgnoreCase(b.trim().replaceAll("\\s+", " "))) {
// Strings equivalent
}
Thoughts?
I have my code written exactly as it would be if i was comparing Strings, but it wont work. alternatively, changing my character to a String would likely work if there is a way to do that.
Edit- I took a shower and realized that since characters are saved as unicode values, i could just compare them like you would an integer. Eg- if(char == 65)