Convert string containing an array to array object - java

Hello I have a string containing an array! I want to be able to construct this into an array but I cannot find any methods for doing so! Can someone help me, this is what my string looks like
[111111,111111,111111,111111,111111,111111,111111]

Just take out the square brackets then use the string split method, giving ',' as a delimiter.
String str = "[111111,111111,111111,111111,111111,111111,111111]"
//remove the brackets
//as backslash mentioned, str.substring is a better approach than using str.replaceAll with regex
str = str.substring(1, str.length()-1);
//split the string into an array
String[] strArray = str.split(",");

Related

Any alternative to String[] when splitting a String?

In making a scanner, I want to split apart the input twice:
First remove any spaces with String.split("\\s+");
Then split the remaining String into chars with String.split("(?!^)");
After removing the spaces, I can't seem to figure out how to make a String that holds the entirely new Array of parts of my String.
With this, I tried String = String.split(), and that didn't work.
Google didn't help either.
You seem to be overcomplicating this, why not something as simple as:
// remove spaces
String a = "abc".replace(" ", "");
// to array of chars
char[] chars = a.toCharArray();

How to split string separated by | character

I have input string in the following format
first|second|third|<forth>|<fifth>|$sixth I want to split this string into an array of string with value [first,second,third,,,$sixth]. I am using following code to split the string but that is not working. please help me.
public String[] splitString(String input){
String[] resultArray = input.split("|")
return resultArray;
}
Could you please tell me what am I doing wrong.
You need to escape | using backslash as it is a special character. This should work:
String[] resultArray = input.split("\\|")
| is a meta character meaning it represents something else in regex. Considering split takes regex as an argument, it interprets the argument using regex. You need to "escape" all of the meta characters by placing a \\ before it. In your case, you would do:
String[] resultArray = input.split("\\|");

Java split string with "||" taking it as regex instead of string

I have the following string "ABC" and "AAA||BBB"
I am trying to split it using the characters "||" but the split method is taking this as a regex expression, returning an array of characters instead of {"ABC"} and {"AAA", "BBB"}
I have tried scaping the bar with a back slash, but that didn't work.
How can I make the split method to take "||" as a String and not as a regex?
Thanks
Escape the pipes
Use \\|\\| instead
If you don't want to deal with escaping then you can use Pattern#quote:
String[] tok = "AAA||BBB".split(Pattern.quote("||"));
OR simple:
String[] tok = "AAA||BBB".split("\\Q||\\E"));
String[] result = "The||man is very happy.".split("\\|\\|");
for (int x=0; x<result.length; x++){
System.out.print(result[x]);
}
There you go its simple

StringTokenizer delimiters for each Character

I've got a string that I'm supposed to use StringTokenizer on for a course. I've got my plan on how to implement the project, but I cannot find any reference as to how I will make the delimiter each character.
Basically, a String such as "Hippo Campus is a party place" I need to divide into tokens for each character and then compare them to a set of values and swap out a particular one with another. I know how to do everything else, but what the delimiter would be for separating each character?
If you really want to use StringTokenizer you could use like below
String myStr = "Hippo Campus is a party place".replaceAll("", " ");
StringTokenizer tokens = new StringTokenizer(myStr," ");
Or even you can use split for this. And your result will be String array with each character.
String myStr = "Hippo Campus is a party place";
String [] chars = myStr.split("");
for(String str:chars ){
System.out.println(str);
}
Convert the String to an array. There is no delimiter for separating every single character, and it wouldnt make sense to use string tokenizer to do that even if there was.
You can do something like:
char[] individualChars = someString.toCharArray;
Then iterate through that array like so:
for (char c : individualChars){
//do something with the chars.
}
You can do some thing like make the string in to a Char array.
char[] simpleArray = sampleString.toCharArray();
This will split the String to a set of characters. So you can do the operations which you have stated above.

using regular expression as delimiter with StringTokenizer

I am new to java progrmming and came across the StringTokenizer class. The constructor accepts the string to be split and another optional delimiter string each character of which gets treated as an individual delimiter while splitting the original string. I was wondering if there is any way to split the string passing a regex as the delimiter. for example:
String s="34.5xy32.6y45.7x36xy"
StringTokenizer t=new StringTokenizer(s,"xy");
System.out.println(t.nextToken());
System.out.println(t.nextToken());
The actual output is:
34.5
32.6
However, the desired output is:
34.5
32.6y45.7x36
Hope you guys can help. Also, please suggest some way around if it is not possible with StringTokenizer class.
Thanks in advance.
p.s. Is there any way to know which character the StringTokenizer is currently using as delimiter out of the provided set?
Here you would want to use String.split(), this will give you an array with your desired output.
It will take your input and split it around exact matches of your string you provide. StringTokenizer will split around anyone of the set that you provide it rather than a regular expression.
So you change your code to:
String s="34.5xy32.6y45.7x36xy";
String[] splitString = s.split("xy");
System.out.println(splitString [0]);
System.out.println(splitString [1]);
For more complex examples you probably want boundary checking on the array also to make you don't go off the end of the array
Try with this.
String s="34.5xy32.6y45.7x36xy";
final String SPLIT_STR = "xy";
final String mainStr = "34.5xy32.6y45.7x36xy";
final String[] splitStr = mainStr.split(SPLIT_STR);
System.out.println("First Index Of xy : " +
mainStr.indexOf(SPLIT_STR));
for(int index=0; index < splitStr.length; index++) {
System.out.println("Split : " + splitStr[index]);
}

Categories

Resources