• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4825
  • Last Modified:

java function to test string for double byte characters

Hi,
   I need to know if there is a way in java to check if the input string contains double byte characters.
Thanks
0
res_this
Asked:
res_this
  • 3
1 Solution
 
marklorenzCommented:
Try this:

"java to convert the string text(with double byte code character) I receive from JTextField and
convert it to ASCII code"

http://java.ittoolbox.com/groups/technical-functional/java-l/how-to-add-double-byte-characterexample-chinese-characterto-mysql-database-using-java-484864


0
 
CEHJCommented:
*All* Java characters are double byte, in the sense that a String is composed of char[] and char is two bytes
0
 
res_thisAuthor Commented:
I dont need to convert, just need to check if the double byte characters exist in the string
0
 
CEHJCommented:
How many bytes are used depends on the encoding. For UTF-8, you could test as follows:
        for(int i = 0;i < s.length();i++) {
            String test = "" + s.charAt(i);
            byte[] bytes = test.getBytes("UTF-8");
            if (bytes.length > 1) {
                System.out.printf("char %s is %d bytes long\n", test, bytes.length);
            }
        }

Open in new window

0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now