java function to test string for double byte characters

Posted on 2008-11-11
Last Modified: 2013-11-24
   I need to know if there is a way in java to check if the input string contains double byte characters.
Question by:res_this
    LVL 12

    Expert Comment

    Try this:

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

    LVL 86

    Expert Comment

    *All* Java characters are double byte, in the sense that a String is composed of char[] and char is two bytes

    Author Comment

    I dont need to convert, just need to check if the double byte characters exist in the string
    LVL 86

    Accepted Solution

    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

    LVL 86

    Expert Comment


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    instanceof  operator in java 26 57
    advance java 4 36
    Java Message handling in Service Layer 3 22
    JAVA part two 5 18
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now