I want to convert weird characters to normal in Java - java

I'm dealing with Java and I want to convert a String like this:
String = "SAYILI BU�DAYLI TARIM KREDİ KOOPERATİFİ"
to
SAYILI BUĞDAYLI TARIM KREDİ KOOPERATİFİ
as output. How can I do that?

You have non printable characters in your input string
String str = "SAYILI BU�DAYLI TARIM KREDİ KOOPERATİFİ";
Normalizer.normalize(str, Normalizer.Form.NFD);
str = str.replaceAll("[^\\x00-\\x7F]", "");
System.out.println(str);
This will remove the non printable characters from you string
output string will be SAYILI BUDAYLI TARIM KRED KOOPERATF

Related

Removing certain characters in a string

I want to remove the equal sign in the string below.
String str = "[=Ind(\"Blr-ind\",\"Company\")]";
You can also use String.replaceAll() to replace all occurrences by any other String
String input = "[=Ind(\"Blr-ind\",\"Company\")]";
input = input.replaceAll("=", "");
System.out.println(input);
Use String.replaceFirst() on the string:
String input = "[=Ind(\"Blr-ind\",\"Company\")]";
input = input.replaceFirst("=", "");
System.out.println(input);
Output:
[Ind("Blr-ind","Company")]
just use the replace method :
String s = "[=Ind(\"Blr-ind\",\"C=ompany\")]";
s = s.replace("=", "");

Encode only specific characters in String

I have to encode only some special characters in a string to numeric value.
Say,
String name = "test $#";
I want to encode only characters $ and # in the above string. I tried using below code but it did not work out.
String encode = URLEncoder.encode(StringEscapeUtils.escapeJava(name), "UTF-8");
The encoded value will be like, for white space the encoded value is &#160
What about to split that String (by string#split method - with space as regex), from Array, which it returns you can use last item and you will get there symbols, what you need :)
String name = "test $#";
String nameSplittedArr = name.split(" ");
String yourChars = nameSplittedArr[nameSplittedArr.length-1]; //indexes from zero
That should works :)
As per the comments, I think you are after a customized encoding function. Something like:
public static String EncodeString(String text) {
StringBuffer sb = new StringBuffer();
for (char c : text.toCharArray()) {
if (Character.isLetterOrDigit(c)) {
sb.append(c);
} else {
sb.append("&#" + (int)c + ";");
}
}
return sb.toString();
}
An example of this is here.

How to split a string into two parts on specific delimeter

I have a string "Rush to ER/F07^e80c801e-ee37-4af8-9f12-af2d0e58e341".
I want to split it into 2 strings on the delimiter ^. For example string str1=Rush to ER/F07 and String str2 = e80c801e-ee37-4af8-9f12-af2d0e58e341
For getting this i am doing splitting of the string , I followed the tutorial on stackoverflow but it is not working for me , here is a code
String[] str_array = message.split("^");
String stringa = str_array[0];
String stringb = str_array[1];
when I am printing these 2 strings I am getting nothing in stringa and in stringb I am getting all the string as it was before the delimiter.
Please help me
You have to escape special regex sign via \\ try this:
String[] str_array = message.split("\\^");
It is because the .split() method requires a regex pattern. Escape the ^:
String[] str_array = message.split("\\^");
You can get more information on this at http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-.

split string and get the value using regex from a complicated string

I have a string like this {"product_tags":"yin_yang,yin yang"}.
what I want to do is just avoid everything else other than yin yang. There is two strings but I just want the first one.
Note that in some cases even if the second string is not available I want to get the same result. And that string might change so it is not necessary that the string will be always yin_yang sometimes it can be motorbike or anything else.
It Look like JSON String Use the JSONParser in java
JSONObject jobject=new JSONObject(STRING);
String value=jobject.getString("product_tags");
EDITED
Using REGEX
String json="{\"product_tags\":\"yin_yang,yin yang\"}";
json=json.replaceAll("([{}]+)", "");
String value[]=json.split(":");
System.out.print(value[1]);
You can use StringTokenizer to parse your string
String str ="{\"product_tags\":\"yin_yang,yin yang\"}";
StringTokenizer to = new StringTokenizer(str,":}");
while(to.hasMoreTokens()){
String firstString = (String) to.nextElement();
String secondString = (String) to.nextElement();
System.out.print(secondString);
}

Replace non-ascii character by ascii code using java regex

I have string like this T 8.ESTÜTESTतुम मेरी. Now using java regex i want to replace non-ascii character Ü, तुम मेरी with its equivalent code.
How can i achieve this?
I can replace it with any other string.
String str = "T 8.ESTÜTESTतुम मेरी";
String resultString = str.replaceAll("[^\\p{ASCII}]", "");
System.out.println(resultString);
It prints T 8.ESTTEST
Sorry, I don't know how to do this using a single regex, please check if this works for you
String str = "T 8.ESTÜTESTतुम मेरी";
StringBuffer sb = new StringBuffer();
for(int i=0;i<str.length();i++){
if (String.valueOf(str.charAt(i)).matches("[^\\p{ASCII}]")){
sb.append("[CODE #").append((int)str.charAt(i)).append("]");
}else{
sb.append(str.charAt(i));
}
}
System.out.println(sb.toString());
prints
T 8.EST[CODE #220]TEST[CODE #2340][CODE #2369][CODE #2350] [CODE #2350][CODE #2375][CODE #2352][CODE #2368]
the problem seems to be how to tell regex how to convert what it finds to the code.

Categories

Resources