Convert String with special characters - java

String descriptionEscaped = "Domnul Florin Cîţu afirmă"
=>
String descriptionEscaped = "Domnul Florin Cîţu afirmă, sâmbătă"
Is there a way to do this ?
(Sorry for the confusing title of question)

following is helping you to convert special characters
public static void main(String[] args) {
String descriptionEscaped = "Domnul Florin Cîţu afirmă";
descriptionEscaped =
StringEscapeUtils.unescapeHtml4(descriptionEscaped);
System.out.println(descriptionEscaped);
}

Related

How to remove particular String from String url

I have to remove this.
String removeKey = "problem_keys";
Code
public class Solution {
public static void main(String args[]) {
String removeKey = "problem_keys";
String url = "{\"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;problem_keys:1,35,3,4,5,6,7,15,16,11,12,16;\";}";
StringBuffer sb = new StringBuffer(url);
removeKey(sb, removeKey);
System.out.println(sb.toString());
}
public static void removeKey(StringBuffer url, String removeKey) {
int sIndex = url.indexOf(removeKey);
while (url.charAt(sIndex) != ';') {
url.deleteCharAt(sIndex);
}
url.deleteCharAt(sIndex);
}
}
Expected output.
{"arrival_mode:Self Drop;schedule:2020-09-10;payment_mode:Pay Online;address_id:67052;problem_id:11;problem_name:Abnormal noise;first:2000;product_name:demo tc;category_name:selfby;brand_name:bpl;transaction_type:Request;type:Display;}";
Guessing you want also to remove the "values", using java 8:
String toRemove = Arrays.stream(url.split(";")).filter(part -> part.contains(removeKey)).findFirst().orElse("");
String newUrl = url.replace(toRemove, "");
System.out.println(newUrl);
Speaking about the delimeters you can consider adding ";" to the toRemove string in a conditional block.
If you're aim is only to get rid of the string removeKey you can just:
url.replace(removeKey, "");
I would go with this:
String removeKey = "problem_keys;";
url = url.replace(removeKey, "") // Replace the whole "problem_keys;" string with an empty string

Java - Cut a string programmatically

I have a string (URL) like this:
"https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp"
I need to extract the last part only i.e. 02_Cuppy_lol.webp.
How can I do that?
Thanks!
You can use substring() and lastIndexOf() here:
String value = completeString.substring(completeString.lastIndexOf("/") + 1);
You can split this text/url and get last part, for example:
String url = "https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] splittedUrl = url.split("/");
String lastPart = splittedUrl[splittedUrl.length()-1)];
you can use the method split().follow this example
public class Demo {
public static void main(String args[]){
String str ="https://www9.online-convert.com/dl/web2/download-file/248f2225-7ed3-48dd-a586-ac1390bbeaab/02_Cuppy_lol.webp";
String[] temp=str.split("/");
int lastIndex =temp.length-1;
String lastPart = temp[lastIndex];
System.out.println(lastPart);
}
}
Output-:
02_Cuppy_lol.webp

Arabic to latin conversion failure?

package com.webom.crypt;
import org.apache.commons.lang3.StringEscapeUtils;
import com.ibm.icu.text.Transliterator;
public class Test {
public static String ARABIC_TO_LATIN = "Any-Arabic";
public static String ARABIC_TO_LATIN_NO_ACCENTS = "Arabic-Latin/BGN; nfd; [:nonspacing mark:] remove; nfc";
public static void main(String[] args) {
String ARABICString = "صدام حسين التكريتي";
String unicodeCodes = StringEscapeUtils.escapeJava(ARABICString);
System.out.println("Unicode codes:" + unicodeCodes);
// conversion
Transliterator ARABICToLatinTrans = Transliterator.getInstance(ARABIC_TO_LATIN);
String result1 = ARABICToLatinTrans.transliterate(ARABICString);
System.out.println("ARABIC to Latin:" + result1);
// conversion
Transliterator ARABICToLatinNoAccentsTrans = Transliterator.getInstance(ARABIC_TO_LATIN_NO_ACCENTS);
String result2 = ARABICToLatinNoAccentsTrans.transliterate(ARABICString);
System.out.println("ARABIC to Latin (no accents):" + result2);
}
}
As conversion of arabic to latin fails because there is issue regarding to the instances .Could you please find out the correct instance string? As when you use google translator it will show exact conversion.

replaceAll Java method to remove "\\n" from String [duplicate]

This question already has answers here:
How to remove the backslash in string using regex in Java?
(3 answers)
Closed 4 years ago.
I have a simple treatement but I'm stuck
I have something like
"\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
String filtre1 = value.replaceAll("\"", "");
String filtre2 = filtre1.replaceAll("\\n", "");
System.out.println(filtre2);
}
the result is .. I have always "\n" I want to remove it
iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\neJzt3XecXVW99
Maybe:
String filtre2 = filtre1.replaceAll("\\n", "");
need to be :
String filtre2 = filtre1.replaceAll("\\\\n", "");
(sorry i cannot just add comment)
You could try this:
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
String filtre1 = value.replaceAll("\"", "");
String filtre2 = filtre1.replaceAll("\\\\n", "");
System.out.println(filtre2);
}
You basically have two choices, both of which are in the below code:
public static void main(String[] args) {
String value = "\"iVBORw0KGgoAAAANSUhEUgAAAwoAAADwCAYAAACg2ZPDAAAABHNCSVQICAgIfAhkiAAAIABJREFU\\neJzt3XecXVW99";
System.out.println(value.replace("\"", "").replace("\\n", ""));
System.out.println(value.replaceAll("\"|\\\\n", ""));
}

Remove a character from java string using hex code

i would like to remove a character from java string using hex code:
i am trying following code but seems to not be correct as the character isn't replaced: ÿ
String str ="test ÿ";
str.replaceAll("\\x{9F}","")
is there any thing wrong with the syntax of the hex code? Thanks.
Could you please try this:
public class AsciiHexCode {
public static void main(String[] args) {
String str = "test ÿ";
String result = str.replaceAll("[^\\x00-\\x7F]", "");
System.out.println("result : "+ result);
}
}
To mach ÿ you need \u00ff instead, as Jon mentioned.
String replaced = str.replace("\u00ff", "");
in your case.

Categories

Resources