Java String remove all non numeric characters but keep the decimal separator
Try this code:
String str = "a12.334tyz.78x";
str = str.replaceAll("[^\\d.]", "");
Now str
will contain "12.334.78"
.
Java Regex - remove non-numeric characters
Here is one viable approach:
String input = "hello world -23 as-df blah blah blah";
String output = input.replaceAll("[^\\d-]|-(?=\\D)", "");
System.out.println(output);
This prints:
-23
The regex used here says to match:
[^\\d-] any non numeric character EXCEPT for dash (i.e. spare -)
| OR
-(?=\\D) remove dash if NOT followed by a number
That is, we do strip off dash if it not be part of a negative number. The replacement is empty string, to remove this unwanted content.
Java remove non numeric characters from string except x
use this: [^x0-9]
You may check it on http://gskinner.com/RegExr/
RegEx to remove non-digit and non-decimal point from string
You may use:
str = str.replaceAll("[^\\d.]+", "");
[^\\d.]
is negated character class that will match any character except digit or dot.
Added +
to make this bit more efficient.
RegEx Demo
How to remove all non-numerical characters from a string input in java?
You can use this method to convert your string to only numericals:
public String getNumericString(String ourString)
{
StringBuilder neededCharacters = new StringBuilder();
//Read throught the entire length of your input string
for(int i = 0;i<ourString.length();i++)
{
//Get the current character
char ch = ourString.charAt(i);
//Check if the character is a numerical
if (Character.isDigit(ch))
{
//if the character is a number then add it to our string
// builder
neededCharacters.append(r.charAt(i));
}
}
onlyNumericalString = needed.toString();
return onlyNumericalString;
}
You can ask me if you don't understand anything in the code.You should make small edits depending on your needs.
Removing all but the first two numbers in a decimal
Multiply by 10,000 (100 for percentage, 100 for the two decimal places), truncate to integer, divide by 100 (to get back to percentage).
Writing it out one step at a time, for clarity of exposition:
Double n = input.nextDouble();
int i = (int)(n * 10_000);
Double pc = i / 100.0;
System.out.printf("%.2f\n", pc);
Java substring keep only digits and decimal
Try using a replaceAll on the string, specifying all characters that are not a decimal point or number:
myString = myString.replaceAll("[^0-9\\.]","");
"^0-9\." means all characters that are not a number 0 through 9, or a decimal. The reason why we put two slashes are to escape the period, as it has a different connotation in a Java regex than the literal character '.'.
Strip all non-numeric characters from string in JavaScript
Use the string's .replace
method with a regex of \D
, which is a shorthand character class that matches all non-digits:
myString = myString.replace(/\D/g,'');
Remove all non-numeric characters but keep a specific word
This should do the trick
Pattern pattern = Pattern.compile("Vol ([A-Z]{3}) Ch (\\d{3})");
Matcher matcher = pattern.matcher(input);
if(matcher.find()){
String volume = matcher.group(1);
String chapter = matcher.group(2);
}
Related Topics
Which Cipher Suites to Enable for Ssl Socket
How to Add Jradiobutton to Group in Jtable
Certain Unix Commands Fail with "... Not Found", When Executed Through Java Using Jsch
Java: Notify() VS. Notifyall() All Over Again
Using "Like" Wildcard in Prepared Statement
Split String on Spaces in Java, Except If Between Quotes (I.E. Treat \"Hello World\" as One Token)
How to Add Javafx Runtime to Eclipse in Java 11
"Integer Number Too Large" Error Message for 600851475143
Java Remove Duplicates from an Array
Java: How to Split an Arraylist in Multiple Small Arraylists
Handling Passwords Used for Auth in Source Code
How to Specify the Schema When Connecting to Postgres with Jdbc
Junit 5: How to Assert an Exception Is Thrown
How to Invoke a Linux Shell Command from Java