I have following string
String str = "url:http://www.google.com"
Now I want to split the above string using :.
If I split above string using : then above string split into 3 segments.
But I want whole URL in one segment. How can I get the whole URL?
Three is an one way that I found using substring
String webURL = str.substring(4, str.length());
Is there any other best way to that?
You can call String.split(String, int) where the second argument is a limit (or count). Something like,
String str = "url:http://www.google.com";
String[] arr = str.split(":", 2);
System.out.println(arr[1]);
Output is (as requested)
http://www.google.com
String str= "url:http://www.google.com";
// find the first : and take string beyond that
str = str.substring(str.indexOf(':')+1);
System.out.println(str);
Related
I have String like String str = "Abhishek Patel(123121)"; Nd I want Split String in two part.
String Name = "Abhishek Patel";
String ID = 123121;
i had tried like this in in java
String str = "Abhishek Patel(123121)";
String a[] = str.split("(");
String Name =a[0];
You can use a combination of split and substring
String name = "Abhishek Patel(1234567)";
String[] parts = name.split("\\(");
System.out.println(parts[0]);
System.out.println(parts[1].substring(0, parts[1].length() -1));
As #JoakimDanielson has correctly pointed out, if the last ) is optional then it maybe be better to use replace rather than substring
System.out.println(parts[1].replace(")", ""));
Take advantage of two facts.
The split method by default throws away any empty strings that appear after the matches.
You don't need to escape ( or ) if they appear in [] characters in a regular expression.
So you can just write this.
String toSplit = "Abishek Patel(12345)";
String[] parts = toSplit.split("[()]");
This gives an array of only two elements, not three, and they are the name and id.
Try this. will help you
String str = "Abhishek Patel(123121)";
String a[] = str.replace("(", " ").replace(")", " ").split(" ");
String Name =a[0];
String id =a[1];
System.out.println(Name);
System.out.println(id);
EDIT-------------
as suggested by Scary Wombat that there could be 2 spaces in the name it self. You can change this to something else.
The basic idea was to remove the unwanted and boundry characters with one common and split then.
Thanks #ScaryWombat.
For instance I have a String that contains:
String s = "test string *67* **Hi**";
I want to to get this String :
*67*
With the stars, so I can start replace that part of the string. My code at the moment looks like this:
String s = "test string *67* **Hi**";
s = s.substring(s.indexOf("*") + 1);
s = s.substring(0, s.indexOf("*"));
This outputs: 67 without the stars.
I would like to know how to get a string between some special character, but not with the characters together, like I want to.
The output should be as followed:
//output: test string hello **hi**
To replace only the string between special characters :
String regex = "(\\s\\*)([^*]+)(\\*\\s)";
String s = "test string *67* **Hi**";
System.out.println(s.replaceAll(regex,"$1hello$3"));
// output: test string *hello* **Hi**
DEMO and Regex explanation
EDIT
To remove also the special characters use below regex:
String regex = "(\\s)(\\*[^*]+\\*)(\\s)";
DEMO
You just need to extend boundaries:
s = s.substring(s.indexOf("*"));
s = s.substring(0, s.indexOf("*", 1)+1);
s = s.substring(s.indexOf("*"));
s = s.substring(0, s.indexOf("*", 1) + 1);
Your +1 is in the wrong place :) Then you just need to find the next one starting from the second position
I think you can even get your output with List as well
String s = "test string *67* **Hi**";
List<String> sList = Arrays.asList(s.split(" "));
System.out.println(sarray.get(sarray.indexOf("*67*")));
Hope this will help.
Java- Extract part of a string between two similar special characters.
I want to substring the second number, example :
String str = '1-10-251';
I want the result to be: 10
String str = "1-10-251";
String[] strArray = str.split("-");
System.out.println(strArray[1]);
I have one string and I want to split it into substring in Java, originally the string is like this
Node( <http://www.mooney.net/geo#wisconsin> )
Now I want to split it into substring by (#), and this is my code for doing it
String[] split = row.split("#");
String word = split[1].trim().substring(0, (split[1].length() -1));
Now this code is working but it gives me
"wisconsin>"
the last work what I want is just the work "wisconsin" without ">" this sign, if someone have an idea please help me, thanks in advance.
Java1.7 DOC for String class
Actually it gives you output as "wisconsin> " (include space)
Make subString() as
String word = split[1].trim().substring(0, (split[1].length()-3));
Then you will get output as
wisconsin
Tutorials Point String subString() method reference
Consider
String split[] = row.split("#|<|>");
which delivers a String array like this,
{"http://www.mooney.net/geo", "wisconsin"}
Get the last element, at index split.length()-1.
String string = "Enter parts here";
String[] parts = string.split("-");
String part1 = parts[0];
String part2 = parts[1];
you can just split like you did before once more (with > instead of #) and use the element [0] istead of [1]
You can just use replace like.
word.replace(char oldChar, char newChar)
Hope that helps
You can use Java String Class's subString() method.
Refer to this link.
I have this string,
anyType{image0=images/articles/4_APRIL_BLACK_copy.jpg; image1=images/articles/4_APRIL_COLOR_copy.jpg; }
What i want is only
"images/articles/4_APRIL_BLACK_copy.jpg"
How do i get this?
This is how I perform a split in my app.
String link = "image0=images/articles/4_APRIL_BLACK_copy.jpg";
String[] parts = link.split("=");
String first = parts[0];
Log.v("FIRST", first);
String second = parts[1];
Log.v("SECOND", second);
This method will split your string into 2 at the "=" and give you 2 split strings. In your case, the String second is the result you want.
This should work:
s.split("=")[1]
You are splitting the string on = which would return substrings in an array. The second element is what you need.