Android string manipulation [closed] - java

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
For this string, 16.82080560, 96.13055810 I want to get
"String one = 16.82080560"
and
"String two = 96.13055810"
in android.
Admin that I suck in string manipulation and regex.
Please let me know how can I get such two value from a string.

String[] components = original.split(",");
If the Strings are always separated by a comma you can use String.split
For a better regex pattern see the comment from #npinti:
Minor side note, it might be better to do \\s*,\\s* instead of just ,.
Just , might cause problems should the OP wish to cast these to
floats, since the extra white space at the beginning of the second
number will most likely not be recognized as a proper number.

Another option:
String str = "16.82080560, 96.13055810";
StringTokenizer st = new StringTokenizer(str,", ");
String one = st.nextToken();
String two = st.nextToken();

Related

regex to detect file structure in java [closed]

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 want to detect the file structure in a string.
e.g
if I have a string as /name/test/testme/2 I should be able to store it in a arraylist as different elements like {[name],[test],[testme],[2]}
String[] elements = "/name/test/testme/2".split("/");
More info can be found in the String.split() Javadoc
As Lukas pointed out, (please give him some upvoting) you should use the split method.
String[] elements = "/name/test/testme/2".split("/");
The regular expressions are not used to split strings in sections. Regular expressions are used for matching the target string with a specific generic format. In this case a boolean value indicating if the strings match is returned.
Hope I helped!

Java. Splitting a multiple word string into two word strings every space [closed]

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
The first round of this was answered here last night. The solution that #Reimeus gave was great and gives the below output from:
My name is the mighty llama
To:
String[] stringArray = string.split("(?<!\\G\\S+)\\s");
My name
is the
mighty llama
However, I now find myself need to correct what I'm doing. I actually need to be able to split the string like so.
My name
name is
is the
the mighty
mighty llama
You can split it into single words and build your required array in the code.
String string = "My name is the mighty llama";
String[] stringArray = string.split(" ");
String[] outputArray = new String[stringArray.length - 1];
for (int i = 0; i < stringArray.length - 1; i++) {
outputArray[i] = stringArray[i] + " " + stringArray[i+1];
}
you cannot duplicate a token with String.split(), because regex is used to SEARCH.
you cannot do this without some kind of edit, maybe a String.replaceAll() will accomplish.

How to replace string with another string by index? [closed]

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 like this- 11/15/2013.
I want to replace 2013 with 2000 (last 2 digits with 0). how to do this?
You can always do replace date.replace("13", "00");
but if you are looking at something generic of a solution (In case if you are not aware of what the last 2 digits are, yet you want them to be '00') you can do something like this :
String date = "11/15/2013";
String newDate = date.substring(0,8)+"00";
Or you can use a StringBuilder:
StringBuilder date = new StringBuilder("11/15/2013");
date.setCharAt(8, '0');
date.setCharAt(9, '0');
or
StringBuilder date = new StringBuilder("11/15/2013");
date.replace(8, 10, "00");
code snippet
String date = "11/15/2013";
String replaced = date.replace("2013","2000");
If you really want to replace a sequence at a certain index of a String, use a StringBuilder:
String replaced = new StringBuilder(date)
.replace(8, 10, "00").toString();
String date = "11/15/2013";
System.out.println(date.substring(0, date.length()-2).concat("00"));
Is this what you are looking for?
Better code snippet
String replaced = date.replace("11/15/2013.","11/15/2000");

Remove the first letter from a string in Java [closed]

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
Given a string "Hello". I only want all the letters after the first letter to be in my new string.
E.g.: "ello"
How could this be done?
Tried pattern matching but cant get it to work :(
Try this:
String s = "Hello";
String newS = s.substring(1); // newS is "ello"
The above will create a new string containing all the characters of the original, except the first one. See the documentation for more details.

How to get a substring from a string [closed]

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 this xml file from where I'm reading this string,
http://localhost:8080/sdpapi/request/10/notes/611/
My question is how can I get just the 611, which is of variable, can be 100000, for example, from this string?
Split the string
String input = "http://localhost:8080/sdpapi/request/10/notes/611/";
String output = input.split("notes/")[1].split("/")[0];
output is the value you need
What language?
Anyway, in most cases it's a syntax like:
String.substring(begin, length);
... where 'begin' is the number of the letter in the string-1. For extracting http from the above string you would write
substring(0, 4);
In case you always need the last string between the last two '/'s, you can retrieve the position of the slashes with index-functions (as stated in the answer of #Liran for example).
// EDIT: In Java the second parameter of substring is not length, but endIndex:
String s = "http://localhost:8080/sdpapi/request/10/notes/611/";
s.substring(46, s.lastIndexOf('/'));
It depends on programming language you use, but Regular Expressions should be the same in most of them:
/(\d+)\/$/
well, it depend in what language are you writing... in c# for example
string s = #"http://localhost:8080/sdpapi/request/10/notes/611/";
s.SubString(s.LastIndexOf('/'));
or
Path.GetFileName(s);
for java
new File(s).getName();

Categories

Resources