Hi I have formatted a string using the below code
mForecastStr = String.format("%s - %s - %s/%s",dateView.getText(),
forecastView.getText(), highView.getText(),
lowView.getText());
Now I want to separate the values from mForecastStr
Like the value before the first hyphen, then the second hyphen and the n the value after the third hyphen.
How can I do that?
To get an array of string with the split values:
String[] arrForecast = mForecastStr.split(" - ");
Now arrForecast[0] will contain the first value, arrForecast[1] the second one and arrForecast[2] the third one.
Related
I have been trying to replace specific part of String(array of space characters) with another String.
for Example
String string1 = " ";
String string2 = "hello";
I want to replace the first 5 characters in string1 with "hello".
Can anyone help me?
if I print the length of the string1 it should print the original length string1 not the length of string2.
I don't want trimmed version.
Here is an example that works to solve your problem:
String one = " ";
String two = "Hello";
char[] strArray = one.toCharArray();
for(int x = 0; x < 5; x++) {
strArray[x] = two.charAt(x);
}
one = String.valueOf(strArray);
System.out.print(one);
In this example we create an empty String with 5 character spaces like you have in your question. We then create a String that contains the word "Hello". We can create a CharArray[] that will store all of the elements in String one. Since we want to change the first 5 indexes, we will run a loop that iterates 5 times. until the first 5 indexes are changed. On each iteration, we will set the value of that character index in the array to the value of the character index in String two. When the loop is done we set one = to the String value of the Char Array. Here is your output:
Hello
This solution works along the lines of what you were looking for. For example, lets say you alter the code and String one looks like this:
String one = " theDogCrossedTheRoad";
System.out.println("The length is: " + one.length());
The output would be a length of 25 for String one. And then lets test the length of String one after the code executes:
System.out.println("The length is: " + one.length());
The output would still be 25, so the code doesn't trim the length of your original String. It simply replaces the first 5 characters. And here is your output:
The length is: 25
HellotheDogCrossedTheRoad
The length is: 25
As you can see the rest of you String is still intact, and the only thing that changed are the first 5 characters. You still have the same character length, so your code will not be Trimmed.
I have a string with , separated I want to read till 4th index then remaining string I want to consider as one string.
Like in below
String str = "abc,xyz,123,789,ijk,1232,123,123,STU,PQR,111";
I want to split and take string after ijk in one string and from abc to 789 each part in different string.
String::split can take a second parameter indicating how many groups to form, which in your case is 5:
String[] result = str.split(",", 5);
I'm trying to get the last three characters in a string. With the following code, I'm trying to get the last three characters of the fname variable, but I'm getting a "The method Length(int) is undefined for the type String" error:
String fname = request.getParameter("fname");
String lname = request.getParameter("lname");
String number = request.getParameter("number");
String firstPart = lname.substring(0, 3);
String middlePart = fname.substring(0, fname.Length(3));
So there are two problems here:
Firstly you're calling fname.Length(3), which doesn't make sense as String doesn't have a Length(int n) method on it. What it does have is a substring(int) method and a length() method, which you can use as follows:
String middlePart = fname.substring(fname.length() - 3);
As outlined in the linked JavaDocs, String.substring() "Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.". So if we can provide it with the index (or position) within the String fname where we want to start copying from.
If I've got a String such as "Chicken", and I want the last 3 characters, I'd call "Chicken".substring(4), and the result would be "ken" (Strings are zero-indexed, so the character 'k' has index 4).
Instead of hard coding the index where I want to start the substring from, I use the String.length() method which tells me how long a String is, and subtract 3. In the above example, "Chicken".length() is 7, and so "Chicken".length() - 3 is the index where you should start substring-ing if you want the last 3 characters.
String lastThreeChars = string.substring(string.length() - 3);
String s = "10.226.18.158:10.226.17.183:ABCD :AAAA"
My requirement is to split the string at up to 3rd : or up to 2nd :. i.e.
Something like String sa[] = s.split(), but with the regex splitting only up to 3rd or 2nd.
s[0] = "10.226.18.158"
s[1] = "10.226.17.183"
s[2] = "ABCD :AAAA"
According to the String#split() javadoc you can add a number to limit the number of splits.
s.split(":", 3);
Edit: as melwil metions This will return an array of up to the number passed in long.
So in your example of splitting up to 2nd : you would need to pass in 3.
s.split(":",3) returns the output
sa[0] = "10.226.18.158"
sa[1] = "10.226.17.183"
sa[2] = "ABCD :AAAA"
Relevent section quoted from the java doc about how the second argument (limit) works.
The limit parameter controls the number of times the pattern is
applied and therefore affects the length of the resulting array. If
the limit n is greater than zero then the pattern will be applied at
most n - 1 times, the array's length will be no greater than n, and
the array's last entry will contain all input beyond the last matched
delimiter. If n is non-positive then the pattern will be applied as
many times as possible and the array can have any length. If n is zero
then the pattern will be applied as many times as possible, the array
can have any length, and trailing empty strings will be discarded.
You can split your string basing on one non-whitespece character, \S{1}, followed by a colon, ::
String sa[] = s.split("\\S{1}:");
I have a string as follows:
2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576
I want to extract the number: 872226816, so in this case I assume after the second comma start reading the data and then the following comma end the reading of data.
Example output:
872226816
s = "2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576";
s.split(",")[2];
Javadoc for String.split()
If the number you want will always be after the 2nd comma, you can do something like so:
String str = "2012/02/01,13:27:20,872226816,-1174749184,2136678400,2138578944,-17809408,2147352576";
String[] line = str.split(",");
System.out.println(line[2]);