I need to substract an specific parameter (urlReturn) from a URL like this :
http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&urlReturn=http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323&fh=05012014124755&store=TESO
My final string should look like this:
String urlReturn = http://somedomain.mx:7003/SP/app/login.xhtml?id=1234&mat=2323;
And the rest of the string should look like this:
String urlReturn2 = http://somedomain.mx/th=6048000&campus=A&matric=L01425785&serv=GRAD&count=2&per=TEST&passwd=69786e720c0f0e&mount=1000.00&fh=05012014124755&store=TESO
I currently have this :
String string = string.toString().split("\\&")[0];
But the urlReturn parameter should always come as the first one.
Try this (s is your original String):
urlReturn = s.substring(0, s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
urlReturn2 = s.substring(s.indexOf("&urlReturn=")).replace("&urlReturn=", "");
Definitely not elegant at all, but working. I really need some sleep now so take my anser carefully :) You may alswo wanto to check if the parameters is in the String s via the contains method to avoid index out of bounds exceptions.
use string#split
url.split("\\?")[1];
Related
Hello I have a url string like
http://example.com/foo/?bar=15&oof=myp
Now lets say that I want to change the int value in the bar parameter to 16, in order to have
http://example.com/foo/?bar=16&oof=myp
How can I do this? Considering that the number after the = might be of 1, 2 or ever 3 characters. Thank you
You can use UriComponentsBuilder (it's part of Spring Web jar) like this:
String url = "http://example.com/foo/?bar=15&oof=myp";
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString(url);
urlBuilder.replaceQueryParam("bar", 107);
String result = urlBuilder.build().toUriString();
Substitute 107 with the number you want. With this method you can have URI or String object from urlBuilder.
Use regex to find the number parameter in the string url
Use String.replace() to replace the old parameter with the new parameter
I have this string "nullRobert Luongo 431-232-1233 Canada"
and I'd like to get rid of the null character. I don't know how it got there, but I would like to get rid of it.
It's likely that you get it because you did something like this:
String s = null;
.
.
.
s+="Robert Luongo 431-232-1233 Canada"; //mind the +=
One way to correctly do it is by:
String s= "";
You're concatenating a String that is null -- don't do that. Initialize the String at least with ""
So rather than
String myString;
When you do this and later do:
myString += "something";
you'll get `nullsomething
So instead assign an empty String to the String of interest when you declare it.
String myString = "";
Option 1: Recommended
I would suggest you to look into the issue from where the string is contains 'null' and fix it at the source rather than using hacks.
Option 2: Not recommended
But still if you want to replace the null from it then you can use the following. But it is not recommended as it can replace a proper word containing 'null' in it and make the string junk.
The following replaces the first instance of 'null' with empty String.
string.replace("null", "");
You can also replace all the instances of 'null' with empty String using replaceAll.
string.replaceAll("null", "");
You can use this.
String stringWithoutNull = stringWithNull.replaceAll("null", "");
So I'm looking to get the first numbers of an IP. Let's say I have something like 255.35.54.34. I want to get the first part of numbers up until the first period. How would I do this in Java? So it'd leave me with 255.
Take a look at the String class. You can use a couple of methods to accomplish this:
the indexof(...) method will give you the offset of the "."
the substring(...) method will allow you to get a string using the above offset
Or another option is to use the split(...) method to get an array of all four IP values.
As #camickr says, you can use indexOf and substring thus:
String ipAddress = "192.168.1.9";
System.out.println(ipAddress.substring(0, ipAddress.indexOf('.')));
This will print "192"
You can use the split() method with a period as the argument. This will split the string along periods and give you a String[].
Then get the values just as you would from a normal array by using a subscript. In your case index 0 will get you the value
String ip = "255.255.255.255";
String[] splitIP = ip.split(".");
String required = splitIP[0];
You can do it in two ways :
One is that you use String.split() method, and other is to using StringTokenizer class.
Using String.split() :
String ip = "255.1.2.3";
String[] splitIP = ip.split("\\.");
String required = splitIP[0];
System.out.println(required);
Here \\ is required,oherwise it will throw an exception
Using StringTokenizer :
String ip = "255.1.2.3";
StringTokenizer tk=new StringTokenizer(ip,".");
while (tk.hasMoreTokens())
{
System.out.println(tk.nextToken());
break;
}
Hope this will help you..
How can I substring a string if I have to search for a specific word in a string first which will become the start point of substring?
For example, I have a url like this http://www.youtube.com/get_video_info?html5=1&video_id=fn45T6k5JzA&cpn=SS3mhNaZwOE7WnYl&ps=native&el=embedded&hl=en_US&sts=15956&width=500&height=400&c=web&cver=html5 and I need to substring video ID from it.
You can try regular expressions
Here's some information on using regular expressions in java
The following regular expression:
video_id=(.*?)&
should do it.
http://rubular.com/r/d24wDwk2wv
To answer the title explicitly:
s = s.substring(s.indexOf("word") + "word".length());
Use this:
String url="http://www.youtube.com/get_video_info?html5=1&video_id=fn45T6k5JzA&cpn=SS3mhNaZwOE7WnYl&ps=native&el=embedded&hl=en_US&sts=15956&width=500&height=400&c=web&cver=html5";
String subData=url.substring(url.indexOf("video_id=")+"video_id=".length(),url.indexOf("&",url.indexOf("video_id="))); // outputs fn45T6k5JzA
Have a look at different variations string#indexOf method here.
In this case you would use String.indexOf(String toIndex) and this will give the index of the first character in the substring. If you do:
String video = "http://www.youtube.com/get_video_info?html5=1&video_id=fn45T6k5JzA&cpn=SS3mhNaZwOE7WnYl&ps=native&el=embedded&hl=en_US&sts=15956&width=500&height=400&c=web&cver=html5";
String data = video.substring(video.indexOf("video_id=") + 9);
This should give you what you're looking for.
You can use substring from between posithon after video_id= and its next &
String link = "http://www.youtube.com/get_video_info?html5=1&video_id=fn45T6k5JzA&cpn=SS3mhNaZwOE7WnYl&ps=native&el=embedded&hl=en_US&sts=15956&width=500&height=400&c=web&cver=html5";
int start = link.indexOf("video_id") + "video_id".length() + 1; // +1 to include position of `=`
int end = link.indexOf("&", start);
String value = link.substring(start, end);
System.out.println(value);
output: fn45T6k5JzA
Other, probably more readable way would be using URLEncodedUtils from Apache HttpComponents
String link = "http://www.youtube.com/get_video_info?html5=1&video_id=fn45T6k5JzA&cpn=SS3mhNaZwOE7WnYl&ps=native&el=embedded&hl=en_US&sts=15956&width=500&height=400&c=web&cver=html5";
List<NameValuePair> parameters = URLEncodedUtils.parse(link,
Charset.forName("UTF-8"));
for (NameValuePair nvp : parameters) {
if (nvp.getName().equals("video_id"))
System.out.println(nvp.getValue());
}
output: fn45T6k5JzA
I have a string like this:
String str="\"myValue\".\"Folder\".\"FolderCentury\"";
Is it possible to split the above string by . but instead of getting three resulting strings only two like:
columnArray[0]= "myValue"."Folder";
columnArray[1]= "FolderCentury";
Or do I have to use an other java method to get it done?
Try this.
String s = "myValue.Folder.FolderCentury";
String[] a = s.split(java.util.regex.Pattern.quote("."));
Hi programmer/Yannish,
First of all the split(".") will not work and this will not return any result. I think java String split method not work for . delimiter, so please try java.util.regex.Pattern.quote(".") instead of split(".")
As I posted on the original Post (here), the next code:
String input = "myValue.Folder.FolderCentury";
String regex = "(?!(.+\\.))\\.";
String[] result=input.split(regex);
System.out.println("result: "+Arrays.toString(result));
Produces the required output (an array with two values):
result: [myValue.Folder, FolderCentury]
If the problem you're trying to solve is really that specific, you could do it even without using regular expression matches at all:
int lastDot = str.lastIndexOf(".");
columnArray[0] = str.substring(0, lastDot);
columnArray[1] = str.substring(lastDot + 1);