Problem in replacing special characters in a String in java - java

I got a String as response from server which is like the below:
hsb:\\\10.217.111.33\javap\Coventry\
Now I want to parse this string in such a way that I need to replace all \ with /.
Also I need to remove the first part of the String which is hsb:\\\
So, my resultant string should be of like this :
10.217.111.33/javap/coventry/
Can anyone help me by providing sample java code for this problem.

Here you have a "dirty" startup "solution":
String s = "hsb:\\\\\\10.217.111.33\\javap\\Coventry\\";
String w = s.replace('\\', '/');
String x = w.replace("hsb:///", "");

String result = yourString.substring(7);
result = result.replaceAll("\\\\", "/");

Related

Replace multiple substrings within a string

I have string such as
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3"
I would like to replace the "substrings" "v1","v2" and "v3" with other values. How can I achieve this?
Does something like this work for your case?
String url = "www.test.com/blabla/?p1=v1?p2=v2?p3=v3";
String result = String.format(url.replaceAll("v[0-9]", "%s"), "arg1", "arg2", "arg3");
System.out.println(result); //www.test.com/blabla/?p1=arg1?p2=arg2?p3=arg3
Edit:
Just a brief explanation of what this does, it replaces all the v1,v2,v3,v4,v5,v6,v7,v8,v9,v0 in the original url for %s and then uses this in the format method so you can attribute what you want it to be.

How to convert a ip string xxx.xxx.xxx.xxx:xxxxx to xxx.xxx.xxx.xxx?

I'm getting this ips from my server like:
"/177.127.101.68:53964"
"/201.80.15.100:54263"
"/177.67.38.54:51309"
and i need it to be like just "177.127.101.68", i was going to delete the last 5 string characters but sometimes it comes "/186.213.186.40:4625" so i dont know exactly how to do it... is there any way to do that?
Use the split() and substring() methods of the String class, for one approach:
String ip = "/177.127.101.68:53964";
String whatYouWant = ip.split(":")[0].substring(1);
Please see the Javadocs for split and substring. You'll find yourself using them a lot, in my opinion.
You only need substring:
String ip = "/177.127.101.68:53964";
String result = ip.substring(1, ip.indexOf(':')-1);
Other way around could be something like
String myIP = "/177.127.101.68:53964";
URL url = new URL("http:/" + myIP);// will create URL for http://177.127.101.68:53964
String host = url.getHost(); // will return only 177.127.101.68 part

How to remove a substring in java

I am receiving a file path with "xyz" appended to it. name would look like D:/sdcard/filename.docxyz
i am using the below code to remove xyz but it is not working. what is missing here ?
String fileExtension = path.substring(path.lastIndexOf(".")+1);
String newExtension= fileExtension;
newExtension.replace("xyz", "");
path.replace(fileExtension, newExtension);
return path;
What is missing is that you need to save the result of your operations. Strings are immutable in Java, and the results of all String manipulations are therefore returned in the form of a new String:
newExtension = newExtension.replace("xyz", "");
path = path.replace(fileExtension, newExtension);
String in java are immutable, and changes upon it never occurs in place, but every time a new string is returned,
newExtension = newExtension.replace("xyz", "");
You could also use replaceAll() with a regex.
public static void main(String[] args) {
String s = "D:/sdcard/filename.docxyz";
System.out.println(s.replaceAll("xyz$", "")); // $ checks only the end
}
O/P :
input : s = "D:/sdcard/filename.docxyz";
D:/sdcard/filename.doc
input : String s = "D:/sdcard/filenamexyz.docxyz";
output : D:/sdcard/filenamexyz.doc
newExtension.replace("xyz", "");
Will only return string which has "xyz" removed but newExtension will remain as it is. Simple fix for your problem is use as below
String newExtension= fileExtension.replace("xyz", "");

Using regular expressions to rename a string

In java, I want to rename a String so it always ends with ".mp4"
Suppose we have an encoded link, looking as follows:
String link = www.somehost.com/linkthatIneed.mp4?e=13974etc...
So, how do I rename the link String so it always ends with ".mp4"?
link = www.somehost.com/linkthatIneed.mp4 <--- that's what I need the final String to be.
Just get the string until the .mp4 part using the following regex:
^(.*\.mp4)
and the first captured group is what you want.
Demo: http://regex101.com/r/zQ6tO5
Another way to do this would be to split the string with ".mp4" as a split char and then add it again :)
Something like :
String splitChar = ".mp4";
String link = "www.somehost.com/linkthatIneed.mp4?e=13974etcrezkhjk"
String finalStr = link.split(splitChar)[0] + splitChar;
easy to do ^^
PS: I prefer to pass by regex but it ask for more knowledge about regex ^^
Well you can also do this:
Match the string with the below regex
\?.*
and replace it with empty string.
Demo: http://regex101.com/r/iV1cZ8
Try below code,
private String trimStringAfterOccurance(String link, String occuranceString) {
Integer occuranceIndex = link.indexOf(occuranceString);
String trimmedString = (String) link.subSequence(0, occuranceIndex + occuranceString.length() );
System.out.println(trimmedString);
return trimmedString;
}

How to extract a url from a string in Java?

I have a string containing a short-code which looks like the one below:
some text...
[video url="http://www.example.com/path/to/my/video.ext"]
...some more text...
I want to be able to first check if the string contains that short-code and second extract the URL from it in Java (specifically Android).
use this regex for checking and grabbing url:
\[\w+\s+url="(?<urllink>)[^"]*"\s*]
and get gorup named urllink
try as:
String str = "[video url=\"http://www.example.com/path/to/my/video.ext\"]";
if (str.contains("url=\""))
{
int indexoff = str.indexOf("url=\"");
int indexofff = str.indexOf("\"]");
String strurl = str.substring(indexoff, indexofff - indexoff);
strurl = strurl.Replace("url=\"", ""); //get url string here
}
Android provides several function for this purpose. SOme of this are:
http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/utils/URLEncodedUtils.html
http://developer.android.com/reference/org/apache/http/client/utils/URLEncodedUtils.html
String url = "whatever"
Boolean myBool = url.contains("ate");
String contains. Not sure what extract url means, but the string class has lots of useful functions.
A powerful and maintainable manner it to Java URL.class, then, you can mix with Regex

Categories

Resources