Related
I have a string, ie 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17. How do I get each value and convert it into an array? [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]. I can't find any suggestions about this method. Can help? I did try using regex, but it just simply remove ',' and make the string into one long sentence with indistinguishable value. Is it ideal to get value before and after ',' with regex and put it into []?
You could use following solution
String dummy = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] dummyArr = dummy.split(",");
Try this to convert string to an array of Integer.
String baseString = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] baseArray = baseString.split(",");
int[] myArray = new int[baseArray.length];
for(int i = 0; i < baseArray.length; i++) {
myArray[i] = Integer.parseInt(baseArray[i]);
}
Java provides method Split with regex argument to manipulate strings.
Follow this example:
String strNumbers= "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] strNumbersArr= strNumbers.split(",");
You can convert an array of string in array of integer with Streams
int[] numbersArr = Arrays.stream(strNumbersArr).mapToInt(Integer::parseInt).toArray();
Use String.split() and you will get your desired array.
String s1="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] mumbers=s1.split(","); //splits the string based on comma
for(String ss:numbers){
System.out.println(ss);
}
See the working Example
String csv = "Apple, Google, Samsung";
String[] elements = csv.split(",");
List<String> fixedLenghtList = Arrays.asList(elements);
ArrayList<String> listOfString = new ArrayList<String>(fixedLenghtList);
//ouput
[Apple, Google, Samsung]
if you want an int array
String s = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17";
String[] split = s.split(",");
int[] result = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
I wish to get the specific element named Something in a string within a String[] for JAVA.
My code =
String sentence = "AP=Something+example|AS=Explanation";
String[] word = sentence.split("\\|");
for (String w: word){
System.out.println(w);
}
My current output:
AP=Something+example
AS=Explanation
My expected output should be:
Something // the other information I dont want to take. Is there a better and faster way and not too time consuming?
Thank you in advance
If you want to split on plus not pipe then change
String[] word = sentence.split("\\|");
to
String[] word = sentence.split("\\+");
I don't understand what you really want, but:
String sentence = "AP=Something+example|AS=Explanation";
String[] recs = sentence.split("\\|");
HashMap<String, String> h = new HashMap<String,String>();
for (String r : recs) {
String[] vals = r.split("=");
h.put(vals[0], vals[1]);
}
System.out.println(h.get("AP").split("\\+")[0]);
This code is so bad, but look at common idea.
My english so bad too.
I am new to java . I have this String.
str="plane,cat,red,dogy";
I want to make a loop and send the data . the below is wrong but i want something similar to it.
for ( int i = 0; i>str.length; i++)
{
str=split.string(,);
// i know it wrong but I want to get the result before comma, for example first loop plane, second loop cat third loop red and so on
updatestatement(str);
}
This should be what you are looking for:
String str="plane,cat,red,dogy";
for(String subString: str.split(",")){
updatestatement(subString);
}
String[] words= str.split(",");
for (String w : words){
//Do whatever you want with each word
}
Don't use for to split. Just:
String[] parts = str.split(",");
It is very unclear what you need. But i think you are looking for this:
String[] s = str.split(",");
for ( int i = 0; i<s.length; i++)
{
// i know it wrong but I want to get the result before comma, for example first loop plane, second loop cat third loop red and so on
updatestatement(str);
}
Where updatestatement is a method in your class
The answer is simple.
String str="plane,cat,red,dogy";
String[] items = str.split(",");
System.out.println("No of items::"+items.length);
If you want to print each item,
for (String eachItem : items) {
System.out.println(eachItem);
//updateStatement(eachItem);
}
You should do it as follows :
String str="plane,cat,red,dogy";
String[]str1=str.split(",");
for ( int i = 0; i>str1.length; i++)
{
updatestatement(str1[i]);
}
String str="plane,cat,red,dogy";
String[] parts = str.split(",");
Arrays.stream(parts).forEach(System.out::println);
This solution only works with Java 8 because of the stream method. If you remove the last line it also works with other Java versions.
I have a String :
str="[a],[b],[c]";
How can I convert str to array in Java (Android):
array[0] -> a
array[1] -> b
array[2] -> c
EDIT:
and what about multidimensinal array? str="[["a1","a2","a3"],["b1","b2","b3"]]";
try
String str="[a],[b],[c]";
str= str.replaceAll("\\]|\\[", "");
String[] arr= str.split(",");
===========================================
update
converting multi dimension array to single dimension is already answered in SO please check change multidimensional array to single array
just copied the solution
public static String[] flatten(String[][] data) {
List<String> toReturn = new ArrayList<String>();
for (String[] sublist : Arrays.asList(data)) {
for (String elem : sublist) {
toReturn.add(elem);
}
}
return toReturn.toArray(new String[0]);
}
You can use following way.
String Vstr = "[a],[b],[c]";
String[] array = Vstr.replaceAll("\\]|\\[", "").split(",");
You would need to process your string and build your array. You could either take a look at .split(String regex) (which might require you to do some more processing to clean the string) or else, use a regular expression and do as follows:
Use a regex like so: \[([^]]+?)\]. This will seek out characters in between square brackets and put them into a group.
Use the .find() method available from the Matcher class and iterate over the matches. Put everything into a list so that you can put in as many hits as you need.
If you really need the result to be in an array, use the .toArray() method.
Take a look at String.split() method
An alternative to the regex and what npinti, i think, is talking about:
String myStrg = "[a],[b],[c]";
int numCommas = 0;
for( int i = 0; i < myStrg.length(); i++ )
{
// Count commas
if( myStrg.charAt(i) == ',' )
{
numCommas++;
}
}
// Initialize array
myArry = new String[numCommas];
myArry = myStrg.split(",");
// Loop through and print contents of array
for( String arryStrg: myArry )
{
System.out.println( arryStrg );
}
Try this code.
String str="[a],[b],[c]";
str= str.replaceAll("\\]|\\[", "");
String[] arr= str.split(",");
I have got a Java String as follows:
C|51199120|36937872|14261248|0.73|I|102398308|6240560|96157748|0.07|J|90598564|1920184|8867 8380|0.0
I want split this using regex as String arrays:
Array1 = C,51199120,36937872,14261248,0.73
Array2 =I,102398308,6240560,96157748,0.07
Array3 =J,90598564,1920184,88678380,0.03
Can Anybody help with Java code?
I don't think it's that simple. You have two things you need to do:
Break up the input string when you encounter a letter
Break up each substring by the pipe
I'm no regex expert, but I don't think it can be a single pattern. You need two and a loop over the substrings.
You can easily split your string on subcomponents using String.split("\\|"), but regexes won't help you to group them up in different arrays, nor will it help you to convert substrings to appropriate type. You'll need a separate logic for that.
Use String.split() method.
String []ar=str.split("(?=([A-Z]))");
for(String s:ar)
System.out.println(s.replace("|",","));
Simpler to just split then loop.
More or less:
String input = ...
String[] splitted = input.split("|");
List<String[]> resultArrays = new ArrayList<String[]>();
String[] currentArray = null;
for (int i = 0; i < splitted.length; i++) {
if (i % 5 == 0) {
currentArray = new String[5];
resultArrays.put(currentArray);
}
currentArray[i%5] = splitted[i];
}