I have a String Chocolate:30:2 in a variable and I want to extract the number after the second colon i.e. 2. So, How can I extract that number?
For example:
String s = "Chocolate:30:2";
String number = s.split(":")[2];
If the second colon is actually the last colon, you can use:
String after = str.substring(1 + str.lastIndexOf(':'));
You can use String lastIndexOf method.
String result = str.substring(str.lastIndexOf(':') + 1);
Related
I am having trouble with a recent Java project. I am attempting to just the String "white" from the String. No matter what method I attempt, the last "_" always remains.
String questionText = "The white house is _white_";
String correctResponse = questionText.replace(questionText.substring(0, questionText.indexOf("_")+1), "");
correctResponse.substring(0,correctResponse.length()-1);
System.out.println(correctResponse);
substring don't modify original object.
use
correctResponse = correctResponse.substring(0, correctResponse.length() - 1);
I would use a regular expression to group everything between underscores, and then String.replaceAll(String, String) to actually remove everything but the group. Like,
String correctResponse = questionText.replaceAll(".+\\s+_(.+)_", "$1"); // white
If the string you want is always between underscores (or at least after one underscore), you could just split the string and take the substring at index 1:
String correctResponse = questionText.split("_")[1];
Use lastIndexOf
String correctResponse = questionText.replace(questionText.substring(questionText.indexOf("_"), questionText.lastIndexOf("_")+1), "");
You think to complicated - why do you need replace? You can achieve the same with substring
First statement
String correctResponse = questionText.substring(questionText.indexOf("_")+1)
// ==> correctResponse = "white_"
Second statement
correctResponse = correctResponse.substring(0, correctResponse.indexOf("_"))
// ==> correctResponse = "white"
As #neuo pointed out, substring won't change the string..
You just need to change line 3.
Original Line :
correctResponse.substring(0,correctResponse.length()-1);
Correct Line :
correctResponse = correctResponse.substring(0,correctResponse.length()-1);
If you use a regular expression you don't have to check index bounaries.
String string = "Merry Christmas!".replaceAll(".$", "");
System.out.println(string);
will print out
Merry Christmas
I need to sub string the string after "2:" to the end of line as it is a changeable string:
Which mean in this example that I want to take the string "LOV_TYPE" from this 2 lines
ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE
ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 3: AUDIT_LEVEL
I tried to use subString(int startingIndex, int endingIndex) method, I can determine the first argument which is starting point.. but I can't determine the end point.
You can use two substrings, one that gets the String after 2:, and then one that gets the string before the next new line.
string = string.substring(string.indexOf("2:") + 2);
string = string.substring(0, string.indexOf("ObjMgrSqlLog));
If you need to get rid of the spaces on either end, you can then trim the string.
string = string.trim();
source:
String str = "ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";
You can use regex
String out1 = str.replaceAll("^.*?.\\:.*[ ]", "");
or classic index-of
int lastCh = str.lastIndexOf(":");
String out2 = str.substring(++lastCh).trim();
output:
System.out.println(out1);
System.out.println(out2);
If you use str.substring(startingIndex), you will have the substring to the end of the string. It seems to be what you want. If you have extra spaces at the end of the string, you can always use a str.trim() to remove the spaces.
Use substring along with .length() to get the value of the length of the string. For example:
String original = "ObjMgrSqlLog Detail 4 2014-03-26 13:19:58 Bind variable 2: LOV_TYPE";
String newString = original.substring (62, original.length ());
System.out.print (newString);
I have a string :
"id=40114662&mode=Edit&reminderId=44195234"
All i want from this string is the final number 44195234. I can't use :
String reminderIdFin = reminderId.substring(reminderId.lastIndexOf("reminderId=")+1);
as i cant have the = sign as the point it splits the string. Is there any other way ?
Try String.split(),
reminderIdFin.split("=")[3];
You can use indexOf() method to get where this part starts:
int index = reminderIdFin.indexOf("Id=") + 3;
the plus 3 will make it so that it jumps over these characters. Then you can use substring to pull out your wanted string:
String newString = reminderIdFin.substring(index);
Remove everything else and you'll be left with your target content:
String reminderIdFin = reminderId.replaceAll(".*=", "");
The regex matches everything up to the last = (the .* is "greedy").
I am trying to parse the certain name from the filename.
The examples of File names are
xs_1234323_00_32
sf_12345233_99_12
fs_01923122_12_12
I used String parsedname= child.getName().substring(4.9) to get the 1234323 out of the first line. Instead, how do I format it for the above 3 to output only the middle numbers(between the two _)? Something using split?
one line solution
String n = str.replaceAll("\\D+(\\d+).+", "$1");
most efficent solution
int i = str.indexOf('_');
int j = str.indexOf('_', i + 1);
String n = str.substring(i + 1, j);
String [] tokens = filename.split("_");
/* xs_1234323_00_32 would be
[0]=>xs [1]=> 1234323 [2]=> 00 [3] => 32
*/
String middleNumber = tokens[2];
You can try using split using the '_' delimiter.
The String.split methods splits this string around matches of the given ;parameter. So use like this
String[] output = input.split("_");
here output[1] will be your desired result
ANd input will be like
String input = "xs_1234323_00_32"
I would do this:
filename.split("_", 3)[1]
The second argument of split indicates the maximum number of pieces the string should be split into, in your case you only need 3. This will be faster than using the single-argument version of split, which will continue splitting on the delimiter unnecessarily.
I have for example this string:
hello.name-2.txt
And I need to remove only character after "-".
So my output should look:
hello.name-.txt
How can I do it?
You can do
s = s.replaceAll("-.", "-");
if you want to replace a number even "hello.name-1234.txt" you can use
s = s.replaceAll("-\\d+", "-");
If you only want to do this once, you can use replaceFirst instead.
int dashIndex = yourString.indexOf("-");
String result = yourString.substring(0, dashIndex + 1)
+ yourString.substring(dashIndex + 2);