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
Related
I'm using JAVA and I have a string called example that looks like;
example = " id":"abcd1234-efghi5678""tag":"abc" "
NOTE: I have't escaped the "'s using \ but you get the idea..
...I want to just return;
abcd1234
...I've been trying using substring
example = (example.substring(example.lastIndexOf("id\":\"")+5));
(because this string could be anywhere in a HTML/JSON File) which kinda works all the lastIndexOf does is find it and then keep everything AFTER it - i.e it returns;
abcd1234-efghi5678""tag":"abc"
Basically I need to find the lastIndexOf based on the string and limit it returns afterwards - I found that I could do it another substring command like this;
example = (example.substring(example.lastIndexOf("id\":\"")+5));
example = example.substring(0,8);
...but it seems messy. Is there any way of using lastIndexOf and also setting a max length at the same time - it's probably something really simple that I can't see due to staring at it for so long.
Many thanks in advance for your help!
Don't substring twice. Use the found index twice instead:
int idx = example.lastIndexOf("id\":\"");
example = example.substring(idx + 5, idx + 13);
Or, if the length is dynamic, but always ends with -:
int start = example.lastIndexOf("id\":\"");
int end = example.indexOf('-', start);
example = example.substring(start + 5, end);
In real code, you should of course always check that the substring is found at all, i.e. that idx / start / end are not -1.
You can use a regex to find the specific substring:
String regex = "^id[^a-z0-9]+([a-zA-Z0-9]+)-.*$";
Matcher p = Pattern.compile(regex).matcher(example);
String result = null;
if (p.matches()) {
result = p.group(1);
}
System.out.println(result); //outputs exactly "abcd1234"
The pattern uses a capturing group that matches id followed by non-alphanumeric characters and preceding -.
I want to parse the following and store it as a new string, with the condition that mawi is stored and everything else is removed.
<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>
One solution I suppose could be a substring starting with the first character after the first > and ending two characters before the first -. All the data is identical. The result is a String with value mawi.
String initial = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
String substring = initial.substring(example.indexOf(">"));
Not sure where to go from here... Any thoughts?
Although the below code do the trick, I suggest you to use Jsoup or XML Parse if you are processing multiple strings like this
Pattern pattern = Pattern.compile("<ns0:Assignee>(.+?)</ns0:Assignee>");
Matcher matcher = pattern.matcher("<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>");
matcher.find();
String result = matcher.group(1);
String finalString = result.split(" - ")[0];
System.out.println(finalString); // mawi
If all the strings are built like your example string, you could go with this:
initial.substring(initial.indexOf('>') + 1, initial.indexOf(' '));
Note the + 1 at the start index.
When your Strings are more complicated, I would recommend either using a library for working with XML or using Regular Expressions.
So now you got substring which is equal to: >mawi - Manfred Wilson</ns0:Assignee>.
Now, you can substring your substring again to find only mawi, like this;
String initial = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
String midSub = initial.substring(initial.indexOf('>'));
String finalSub = midSub.substring(1, midSub.indexOf(' ')); // 1 because we still have `>`
System.out.println(finalSub);
Or, one liner:
String finalSub = initial.substring(initial.indexOf('>')+1, initial.indexOf(' '));
show this:
String s = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
s = s.substring(s.indexOf("<ns0:Assignee>")+"<ns0:Assignee>".length(), s.indexOf("</ns0:Assignee>"));
public class string {
public static void main(String[] args) {
String s = "<ns0:Assignee>mawi - Manfred Wilson</ns0:Assignee>";
s = s.substring(14, 18);
System.out.println(s);
}
}
I have a string = ab:cd:ef:gh. On this input, I want to return the string ef:gh (third colon intact).
The string apple:orange:cat:dog should return cat:dog (there's always 4 items and 3 colons).
I could have a loop that counts colons and makes a string of characters after the second colon, but I was wondering if there exists some easier way to solve it.
You can use the split() method for your string.
String example = "ab:cd:ef:gh";
String[] parts = example.split(":");
System.out.println(parts[parts.length-2] + ":" + parts[parts.length-1]);
String example = "ab:cd:ef:gh";
String[] parts = example.split(":",3); // create at most 3 Array entries
System.out.println(parts[2]);
The split function might be what you're looking for here. Use the colon, like in the documentation as your delimiter. You can then obtain the last two indexes, like in an array.
Yes, there is easier way.
First, is by using method split from String class:
String txt= "ab:cd:ef:gh";
String[] arr = example.split(":");
System.out.println(arr[arr.length-2] + " " + arr[arr.length-1]);
and the second, is to use Matcher class.
Use overloaded version of lastIndexOf(), which takes the starting index as 2nd parameter:
str.substring(a.lastIndexOf(":", a.lastIndexOf(":") - 1) + 1)
Another solution would be using a Pattern to match your input, something like [^:]+:[^:]+$. Using a pattern would probably be easier to maintain as you can easily change it to handle for example other separators, without changing the rest of the method.
Using a pattern is also likely be more efficient than String.split() as the latter is also converting its parameter to a Pattern internally, but it does more than what you actually need.
This would give something like this:
String example = "ab:cd:ef:gh";
Pattern regex = Pattern.compile("[^:]+:[^:]+$");
final Matcher matcher = regex.matcher(example);
if (matcher.find()) {
// extract the matching group, which is what we are looking for
System.out.println(matcher.group()); // prints ef:gh
} else {
// handle invalid input
System.out.println("no match");
}
Note that you would typically extract regex as a reusable constant to avoid compiling the pattern every time. Using a constant would also make the pattern easier to change without looking at the actual code.
I have a string :
"id=40114662&mode=Edit&reminderId=44195234"
All i want from this string is the final number 44195234. I can't use :
String reminderIdFin = reminderId.substring(reminderId.lastIndexOf("reminderId=")+1);
as i cant have the = sign as the point it splits the string. Is there any other way ?
Try String.split(),
reminderIdFin.split("=")[3];
You can use indexOf() method to get where this part starts:
int index = reminderIdFin.indexOf("Id=") + 3;
the plus 3 will make it so that it jumps over these characters. Then you can use substring to pull out your wanted string:
String newString = reminderIdFin.substring(index);
Remove everything else and you'll be left with your target content:
String reminderIdFin = reminderId.replaceAll(".*=", "");
The regex matches everything up to the last = (the .* is "greedy").
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);