I have one String = GETMSG_m_m_5556 from this I want to read only 5556, means I want to read all the digits after last "_". The string has not fixed length of numbers it may be like, GETMSG_m_m_9898786589 OR GETMSG_m_m_98987865. So how can I read the numbers after "_"?
Can anyone suggest me the write way.? It may be foolish question but I am stuck on this. I cant get any idea about this.
Thanks in advance.
String digits = sampleString.subString(sampleString.lastIndexOf("_"),sampleString.lenght);
Get the last index of the char '_' in your string and make a required substring to get the numbers .see string.subString()
You can use the string.split() function and take last string.
String[] separated = yourString.split("_");
// Now choose the last array value
You can try using StringTokenizer
StringTokenizer st = new StringTokenizer(String);
while (st.hasMoreTokens()) {
String s = st.nextToken();
if (s.startsWith("_")) {
....
}}
Related
I want determine number from specify string.
Ex: I have many text strings, such as "3.2p" or "3.2px" or "xp3.2" or "p3.2x".
The final result I want is can get number from text in above. Expected result "3.2".
People who know,
Please help me,
Thanks,
I would first remove all the non-numeric characters using a regex, then parse what remains.
String str = input.replaceAll("[^\\d.]", "");
Float.parseFloat(str);
Use this:
String s = "ffffa32.334tccy";
s = s.replaceAll("[^\\d.]", "");
for example, i have the string "12,456,544,233" from the user input,
I want to take each number that is separated by commas and push each one into
a Stack, converting it to an int in the process because the Stack is .
(array implementation of a stack by the way)
So 12 would be at 0, 456 at 1, 544 at 2, etc...
I KNOW I have to use the Integer class to parse, but just not sure how to setup the loop to do everything, if i didn't provide enough info, ask and I will do so!
thanks.
The code I tried:
String input = scan.nextLine();
stack.push(Integer.parseInt(String.valueOf(input.charAt(2))));
Sounds like homework. so just giving some hints
you can use String.split method to split the string into tokens separated by commas
now traverse the array that you get after split and push to stack.
N.B. if it is really a homework then may be you need to implement your own split
Here is how you can split strings
String string = "12,456,544,233";
String[] individualStrings = string.split(",");
split() method Splits this string around matches of the given regular expression.
Next, you can interate over string array and convert each element into integer.
for(int i = 0; i < individualStrings.length; i++)
{
int m = Integer.parseInt(individualStrings[i]);
}
Cheers !!
I've got a string that I'm supposed to use StringTokenizer on for a course. I've got my plan on how to implement the project, but I cannot find any reference as to how I will make the delimiter each character.
Basically, a String such as "Hippo Campus is a party place" I need to divide into tokens for each character and then compare them to a set of values and swap out a particular one with another. I know how to do everything else, but what the delimiter would be for separating each character?
If you really want to use StringTokenizer you could use like below
String myStr = "Hippo Campus is a party place".replaceAll("", " ");
StringTokenizer tokens = new StringTokenizer(myStr," ");
Or even you can use split for this. And your result will be String array with each character.
String myStr = "Hippo Campus is a party place";
String [] chars = myStr.split("");
for(String str:chars ){
System.out.println(str);
}
Convert the String to an array. There is no delimiter for separating every single character, and it wouldnt make sense to use string tokenizer to do that even if there was.
You can do something like:
char[] individualChars = someString.toCharArray;
Then iterate through that array like so:
for (char c : individualChars){
//do something with the chars.
}
You can do some thing like make the string in to a Char array.
char[] simpleArray = sampleString.toCharArray();
This will split the String to a set of characters. So you can do the operations which you have stated above.
I am new to java progrmming and came across the StringTokenizer class. The constructor accepts the string to be split and another optional delimiter string each character of which gets treated as an individual delimiter while splitting the original string. I was wondering if there is any way to split the string passing a regex as the delimiter. for example:
String s="34.5xy32.6y45.7x36xy"
StringTokenizer t=new StringTokenizer(s,"xy");
System.out.println(t.nextToken());
System.out.println(t.nextToken());
The actual output is:
34.5
32.6
However, the desired output is:
34.5
32.6y45.7x36
Hope you guys can help. Also, please suggest some way around if it is not possible with StringTokenizer class.
Thanks in advance.
p.s. Is there any way to know which character the StringTokenizer is currently using as delimiter out of the provided set?
Here you would want to use String.split(), this will give you an array with your desired output.
It will take your input and split it around exact matches of your string you provide. StringTokenizer will split around anyone of the set that you provide it rather than a regular expression.
So you change your code to:
String s="34.5xy32.6y45.7x36xy";
String[] splitString = s.split("xy");
System.out.println(splitString [0]);
System.out.println(splitString [1]);
For more complex examples you probably want boundary checking on the array also to make you don't go off the end of the array
Try with this.
String s="34.5xy32.6y45.7x36xy";
final String SPLIT_STR = "xy";
final String mainStr = "34.5xy32.6y45.7x36xy";
final String[] splitStr = mainStr.split(SPLIT_STR);
System.out.println("First Index Of xy : " +
mainStr.indexOf(SPLIT_STR));
for(int index=0; index < splitStr.length; index++) {
System.out.println("Split : " + splitStr[index]);
}
What I want to do is to measure the data of a line on a large string. I am not sure if any has tried this but I have a string which looks like this.
String a =
"This is
kinda
my String"
which would display on android textview as
This is
kinda
my String
Now what I want to achieve is being able get the length of the second line "kinda".
The purpose for this is to be able to set my paging for a book project.
I hope I was clear enough. Thanks for any advice or ideas shared.
Should just be:
a.split("\n")[1].length()
You can use the String function split(String regex)
To split on a "\n"(newline) then use it as a tuple/array and call for any word you want.
Split based on new line indicator.
String lines[] = a.split("\\r?\\n");
int length =0;
if(lines.length >1)
{
length = lines[1].length();
}
I haven't used java in years that being said I'd imagine something like this
String[] temp; //Let's make an array of strings
temp = a.split("\n"); //Split the large string by carriage return
int length = temp[1].length(); //get the length of the 2nd string
Assuming those are \n separating your lines...