Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
I want to filter specific text from a specific array in java like
String a="code=500,code1=1000"
I want to get only 500 from index 0 by searching "code="
One approach would be to:
Split the String by "," to an array.
Now for each element of the array, do a replaceAll to remove the text before the number.
It might not be the most optimal, but this should work:
String[] split = a.split(",");
for (String s: split) {
String desiredText = s.replaceAll(".+=", "");
}
Then you could add the desiredText to a List or whatever it is you want to do with it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list such:
List<string> list =Arrays.asList("2014-06-20 [txt1:executed]","2014-06-21 [txt2:in progressed]");
I want to extract the contents to an array:
arr[0] =2014-06-20
arr[1] =executed
arr[2] =2014-06-21
arr[3] =in progressed
You might take a look at regular expressions. They are quite powerfull to split Strings according to your wishes.
Here is an example how you can split your String.
String regex = "([0-9-]+)\\s+\\[\\w*:([\\w\\s]*)]";
List<String> list = Arrays.asList("2014-06-20 [txt1:executed]","2014-06-21 [txt2:in progressed]");
String date = list.get(0).replaceAll(regex, "$1");
String status = list.get(0).replaceAll(regex, "$2");
I hope that helps.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to store values from text view for example i am performing the following plus function like 5+6.Now i want to store 5 as value-1 and 6 as value-2.I only have one text view where i am displying like 5+6 in my project.I search a lot about my issue but could not found any proper solution. thanks
try this:
String string =text.getText().toString();
String[] separated = string.split("+");
String firstValue=separated[0];
String secondValue=separated[1];
You can try this and you will able to get the addition of two numbers at once. .
String[] getText = textview.getText().split("\\+");
int total=0;
for(String s:getText){
total+=Integer.parseInt(s);
}
in the end of iteration total will give the final result.From this method it gives the total of any numbers of numbers.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hii friends I am beginner in java. I am getting string from InputStream in the format like:
aD1bD2cD3dD4eD5fD6g
where D1,D2,D3... are data and small letters a,b,c,d,e,f,g are identifier so I can identify data between a and b and c and so on.
Now the problem is that I am getting more than one data pattern with spaces in between. But I need extract only first data from it. For example, consider data received as
a-674b-26c96d-662e-39f93g a74b-2c96d66e-39f86g a-84b-96c96d562e-99f93g
then I need to extract only first data from this entire data string that is
a-674b-26c96d-662e-39f93g
Please help me.
Use this
String[] params = data.split(" ");
String firstString = params[0];
split() function will split your whole string using space as delimiter, resulting in array of strings. First element of the array is the string you are looking for.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a string of numbers which looks like this
["100000100685716-2","603834770-2", "604544970-3"]
can someone help me with a regular expression to match each long (first number before the "-") so i can add it to an array?
The following regex should work: (\d+)-
You do not need a regex here:
Use int pos = str.indexOf('-') to get the location of the dash
Use str.substring(0, pos) to get the initial portion of the string.
If dashes in the input strings are optional, you would need to add a check of pos to be non-negative.
(?:\"\d+)
This should do exactly what you want
String s = "[\"100000100685716-2\",\"603834770-2\", \"604544970-3\"]";
System.out.println(s.split("-")[0].substring(2));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
how to sort variables X and negated varibles X' alphabetic which are saved as string in Java?
Example: String string = "B*A'*D*H'"; result must
String sortedString="A'*B*D*H'";
are the variables always separated with * ?
In that case it'd be very simple:
String in = "B*A'*D*H'";
String[] vars = in.split("\\*");
Arrays.sort(vars);
// Java is missing a simple php-like join function ;-(
StringBuilder sorted = new StringBuilder();
for(String s: vars){
sorted.append(s);
sorted.append("*");
}
sorted.deleteCharAt(sorted.length()-1);
and you're done ;-)