How to insert commas into a number? - java

I've found such an example of using String.format() in a book:
package stringFormat;
public class Main {
public static void main(String[] args) {
String test = String.format("%, d", 1000000000);
System.out.println(test);
}
}
According to the book the output should be: 1,000,000,000. But when I run the code I only get 1 000 000 000 without the commas. Why? how can I get it with commas?

Reproduce the problem with Locale.FRANCE:
Locale.setDefault(Locale.FRANCE);
String test = String.format("%, d", 1000000000);
System.out.println(test); // 1 000 000 000
You can avoid this with Locale.US:
String test = String.format(Locale.US, "%, d", 1000000000);
or
Locale.setDefault(Locale.US);
String test = String.format("%, d", 1000000000);

You can read about the format in Java in the link:
https://docs.oracle.com/javase/tutorial/java/data/numberformat.html
For your problem, you can fix:
public static void main(String[] args) {
String s = "1000000000";
System.out.format("%,"+s.length()+"d%n", Long.parseLong(s));
}
Hope to helpfull!

Related

Convert String with special characters

String descriptionEscaped = "Domnul Florin Cîţu afirmă"
=>
String descriptionEscaped = "Domnul Florin Cîţu afirmă, sâmbătă"
Is there a way to do this ?
(Sorry for the confusing title of question)
following is helping you to convert special characters
public static void main(String[] args) {
String descriptionEscaped = "Domnul Florin Cîţu afirmă";
descriptionEscaped =
StringEscapeUtils.unescapeHtml4(descriptionEscaped);
System.out.println(descriptionEscaped);
}

Remove a character from java string using hex code

i would like to remove a character from java string using hex code:
i am trying following code but seems to not be correct as the character isn't replaced: ÿ
String str ="test ÿ";
str.replaceAll("\\x{9F}","")
is there any thing wrong with the syntax of the hex code? Thanks.
Could you please try this:
public class AsciiHexCode {
public static void main(String[] args) {
String str = "test ÿ";
String result = str.replaceAll("[^\\x00-\\x7F]", "");
System.out.println("result : "+ result);
}
}
To mach ÿ you need \u00ff instead, as Jon mentioned.
String replaced = str.replace("\u00ff", "");
in your case.

regex does not like out#

I write the following code o remove all hashtag words from my text:
public static void main(String[] args) {
System.out
.println(removeHashtag("Got an infection in my eye. Pharmacist thinks something bitten me. This wouldn't have happened under Simeone. Wenger a#sarcasm #wengerin"));
}
public static String removeHashtag(String commentstr) {
String arrWord[] = commentstr.split(" ");
String sentenceWithoutHash = commentstr;
System.out.println(sentenceWithoutHash);
for (int i = 0; i < arrWord.length; i++) {
if (arrWord[i].contains("#")) {
String regex = "\\s*\\" + arrWord[i] + "\\b\\s*";
sentenceWithoutHash = sentenceWithoutHash.replaceAll(regex, "");
}
}
return sentenceWithoutHash;
}
But this code does not work wih this text
Got an infection in my eye. Pharmacist thinks something bitten me. This wouldn't have happened under Simeone. Wenger out#sarcasm #wengerin"
it seems that regex does not like out#
Can anyone help?
You can use this regex to remove any word containing #:
String rep = str.replaceAll("\\s*\\w*#\\w*\\s*", "");
RegEx Demo
This will work as per your condition
((?:[^\s]+)?#[^\s]+)
Regex Demo
String x = str.replaceAll("((?:[^\\s]+)?#[^\\s]+)", "")

output is empty when using string split in java

I am using
public class Test {
public static void main(String[] args)
{
String str = "12,23,34,65,67,87,98,78,09,31,56,98,45";
String s1[] = str.split(str,3);
for(String s:s1)
System.out.println(s);
}
}
But the output is empty.I am getting empty console.But when i print the s1 array length it is giving 2 for me.What is the wrong here.
Thanks in advance..
You will get following out put by using str.split(",",3);
12
23
34,65,67,87,98,78,09,31,56,98,45
If you want to get
12
23
34
You can try something like this
String str = "12,23,34,65,67,87,98,78,09,31,56,98,45";
String s1[] = str.split(",",3);
for(String s:s1) {
System.out.println(s.substring(0,2));
}
Your question is unclear. It appears you want to split the String for every 3 numbers. For this you can use
String[] s1 = str.split("(?<=\\G\\d+,\\d+,\\d+)");

How do I split a string on a fixed character sequence?

Suppose I have following string:
String asd = "this is test ass this is test"
and I want to split the string using "ass" character sequence.
I used:
asd.split("ass");
It doesn't work. What do I need to do?
It seems to work fine for me:
public class Test
{
public static void main(String[] args) {
String asd = "this is test ass this is test";
String[] bits = asd.split("ass");
for (String bit : bits) {
System.out.println("'" + bit + "'");
}
}
}
Result:
'this is test '
' this is test'
Is your real delimiter different perhaps? Don't forget that split uses its parameter as a regular expression...
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
Try this it will work
public class Splitter {
public static void main(final String[] args) {
final String asd = "this is test ass this is test";
final String[] parts = asd.split("ass");
for (final String part : parts) {
System.out.println(part);
}
}
}
Prints:
this is test
this is test
Under Java 6. What output were you expecting?

Categories

Resources