views:

149

answers:

4

How to get the character ± in a string?

+6  A: 

Put this into your string: \u00B1

Source

Matt Ball
+12  A: 

Use Unicode: \u00B1:

System.out.println("Hello \u00B1 world");

Prints:

Hello ± world
Grodriguez
And if ever you don't know the Unicode code point for something, Google does. "unicode plus minus" --> http://www.fileformat.info/info/unicode/char/b1/index.htm
Alan Krueger
Thank you very much......Grodriguez
Praveenb
@Alan Krueger: Googling "unicode plus minus" is a silly advice. It ain't very useful when you don't know the name of the character you're looking at. Like this one, for example, how would you Google it? ' ҉'. That's when a cut/paste and Un*x commands like *hexdump* and *file* are useful ;) Now of course you can cut/paste that character in Google and you'll find out, but suggesting to search for such characters using text like "unicode plus minus" just doesn't make much sense.
Webinator
http://www.unicode.org/charts/
starblue
@Webinator You can come up with counter-examples, but for someone who doesn't know where to look it's remarkably effective.
Alan Krueger
A: 

If you are on Linux system you can find hex codes for almost all the symbol using man pages iso_8859-1(7), iso_8859-10(7), iso_8859-13(7), iso_8859-14(7), iso_8859-15(7), iso_8859-16(7), iso_8859-2(7), iso_8859-3(7), iso_8859-4(7), iso_8859-5(7), iso_8859-6(7), iso_8859-7(7), iso_8859-8(7), iso_8859-9(7). There you will find that the hex code for ± is B1. There!! you have it now.

Amit S
A: 

(Prerequisite: source file must be properly encoded in utf8/utf16/unicode)

Nothing simpler than that:

string s = "±";

Note that java strings and identifiers are always encoded in utf16, so this is natively supported.

codymanix
Since I can't always guarantee what happens to the source after it's been written, and file systems don't generally treat character encoding as a first-class attribute of the file, if it's outside Latin-1 I typically use a Unicode escape. It seems safer that way.
Alan Krueger
Prerequisite? You can specify the encoding with javac? `javac -encoding Cp1252` for example. (I would not recommend this...)
Ishtar
@codymanix: but people having UTF-8 or UTF-16 *.java* source file should be shot! Here we have developers on Windows, Linux and OS X **AND** our build script enforces that *every single .java file* is ASCII-only. Why? Experience. Plain and simple.
Webinator
@Webinator: no, people using tools that don't support UTF-8 should be shot. Why? Experience. Plain and simple.
Tom Anderson
@Tom Anderson: in addition to that, I honestly pitty you and your team if you put UTF-8 characters in *.java* source file instead of externalizing these strings. You do deserve to be shot, because you continue to refuse to admit blatantly obvious evidence. I don't want you nor your crappy programming practice on my team. Same for the two tards who upvoted you.
Webinator
@Tom Anderson: btw, I happen to have quite some system admins skills, so I'm the one who did set up the entire DVCS and build system (including the automated *"no non-ASCII chars ever in a .java file"* check) and I if you really think that you have *"experience"* because you're using "tools" that understand UTF-8, you are completely missing the point. Of course we're using UTF-8 for a great many things, because it has its place. A *.java* file not being one of them. Have fun the day the sh!t hits the fan and remember my wise words.
Webinator
@Webinator: I work in a professional java environment. I am in one - although not working - this very instant. We all run OSes (OS X and various flavours of Linux) that support UTF-8. We moved away from Windows *precisely because* we had agonising problems around file encodings, and we would like to avoid having any shit hitting fans. It's working pretty well so far. I have two suggestions for you. Firstly, choose your tools to fit your process, not vice versa. Secondly, take some deep breaths.
Tom Anderson