Determine a number from text string - java

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.]", "");

Related

Regex Java when we have specific text upto a pattern

As i haven't much worked on regex, can someone help me out in getting the answer for below thing:
(1)I want to remove a text say Element
(2)It may of may not followed by delimiter say pipe(||)
I tried below thing, but it is not working in the way i want:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
System.out.println(str.replaceFirst("Element.*\\||", ""));
System.out.println(str1.replaceFirst("Element.*\\||", ""));
Required output in above cases:
String:abc||Value:abc //for the first case
String:abc //for the second case
Assuming that you can decide to give another value to the original pattern which is Element in this case, you can use Pattern.quote to escape it as below:
String str = "String:abc||Element:abc||Value:abc"; // Sample text 1
String str1 = "String:abc||Element:abc"; // Sample text 2
String originalPattern = "Element";
String pattern = String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern));
System.out.println(str.replaceFirst(pattern, ""));
System.out.println(str1.replaceFirst(pattern, ""));
Your patter is then generic and its value is String.format("\\|{2}%s[^\\|]+", Pattern.quote(originalPattern))
Output:
String:abc||Value:abc
String:abc
You put the escape wrong. It should be:
Element(.*?\|\||.*$)
Put the escape on each pipe, and use ? for non greedy Regex so you only replace just enough string, not everything.
String text = "String:abc||Element:abc||Value:abc";
text = text.replaceAll("\\belement\\b", "");
you might need to use replace all this will replace all element from your string here i am using '\b' word boundary in java regular expression in between the words

How do I extract number from a String in Java?

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

How to break a string into an array

I have a problem with parsing text, i have transcript of interview and i have a tag which channel is talking (ch1,ch2). And i need to break it into array and i could to search in which channel someone tells specific word.
For example this is a part of interview
<ch1>Hello</ch1> <ch2>Hello</ch2> <ch1>How are you</ch1><ch2>I'm fine</ch2>
This is a string
String text = "<ch1>Hello</ch1> <ch2>Hello</ch2> <ch2>How are you</ch2>
<ch2>I'm fine</ch2>";
And i want output
String output[] = {<ch1>Hello</ch1>,<ch2>Hello</ch2>,....}
Thanks for help.
You can use a regular expression with lookahead and lookbehind:
String dialogue = "<ch1>Hello</ch1> <ch2>Hello</ch2> <ch1>How are you</ch1><ch2>I'm fine</ch2>";
String[] statements = dialogue.split("(?<=</ch[12]>)\\s*(?=<ch[12]>)");
System.out.println(Arrays.asList(statements));
Output:
[<ch1>Hello</ch1>, <ch2>Hello</ch2>, <ch1>How are you</ch1>, <ch2>I'm fine</ch2>]
It's a bit hard to read due to the many < and >, but the pattern is like this:
split("(?<=endOfLastPart)inBetween(?=startOfNextPart)")
text.split("<ch").join("-<ch").split("-").
Can be any string instead of "-" which can be used.

Removing series of character in a String

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.

How to get perticular digits from the string in android?

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("_")) {
....
}}

Categories

Resources