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.
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 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.
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
I want to display the first folder name from the path.
/mnt/sdcard/Videos/lk.jpeg, I want to display mnt string. in java
/mnt/sdcard/Videos/lk.jpeg--> **mnt**
You can split on / and use [1] element from result array.
You can either use regular expressions or you can use String.split(). Note that the split() result should be checked for live usage (e.g. if it has at least two elements).
String desired = "/mnt/sdcard/foo".split("/")[1];
String str = "/mnt/sdcard/Videos/lk.jpeg";
System.out.println(str.split("/")[1]);
Try this out. This is a poor question. But maybe the asker can be a newbie.
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 9 years ago.
Improve this question
I have a string
1,11,cda74944-1c61-4325-8137-83a4ab50cb81-A00123,Bs000000216,20140204185143.811
Can you provide me regular expression or java string function to get the sub string:
cda74944-1c61-4325-8137-83a4ab50cb81-A00123 ?
You split the string:
String[] array = String.split(",");
This will return an Array of String like this
["1", "11", "cda74944-1c61-4325-8137-83a4ab50cb81-A00123", "Bs000000216", "20140204185143.811"]
You can access the part you want in array[2].
If there is a string like this, use pattern:
/\w{8}-\w{4}-\w{4}-\w{4}-\w{12}-\w{6}/
The pattern is:
[a-f0-9]++(?>-[a-z0-9]++)+
You can split the String
String[] allStrings= String.split(",");
then try to get allStrings[2] //it will give you the desired result cda74944-1c61-4325-8137-83a4ab50cb81-A00123
A few options:
Using length:
String s = yourString.subString(0, yourString.length() >>> 1);
Using split:
String s = yourString.split(",")[2];
Alternatively, if you have something representing physical data that is always represented similarly, it might be smarter to wrap it in a class instead.
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 9 years ago.
Improve this question
I have code which contains XML which is parsed and treats all values as strings. Some of these strings are metric values with currency or % or number formatting. How can I obtain this formatting from the string and then reapply it later. The formatting is removed when numbers are cast to doubles.
<p><val>$4500.30</val><val>45.0%</val></p>
//parse out
String metric1 = "$4500.30";
String metric2 = "45.0%";
//remove special char
metric1 = metric1.replaceAll("[^A-Za-z0-9.]", "");
//I need to reapply formatting after it is removed(such as $)
You can also use a predefined instances of NumberFormats to be able to parse and format values every time you need. It's better then to have values both as plain & formatted values.
I realized this was a poor approach. Since the values were stored as string I just kept the formatting and used another copy of the string later which was converted to double.