I have this string and i want to convert in into the Array or ArrayList.
[OM, Sagar,Ravi, Raj]
I want to convert it on to string array or Array list. How do i do that?
I have this string and i want to convert in into the Array or ArrayList.
[OM, Sagar,Ravi, Raj]
I want to convert it on to string array or Array list. How do i do that?
Try,
String str="[OM, Sagar,Ravi, Raj]";
String[] arr=str.substring(1, str.length()-1).split(",");
or,
List<String> arrayList=new ArrayList<>(Arrays.asList(arr));
Note: Use jdk 1.7 or greater for diamond operator <>
Just use the appropriate method: String#split().
String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556
(note that this takes a regular expression, so remember to escape special characters if necessary, e.g. if you want to split on period which means "any character" in regex, use split("\\."))
To test beforehand if the string contains a -, just use String#contains().
if (string.contains("-")) {
// Split it.
} else {
throw new IllegalArgumentException("String " + string + " does not contain -");
}
Your question is very unclear as to how do you want to convert your String into Array or other Datastructures. What elements do you want to treat as individual String elements that insert into Array or the entire String gets treated as a single element in Array like:-
String s="[OM, Sagar,Ravi, Raj]"
String[] arr=new String[1];
arr[0]=s;
List arrList=Arrays.asList(arr);
Assuming you are treating each element individually:-
String s="[OM, Sagar,Ravi, Raj]"
String[] arr=s.substring(1, str.length()-1).split(",");
List arrList=Arrays.asList(arr);