How to print only two digits after decimal with System.printf? [duplicate] - java

This question already has answers here:
String.split(".") is not splitting my long String
(2 answers)
Closed 8 years ago.
My problem is that double value converted to string cannot be splitted by dot.
Here you can see my code:
String valueOf = String.valueOf(12.34);
System.out.println("valueOf=" + valueOf);
String[] split = valueOf.split(".");
System.out.println("split=" + Arrays.toString(split));
The output is:
valueOf = 12.34
split = []
Why is split array empty?
You can try to run it on https://ideone.com/BBL4z2.

You need to escape . here. you can use \\.
String[] split = valueOf.split("\\.");
Because in regex you need to escape .

Related

How do I replace the " character in Java? [duplicate]

This question already has answers here:
What are all the escape characters?
(5 answers)
Closed 2 years ago.
I read a line from a txt file containing a string = "abc,"".
If one wanted to replace string to: string = "abc", one would write
string = string.replace(",",""); to replace the comma, but how would one replace the "?
Problem:
string = string.replace(""","");//code does not work because of """
You need to escape the ", so like this:
string = string.replace("\"","")

How do i remove digits from a String [duplicate]

This question already has answers here:
Java: Remove numbers from string
(6 answers)
Closed 2 years ago.
Basically my questions is how can i remove the digits from a string that is being inputted Thanks for helping me as well
Example:
Input
700N
Output:
N
Use String.replaceAll() method:
str.replaceAll("[0123456789]","");
You can use replaceAll
String st1 = "700N";
st1 = st1.replaceAll("\\d+", "");
System.out.println(st1);
output
N
The best way I could come up with :
String input ="700N";
String output= input.replaceAll("\\d","");
The regex \\d means digit.

how to get rid of all punctuation marks/ symbols in a string? [duplicate]

This question already has answers here:
Replacing all non-alphanumeric characters with empty strings
(14 answers)
Closed 4 years ago.
I want to get rid of all symbols or punctuation marks in a string
for example
String str = "\"hello!.?,\"";
the output of str should be: hello
You can use String's replaceAll.
String str = "\"hello!.?,\"";
String newStr = str.replaceAll("[^\\w]", "");
System.out.println(newStr);
For more details on how to construct a regex, see the documentation of Pattern.

Splitting a word where "." occurs [duplicate]

This question already has answers here:
Split string with dot as delimiter
(13 answers)
Closed 7 years ago.
I need to be able to split a single word string like "x.y" into an array of size 2 where the "." occurs, so the array would look like ["x", "y"]. What I've tried is:
String word = "hello.world";
String[] split = word.split(".")
return split.length == 2;
but this seems to just return an empty array (false). How would I go about doing this? Thanks.
Repalce this
String[] split = word.split(".")
with
String[] split = word.split("\\.")
. (dot) has a special meaning in regex so you need to escape it if you want to use it as a literal to split.

Splitting a String using split() not getting required result [duplicate]

This question already has an answer here:
Split string on spaces in Java, except if between quotes (i.e. treat \"hello world\" as one token) [duplicate]
(1 answer)
Closed 8 years ago.
I have a String(freeText) "Manas \"Jaikant IBM\"". I want to split into two strings:
String normalMatch="Manas";
String exactMatch="Jaikant IBM";
It means that String normalMatch contains Manas and String exactMatch contains Jaikant IBM.
i am using the split() method of String class in Java
String[] splittedText= freeText.split("\\s");
I am getting 3 string elements but i need 2 string elements only.
Use substring instead of split:
int index = freeText.indexOf(" ");
String normalMatch = freeText.substring(0,index);
String exactMatch = freeText.substring(index); // endIndex == freeText.length())
Split on quotes(") then, you will get Manas and Jaikant IBM and can ignore the 3rd value.

Categories

Resources