String classs = "java1110======$500.50";
and I want to extract 500.50 from the String value. What should I do?
I tried replace() but it gives me 111050050.
try this :
class.substring(class.lastIndexOf("$") + 1);
Use the ASCII range of numbers to set up the condition that whenever the ASCII value of a particular character comes up in between you put it in a character array,or any string variable,it's all on your discretion.
Hope this helps
Related
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);
I have some issue like this
in my textview Rs. 99.99
String val = textview.getText().toString();
Result :: val :: Rs.99.99
i am converting that into float using this way
float value = Float.parseFloat(val);
i am getting NumberFormatException: Rs.99.99 cannot convert
any one guide me
You can do the following before converting it to float
String substring = str.length() > 2 ? str.substring(str.length() - 3) : str;
You can try this way.
System.out.println(Float.parseFloat("Rs.99.99".substring(3)));
Note: You need to make sure the string always contain "Rs." in the beginning.
i am getting numberFormat Exception Rs.99.99 cannot convert
Yes, because in method
Float.parseFloat(String s);
You get
NumberFormatException -- if the string does not contain a parsable float.
And In your case it isn't,
So best option is to apply Validation to enter only floating point numbers inside text View.
It's not entirely clear what the problem is (do all the strings that cause problems begin with Rs., or are users putting other kinds of garbage at the beginning of the input)? Here's a way to remove all characters from the string, up to (but not including) the first digit:
val = val.replaceFirst("^[^0-9]*", "");
This finds the first occurrence of a pattern that starts at the beginning of the string (the first ^) and consists of 0 or more occurrences of nondigits ([^0-9]).
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.]", "");
I have a String which has numbers and I want to add this sign ":" between every two numbers as if the string was 0123456789 I want it to be like this 01:23:45:67:89
Is there any way to insert it ?? as I read about replace() but this does not help in my case
You could use this magic piece of regex:
System.out.println("0123456789".replaceAll(".{2}(?!$)", "$0:"));
.{2} match 2 characters
(?!$) not at end
$0: First matched argument with : included
String x="0123456789";
String result="";
for(int i=0;i<x.length();i++){
result+=x.charAt(i);
if(i%2==1 && i+1<x.length())
result+=":";
}
I need to remove all the character in a String after a "?" mark like this:
http://xyz.com//static/css/style.css?v=e9b34
However I can't just spit it with "?", I need to make sure that the pattern is something .xyz?any_char_or_symbols
In other words extension-?-any_chars_or_symbols
Anyone can give a hint?
return s.replaceFirst("(\\.\\w+\\?).*$", "$1");
Am I missing something here or this does it:
String s = "http://xyz.com//static/css/style.css?v=e9b34";
int index = s.indexOf('?');
if (index<0)
return null;
String returned = s.substring(0,index);
if (returned.endsWidth(".xyz");
return returned;
else
return null;
First Remove the characters after ? then check for extentions.
String url="http://xyz.com//static/css/style.css?v=e9b34" ;
url=url.substring(0, url.indexOf("?"));
System.out.print(""+url.matches("^[\\w\\d\\:\\/\\.]+\\.\\w{3}(\\?[\\w\\W]*)?$"));
url.matches will check for the extention. I have defined it for 3 words w{3} but you can increase it as w{3,5}. now it will accepts 3 to 5 words extentions. if you want specific formats then use this patteren.
url.matches("^[\\w\\d\\:\\/\\.]+\\.(?i)(css|txt|html)?$")
Hope it will help.