How to split a string with numbers in java - java

I have string "ali22mehdi35abba1lala2". I want to split it to {"ali","mehdi","abba","lala"};
How should I do that ?
I saw here and another place. I can't acheive my end.

Try following code
String string="ali22mehdi35abba1lala2";
String tok[]=string.split("\\d+");
Now tok would have the split array from numbers.

Use this:
String[] phNo = "ali22mehdi35abba1lala2".split("\\d+");

Related

Get two different delimiters from same string

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.

How to Ignore the desired string during the split in Java?

I have a string like
pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>
and i need to split it by ',' but don't want to split (array of struct) array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>
I want split method to ignore these array of struct and split the rest of the string.
How can i achieve this by split method?
Any help would be appreciated.
You can use a regex to replace your array of struct before doing split like this:
String value = "pchase_history:array<struct<pchase_channel:string,trans_dt:string,sku_id:string,sold_qty:bigint>>,first_pchase_dt:string,last_pchase_dt:string,trans_cnt:bigint,last_pchase_sku_cnt:bigint,no_of_pchase_days:bigint,lst_pchase_channel:array<struct<pchase_channel:string>>";
value = value.replaceAll("(array<struct<.*?>>)", "array");
String[] splitedValues = value.split(",");
System.out.println(Arrays.toString(splitedValues));
Output:
[pchase_history:array, first_pchase_dt:string, last_pchase_dt:string, trans_cnt:bigint, last_pchase_sku_cnt:bigint, no_of_pchase_days:bigint, lst_pchase_channel:array]
Click here to test regex online

Splitting string into substring in Java

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.

Regex Pattern to avoid : and , in the strings

I have a string which comes from the DB.
the string is something like this:-
ABC:def,ghi:jkl,hfh:fhgh,ahf:jasg
In short String:String, and it repeats for large values.
I need to parse this string to get only the words without any : or , and store each word in ArrayList
I can do it using split function(twice) but I figured out that using regex I can do it one go and get the arraylist..
String strLine="category:hello,good:bye,wel:come";
Pattern titlePattern = Pattern.compile("[a-z]");
Matcher titleMatcher = titlePattern.matcher(strLine);
int i=0;
while(titleMatcher.find())
{
i=titleMatcher.start();
System.out.println(strLine.charAt(i));
}
However it is not giving me proper results..It ends up giving me index of match found and then I need to append it which is not so logical and efficient,.
Is there any way around..
String strLine="category:hello,good:bye,wel:come";
String a[] = strLine.split("[,:]");
for(String s :a)
System.out.println(s);
Use java StringTokenizer
Sample:
StringTokenizer st = new StringTokenizer(in, ":,");
while(st.hasMoreTokens())
System.out.println(st.nextToken());
Even if you can use a regular expression to parse the entire string at once, I think it would be less readable than splitting it with multiple steps.

how can i split a string

Hi I want to split a string as only two parts. i.e. I want to split this string only once.
EX: String-----> hai,Bye,Go,Run
I want to split the above string with comma(,) as two parts only
i.e
String1 ---> hai
String2 ---->Bye,Go,Run
Please help me how can I do it.
Use String.split(String regex, int limit) method:
String[] result = string.split(",", 2);
String[] result = string.split("\\s*,\\s*" ,2);
This is a very basic Java knowledge...
Have a look at String class definition before asking here:
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You should follow some Java tutorial before starting programming in Java.
if you check out the Java Doc of string
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html
You'll find one of the methods is
split(String regex)
then what you want is to use a regex like "," to get a table of strings
String str = "hai,Bye,Go,Run";
String str1 = str.substring(0, str.indexOf(','));
String str2 = str.substring(str.indexOf(',')+1);
You can use the String method:
public String[] split(String regex, int limit)
e.g. (not tested)
String str = "hai,Bye,Go,Run"
str.split(",", 2);
String str="hai,Bye,Go,Run";
//String 1
String str1=str.substring(0,str.indexOf(','));
//String 1
String str1=str.substring(str.indexOf(',')+1,str.length);
done :)

Categories