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]);
Related
How can I use split function in java using two delimiters in the same string
I want to get the words with commas and spaces separately
String I = "hello,hi hellow,bye"
I want to get the above string splited as
String var1 = hello,bye
String var2 = hi hellow
Any suggestion is very much valued.
I would try to first split them with one of the delimiters, then for each resulting substring split with the other delimiter.
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.
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);
A string is taken as input which is in the form of 23,4,555,67 via deadline nd another input is key yo search the element linearly ?my question is how can we recognize the elements from string separated by comma
You can split the String using split :
String[] tokens = "23,4,555,67".split(",");
String s = "23,4,555,67"
String[] tokens = s.split(",");
This will give you a string array with the numbers.
Alternatively, you can use a StringTokenizer. (java.util)
This can be used if your string is delimited by more than one characters (can be be used as well in case of single character). Your example using StringTokenizer
SrringTokenizer st = new StringTokenizer("23,4,555,67", ",");
while(st.hasMoreElements())
System.out.println(st.nextToken());
I have a string like this :
12
I want to get split to [2] ignoring 1. Is it possible to do so in java?
You can use the split() method to split on a regex input or, better yet, if you know the exact position or character you want to split at (as seems to be the case here), just use substring() combined with indexOf(). Something like:
String substring = string.substring(0, indexOf("2"));
where string is your original String variable..
If you know the exact index,
String str = "12"
String s = str.substring(1,2); // output 2
or
String s = str.substring(0, indexOf("2")); //output 2
or
char[] chars = str.toCharArray();
char c = chars[1]; // output