Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am new to java need help
the main string i have is
eg.
"String1/string2/string3/string4/all_free.sdx"
"file1/file2/string3/string4/all_free.sdx"
the end result i need is to be able to isolate and get string3
i can indexof but not able to achieve it in few steps need brainy people help as i am new to JAVA
If you know it's the third item that you want to get, one simple approach could be using split method, like this:
String myString = "String1/string2/string3/string4/all_free.sdx";
String string3 = myString.split("/")[2];
The call of split("/") produces an array of strings like this:
{"String1", "string2", "string3", "string4", "all_free.sdx"}
Now you can apply the subscript operator to grab the element that you want.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a Vast list of Strings in an ArrayList. I need to compare my input string with the entire list and return the Exact Match or the approximate match. How can I achieve this in Java ?
Compute the hamming edit distance between your search string and each string in your list. Pick the string or strings with the lowest edit distance. If you have to support strings that might need to add or remove characters to match the search string, use the Levenshtein distance..
try=
ArrayList.contains("StringToBeChecked");
If the String is present in the ArrayList, this function will return true, else will return false.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string array
String[] albumnames;
now how to take a string from particular index position with limited number of charachters.
For example,
if, albumnames[position] have value "abcdefghijk"
then i want to take the first 5 characters only.
That is "abcde".
The substring method of String can be used to achieve this. Try
String s = albumnames[position].substring(0,5);
See substring docs
albumnames[position].substring(0,5);
you can see the methods in String class, like substring, indexof
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
All I need to know is how to find the length of the string inside of the array. I also would like to know how to find which string comes first in alphabetical order in the array.
myArr[0].length()
This will access the string at location 0, then get the length of it.
Also, for sorting alphabetically, you can use
Arrays.sort(myArr);
You can simply use .length method on any string array element to get its length,
And to sort that array alphabetically you ca use .sort method -
For example -
String[] strings = { "Hello ", "This ", "is", "Sorting", "Example" };
Arrays.sort(strings);
So after sorting strings[0] would be Example and string[0].length() would be 7
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What I am looking to do is split a given string in Java using a tab delimiter. After the seventh split, I would like to keep everything after that point as an eighth split. This last split will contain tabs also, but must remain intact.
Use the split method that takes two parameters, the second being the split limit. You can pass in a limit of 8. The eighth element will contain the rest of the string after the seventh split.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file which contains the follwing
{"sno":"1c8d1d7eaa32ff3f58a8822111276d5a","at":"app","tt":{"AppParam1":"AppParamValue1","AppParam2":"AppParamValue2"}},"c":{"dt":"Microsoft XDeviceEmulator","pn":"WP","pv":"8.0.9832.0"}
In java I want to find 'sno', 'pn' and 'pv' and replace the values of (what I mean is) currently the above text file has
"sno" has value "1c8d1d7eaa32ff3f58a8822111276d5a"
"pn" has value "WP"
"pv" has value "8.0.9832.0"
New values
"sno" has to be changed to "637829"
"pn" has to be changed to "XYZ"
"pv" has to be changed to "2.2.4.0"
Your help is much appreciated !
Thanks
That's a JSON object.
Convert that string to a JSON object, change the values and then convert it again to a string.
Here you can read about JSON objects in java: http://www.json.org/java/
And here an easy tuto on how to work with them: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/
You can convert it into JSON, make you changes and then convert it back to string.
Or if you really want to replace these values by yourself, you can use these lines :
yourstring = yourstring.replaceFirst("\"sno\":\"[^\"]+\"", "\"sno\":\"637829\"");
yourstring = yourstring.replaceFirst("\"pn\":\"[^\"]+\"", "\"pn\":\"XYZ\"");
yourstring = yourstring.replaceFirst("\"pv\":\"[^\"]+\"", "\"pv\":\"2.2.4.0\"");
Only if there are no way theses key can be found elsewhere in the string