Question:
Java: How to use String contains() for a hexadecimal character?
Allen M
2008-10-10 12:57:57 UTC
I'm trying to figure out whether a string contains an ndash character and if it does I want to replace it with a regular dash. When I try using the contains() method using the raw character the compiler issues a warning and the comparison does not work. So I tried myString.contains("\\x96") which is the hex representation of ndash. No compiler warning but it doesn't work.
Three answers:
liudude_123
2008-10-10 14:04:45 UTC
use the \u0096 instead of \x96, it's a string, not a regex.

String s = "6\u009610";

if (s.contains("\u0096")) {

System.out.println("yes:"+s);

}
deonejuan
2008-10-10 13:37:10 UTC
String.contains( charSequence);



I would use StringBuilder();



String hexWord = nDash;

StringBuilder sb = new StringBuilder( nDash );

boolean doesIt = suspectWord.contains( sb );



or...



String nDashChar = "\\x96";

char[] c = nDashChar.toCharArray();

cndsh = c[0];

if( suspectWord.charAt( i).equals( cndsh )
anonymous
2016-05-25 01:58:33 UTC
You'll never learn if we post the code for you. To the person below me, JavaScript isn't Java.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...