2

I need to assign a utf-8 character to string array. How could I do this? I was just simple pasting it into eclipse from wordpad but some characters are shown as rectangles for example:

equations[4][2] = "N=N₀2";

In this case it is subscript 0; This doesn't work (utf-8 code taken only as an example):

equations[4][2] = "N=N"+char(U+00BA)+"2";
12
  • Try "N=N\u20802"; it's guaranteed to work. If it doesn't work, then your problem isn't what you think it is. (Probably it's a problem of displaying the string rather than of storing data in the string.) Commented Mar 6, 2012 at 0:36
  • I don't want to be annoying but could you say where you got that value from? Now I am using \u2080 to get subscript 0 but it gives me a star (fileformat.info/info/unicode/char/2080/index.htm) Commented Mar 6, 2012 at 0:51
  • I'm missing something: the page that you link to says "SUBSCRIPT ZERO" and displays a subscript zero. Commented Mar 6, 2012 at 0:56
  • It really worked before...sorry I messed up something Commented Mar 6, 2012 at 1:05
  • So . . . do you still have a problem? I don't totally understand your comments, sorry. Commented Mar 6, 2012 at 1:06

2 Answers 2

1

Not all Unicode values are natively supported on Android. If you are trying to set a subscript or superscript to the text in a TextView or on a Button or some similar View with text, you can use HTML like this:

(TextView)findViewById(R.id.textView).setText(Html.fromHtml("X<sup>2</sup>"));

This is handy and easy for subscripts and superscripts. For other Unicode characters, if it doesn't display in Android, then it probably just means that it is not supported on the built-in fonts. In that case, you would need to import your own font.

Summary of Options: You can store the subscript 0 value in your array however you like, but once you need to display it in text on the device, you need to 1) Use HTML, OR 2) Import your own font.

Also, welcome to StackOverflow! Your question was well constructed and formatted, but I would also recommend looking at the FAQ to see the ins and outs of accepting answers. You accept an answer that has solved your question to your desire by clicking the check box next to that answer. This gives the person credit and helps future viewers. Happy coding!

Sign up to request clarification or add additional context in comments.

3 Comments

I need to assign this value to the array. This is a problem
@Laurynas From your comments above, it looks like you are using the Unicode just fine in the array. My point was that if they are not displaying correctly on the device, you'll need to use one of my approaches to display them. It sounds like the Unicode values are just not supported in the Android default fonts.
@Laurynas I just tested all three Unicode (\u2082, \u2081 and \u2080) values you were trying. The subscript 1 and 2 work, but not the 0 (I get the box, like you do). So \u2080 does not seem to be natively supported. You'll need to use HTML or import a font as I describe above.
0

Convert your file to UTF8 and write the character in the code like other caractere.

1 Comment

I saved this file as UTF-8 and wrote the character as a code but it still doesn't work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.