3

I'm trying to check to see if a string contains a specific unicode point from the Segoe MDL2 Assets font.

An example of a unicode value that I want to check for is

\uF14B 

Here's where I'm grabbing my values from

https://learn.microsoft.com/en-us/windows/uwp/design/style/segoe-ui-symbol-font

How exactly can I check a string to see if it contains one of these values?

I have tried

 if (buttons[i].getText().contains("\uF14B")) { buttons[i].setFont(new Font("Segoe MDL2 Assets", Font.PLAIN, 15 )); } 

While this does work, I think that it's pretty ineffecient to have to copy and paste each and every value that I plan to use into a if statement.

Is there an easier way to do this?

Edit:

I ended up placing a ~ after each special character in my array, and parsed it like this. Are there any issues in doing this?

/** Creating the names of the buttons. */ String [] buttonNames = { "Lsh", "Rsh", "Or", "Xor", "Not","And", "\uE752~", "Mod", "CE", "C", "\uF149~", "\uE94A~", "A", "B", "\uF14D~", "\uF14E~", "\uE94F~", "\uE947~", "C", "D", "\uF14A~", "\uF14B~", "\uF14C~", "\uE949~", "E", "F", "\uF14A~", "\uF14B~", "\uF14C~", "\uE948~", "(", ")", "\uE94D~", "0", ".", "\uE94E~" }; /** more code here */ if (buttons[i].getText().contains("~")) { buttons[i].setFont(new Font("Segoe MDL2 Assets", Font.PLAIN, 15 )); buttons[i].setText(buttons[i].getText().substring(0, buttons[i].getText().lastIndexOf('~'))); } 
7
  • What I mean is, how exactly can I check to see if my string contains a unicode value. For example, checking if a string contained \u (although I know this actually won't work.) Commented Jul 11, 2018 at 2:07
  • you can use Matchers for this. Commented Jul 11, 2018 at 2:12
  • Every (non-empty) string contains unicode characters. Commented Jul 11, 2018 at 2:12
  • Possible Duplicate? stackoverflow.com/a/23109928/6785649 Commented Jul 11, 2018 at 2:14
  • An F1xx series character is not a font or even a "character" really, in that it doesn't have an assigned glyph or meaning. Basically "private use" is exactly that, and anyone can use it for anything. I think some websites use the same code points for random graphical glyphs like lines, elbows and arrows to help draw their UI. Commented Jul 11, 2018 at 2:20

2 Answers 2

3

You can invert the font selection logic:

The Font class has goodies like canDisplay and canDisplayUpTo. Javadoc:

public int canDisplayUpTo​(String str) 

Indicates whether or not this Font can display a specified String. For strings with Unicode encoding, it is important to know if a particular font can display the string. This method returns an offset into the String str which is the first character this Font cannot display without using the missing glyph code. If the Font can display all characters, -1 is returned.

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

1 Comment

Awesome. Thank you so much. I wouldn't have found this without your help. You saved me ~50 lines of code.
3

The best / easiest way to scan text to find certain characters is to use a regular expression character class.

A character class is written as [xxx] where xxx can be set of single characters, e.g. a or \uF14B, and/or ranges, e.g. a-z or \uE700-\uE71F.

So, you can write a regex like this:

[\uE700-\uE72E\uE730\uE731\uE734\uE735\uE737-\uE756] 

and so on (that was about 10% of the code points list on the linked page).

The above can also be done using exclusion, i.e.

[\uE700-\uE756&&[^\uE72F\uE732\uE733\uE736]] 

where the [^xxx] means "not any of these characters".

You then compile it and use it to check strings:

String regex = "[\uE700-\uE72E\uE730\uE731\uE734\uE735\uE737-\uE756]"; Pattern p = Pattern.compile(regex); if (p.matcher(buttons[i].getText()).find()) { 

1 Comment

I came up with a way to do it myself, can you check to see that this is an okay way of doing this?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.