This question already has answers here:
Reverse (parse the output) of Arrays.toString(int[]) [duplicate]
(3 answers)
Closed 6 years ago.
Let's say I've got a string with three strings looking like this:
String s = "[object1,object2,object3]";
How do I convert this to a String array which looks like this:
String[] ary = {"object1", "object2", "object3"};
Also mind that each string item (object1, object2 and object3) may also contain additional comma (,) characters.
just use the split method String[] str = s.split(",");
if you want the "[ ]" out use the string.replace method to replace "[]" with ""
I assume you want don't want the outer [], you could use replace/split or substring/split
String myArray[] = s.replace("[","").replace("]","").split(",");
Or
String myArray[] = s.substring(1, s.length() -1 ).split(",");
See the code below with comments to explain what each does.
s = s.replace("[","").replace("]},""); //remove the brackets
String[] ary = s.split(","); //split the string around commas
To do the stuff in your situation you can do
String[] ary = s.substring(1, s.length() -1).split(",");
If there is no "[" and "]" just remove the substring call
Related
This question already has answers here:
How can I remove a substring from a given String?
(14 answers)
Closed 2 years ago.
String string = "yesnoyesnoyesnoyesno";
String substring = "no";
How do I remove every occurrence of the substring from the string so it ends up being "yesyesyesyes"?
If you are using Java 8+, then you could try splitting the input on "no" and then joining the resulting array together into a string:
String string = "yesnoyesnoyesnoyesno";
string = String.join("", string.split("no"));
System.out.println(string);
This prints:
yesyesyesyes
This doesn't use the replace(), so technically it meets the brief:
String str = string.replaceAll("no", "");
String input = "yesnoyesnoyesno";
String output="";
for(String token : input.split("no") )
output = output.concat(token);
System.out.println(output);
It prints:
yesyesyesyes
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 5 years ago.
Why will this code not print out a string?
String s = "My name is Jack";
String[] arr = s.split("");
char[] a = Arrays.toString(arr).toCharArray();
System.out.println(a);
System.out.println(new String(a));
You didn't make a char[] array, but rather a String[] array. Use String#toCharArray() instead:
String s = "My name is Jack";
char[] letters = s.toCharArray();
System.out.println(Arrays.toString(letters));
System.out.println(new String(letters));
Demo
It does print out a String, at least on the second println (the first println yields exactly the same output, but it does it without constructing the String).
Ideone demo
However, the chars of the string you are printing is obtained via:
char[] a = Arrays.toString(arr).toCharArray();
Arrays.toString gives you a string surrounded by [], separated with commas. So "hello" would look like:
[h, e, l, l, o]
You then get the chars of this string, and try to reconstitute it into a string (which is redundant anyway, just print Arrays.toString(arr)).
To print the joined string, use String.join:
String.join("", arr)
This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
I need to be able to split a single word string like "x.y" into an array of size 2 where the "." occurs, so the array would look like ["x", "y"]. What I've tried is:
String word = "hello.world";
String[] split = word.split(".")
return split.length == 2;
but this seems to just return an empty array (false). How would I go about doing this? Thanks.
Repalce this
String[] split = word.split(".")
with
String[] split = word.split("\\.")
. (dot) has a special meaning in regex so you need to escape it if you want to use it as a literal to split.
This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 7 years ago.
Say I have a string a such:
String str = "Kellogs Conflakes_$1.20";
How do I get the preceding values before the dollar ($) sign.
N.B: The prices could be varied say $1200.
You can return the substring using substring and the index of the $ character.
str = str.substring(0, str.indexOf('$'));
You could use String.split(String s) which creates a String[].
String str = "Kellogs Conflakes_$1.20"; //Kellogs Conflakes_$1.20
String beforeDollarSign = String.split("$").get(0); //Kellogs Conflakes_
This will split the String str into a String[], and then gets the first element of that array.
Just do this for split str.split("$") and store it on an array of String.
String[] split = str.split("$");
And then get the first position of the array to get the values that you have before the $
System.out.println(split[0]); //Kellogs Conflakes_
At the position 1 you will have the rest of the line:
System.out.println(split[1]); //1.20
Try this:
public static void main(String[] args)
{
String str = "Kellogs Conflakes_$1.20";
String[] abc=str.split("\ \$");
for(String i:abc)
{
System.out.println(i);
}
}
after this you can easily get abc[0]
This question already has an answer here:
Divide/split a string on quotation marks
(1 answer)
Closed 8 years ago.
This question is Pretty Simple
How to Split String With double quotes in java?,
For example I am having string Do this at "2014-09-16 05:40:00.0",After Splitting, I want String like
Do this at
2014-09-16 05:40:00.0,
Any help how to achieve this?
This way you can escape inner double quotes.
String str = "Do this at \"2014-09-16 05:40:00.0\"";
String []splitterString=str.split("\"");
for (String s : splitterString) {
System.out.println(s);
}
Output
Do this at
2014-09-16 05:40:00.0
Use method String.split()
It returns an array of String, splitted by the character you specified.
public static void main(String[] args) {
String test = "Do this at \"2014-09-16 05:40:00.0\"";
String parts[] = test.split("\"");
String part0 = parts[0];
String part1 = parts[1];
System.out.println(part0);
System.out.println(part1);
}
output
Do this at
2014-09-16 05:40:00.0
Try this code. Maybe it can help
String str = "\"2014-09-16 05:40:00.0\"";
String[] splitted = str.split("\"");
System.out.println(splitted[1]);
The solutions provided thus far simply split the string based on any occurrence of double-quotes in the string. I offer a more advanced regex-based solution that splits only on the first double-quote that precedes a string of characters contained in double quotes:
String[] splitStrings =
"Do this at \"2014-09-16 05:40:00.0\"".split("(?=\"[^\"].*\")");
After this call, split[0] contains "Do this at " and split[1] contains "\"2014-09-16 05:40:00.0\"". I know you don't want the quotes around the second string, but they're easy to remove using substring.