I'm getting from JSON string like this:
{
"name":"Google"
"items": "item1,item2,item3,item4"
}
So I want to create array String[] items and populate it with items from this string. I need somehow to cut this string in parts. Also I'm getting different number of items, not only 4, like in example.
How can I do this....?
String[] items = incomingString.split(",");
Look at String.split() you can pass in a char that will split up your string and return an array
are you looking for the below line or I misunderstood your question ?
String[] result = yourString.split(",");
Related
I have been trying to take the array input by separating the values with '#'.Can anyone of you help me to get through this?
I have used the split function but it is not working. Is there any possibility of solving this problem?
Take your and start appending it to String then:
StringBuffer str;
//loop it
str.append(arr[i])
String[] arrOfStr = str.split("#");
After that convert to Integer and use
In an app I have used 7 checkboxes to get the days name from user and I am storing those name in an array based on selected checkboxes and then converting them to String data and store in database now I want to get the data back as an array only.
for eg:
days={"Mon","Tue","Wed","Sat"}
And now the database column has this value as: [Mon,Tue,Wed,Sat] which is stored as a String.
I can get this string back but how to convert it back to array so that i can compare the array data with current day like if today is Mon so i can find out which column has Mon.
Please help me out as I don't know what to search any related link or post or any suggestions how to achieve this. Thanks.
You can do it like this. Though, your values should not have "," in them.
String[] strs = new String[] { "Foo", "Bar", "Baz" };
String joined = Arrays.toString( strs );
String joinedMinusBrackets = joined.substring( 1, joined.length() - 1);
// String.split()
String[] resplit = joinedMinusBrackets.split( ", ");
you can refer this post
or you convert the resultset into an array using getArray() method, javadoc
Currently, I have trouble attempting to print out the individual lengths efficiently.
String[] array = {"test", "testing", "tests"};
int arraylength = array[0].length();
System.out.println(arraylength);
Now, this does work in printing out the length however, it is inefficient and doesn't work if theoretically I don't know the length of the array.
Thanks for your input and I would appreciate if the code insisted contains "System.out.println" included so I don't have trouble figuring out which to print out.
Use this:
String[] array = {"test", "testing", "tests"};
for(String str : array) {
System.out.println(str.length());
}
If you are using Java 8 then it's a one liner :)
Arrays.asList(array).forEach(element -> System.out.println(element.length()));
What you are doing is, converting your array to a list and then running a for loop over it. Then for every element, you are printing out the length of the element.
EDIT
From one of the comment, this is even a better version of my code.
Arrays.stream(array).map(String::length).forEach(System.out::println);
Here first you convert your array to a list and then map each element to the function length of string class, then you run a foreach over it which prints out the mapped values.
String[] array = {"test", "testing", "tests"};
The length for array is:
int arraylength = array.length;
To have retrieve length for string:
for(String string: array) {
System.out.println(string.length());
}
I am using the split() function to fill an array and I am trying to enter each element into a Mysql table. What is the best way to do this? How can I make the loop iterate until it reaches the end of the array? I was thinking I might need a scanner object that reads from the array which would allow me to use the .hasNext() function. Is there a better way than this?
String text = "This is my example text string";
String array[] = text.split(" ");
Thanks
First of all, text.split(" ") returns String[] not String, but you can loop through the array like this:
String text = "This is my example text string";
String[] array = text.split(" ");
for (String item : array) {
// do something with each item
// like build an SQL statement
}
e.g.:
If the number is 234, I would like the result to be List<String> containing 2,3,4 (3 elements)
If the number is 8763, I would like the result to be List<String> containing 8,7,6,3 (4 elements)
Does commons-math already have such a function?
Convert the number to a String (123 becomes "123"). Use Integer.toString.
Convert the string to a char array ("123" becomes {'1', '2', '3'}). Use String.toCharArray.
Construct a new, empty Vector<String> (or some other List type).
Convert each char in the char array to a String and push it onto the Vector ({'1', '2', '3'} becomes a Vector with "1", "2" and "3"). Use a for loop, Character.toString and List.add.
Edit: You can't use the Vector constructor; have to do it manually.
int num = 123;
char[] chars = Integer.toString(num).toCharArray();
List<String> parts = new Vector<String>();
for (char c : chars)
{
parts.add(Character.toString(c));
}
There isn't an easier way to do this because it really isn't a very obvious or common thing to want to do. For one thing, why do you need a List of Strings? Can you just have a list of Characters? That would eliminate step 3. Secondly, does it have to be a List or can it just be an array? That would eliminate step 4.
You can use the built in java.util.Arrays.asList:
int num = 234;
List<String> parts = Arrays.asList(String.valueOf(num).split("\\B"));
Step by step this:
Converts num to a String using String.valueOf(num)
Splits the String by non-word boundaries, in this case, every letter boundary except the start and the finish, using .split("\\B") (this returns a String[])
Converts the String[] to a List<String> using Arrays.asList(T...)
Arrays.asList( String.valueOf(number).toCharArray() )
Try this:
Arrays.asList(String.valueOf(1234).split("(?!^)"))
It will create list of Strings:
["1", "2", "3", "4"]
This seems like homework.
Consider using % and / to get each digit instead of converting the entire number to a String