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

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", ""));
}

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

How to split URL and take only the host names [duplicate]

This question already has answers here:
What is the fastest way to get the domain/host name from a URL?
(8 answers)
Closed 3 years ago.
Im trying to split URLs, for example https://stackoverflow.com/questions/ and take only stackoverflow.com. How can I do this in Java without using the built in function getHost()?
If you can put your URL into a String , there is this option :
public static void main(String []args){
String str ="https://stackoverflow.com/questions/";
String[] parts = str.split("/");
String part1 = parts[0]; //https:
String part2 = parts[1]; //'nothing'
String part3 = parts[2]; //stackoverflow.com
String part4 = parts[3]; //questions
}
One thing you can do is use String#replaceAll. I know it's not what you want but off the bat it's a decent way to do it.
String uri = "https://stackoverflow.com/questions/";
if (uri.contains("https://")) {
uri = uri.replaceAll("https://", "");
}
if (uri.contains("http://")) {
uri = uri.replaceAll("http://", "");
}
int indexOfBackwardsSlash = uri.indexOf("/");
if (indexOfBackwardsSlash != -1) {
uri = uri.substring(0, indexOfBackwardsSlash);
}
Use URI#getPath.
URI uri = URI.create("https...");
String path = uri.getPath();
You could also use regular expressions:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class UrlRegex {
public static void main(String []args){
String url = "https://stackoverflow.com/questions/";
Pattern pat = Pattern.compile("//([^/]*)"); //match everything between "//" and "/"
Matcher m = pat.matcher(url);
if (m.find()) {
String hostname = m.group(1);
}
}
}
Here you go :
Pattern pattern = Pattern.compile("^(?:(?:http)s?://)?(?<hostGroup>[^/:]+).*");
Matcher matcher = pattern.matcher("https://stackoverflow.com/questions/");
if (matcher.matches()) {
System.out.println(matcher.group("hostGroup"));
} else {
System.out.println("Not found! Invalid URL ^^");
}
The above will find stackoverflow.com for the following urls strings :
https://stackoverflow.com/questions/
http://stackoverflow.com/questions/
stackoverflow.com/questions/
stackoverflow.com:80/questions/
stackoverflow.com/
stackoverflow.com
I guess that's for practicing regex ? Otherwise, prefer using the standard APIs whenever possible - (in your case URI#getHost() !)
Cheers!
If you are sure that you are getting the proper URL format than you can just substring it preferred places.
public static void main(String[] args) {
String url = "https://stackoverflow.com/questions/";
System.out.println(getHostFast(url));
}
public static String getHostFast(String url) {
String subbed = url.substring(url.indexOf('/') + 2);
return subbed.substring(0, subbed.indexOf('/'));
}
The error prof method would need to contain additional check (for example if the next '/' exists after dropping http://.

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

Convert String with special characters

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);
}

Replace dash character in Java String [duplicate]

This question already has answers here:
Java String replace not working [duplicate]
(6 answers)
Closed 9 years ago.
I tried to replace "-" character in a Java String but is doesn't work :
str.replace("\u2014", "");
Could you help me ?
String is Immutable in Java. You have to reassign it to get the result back:
String str ="your string with dashesh";
str= str.replace("\u2014", "");
See the API for details.
this simply works..
String str = "String-with-dash-";
str=str.replace("-", "");
System.out.println(str);
output - Stringwithdash
It's quite easy. You can use an Apache library, that will be useful while you develop an application. This is apache-commons-lang. You can do the following:
public class Main {
public static void main(String[] args) {
String test = "Dash - string";
String withoutDash = StringUtils.replace(test, "-", "");
System.out.println(withoutDash);
}
}

Categories

Resources