Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8675545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:58:32+00:00 2026-06-12T19:58:32+00:00

I am facing a situation where i get Surrogate characters in text that i

  • 0

I am facing a situation where i get Surrogate characters in text that i am saving to MySql 5.1. As the UTF-16 is not supported in this, I want to remove these surrogate pairs manually by a java method before saving it to the database.

I have written the following method for now and I am curious to know if there is a direct and optimal way to handle this.

Thanks in advance for your help.

public static String removeSurrogates(String query) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < query.length() - 1; i++) {
        char firstChar = query.charAt(i);
        char nextChar = query.charAt(i+1);
        if (Character.isSurrogatePair(firstChar, nextChar) == false) {
            sb.append(firstChar);
        } else {
            i++;
        }
    }
    if (Character.isHighSurrogate(query.charAt(query.length() - 1)) == false
            && Character.isLowSurrogate(query.charAt(query.length() - 1)) == false) {
        sb.append(query.charAt(query.length() - 1));
    }

    return sb.toString();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:58:33+00:00Added an answer on June 12, 2026 at 7:58 pm

    Here’s a couple things:

    • Character.isSurrogate(char c):

      A char value is a surrogate code unit if and only if it is either a low-surrogate code unit or a high-surrogate code unit.

    • Checking for pairs seems pointless, why not just remove all surrogates?

    • x == false is equivalent to !x

    • StringBuilder is better in cases where you don’t need synchronization (like a variable that never leaves local scope).

    I suggest this:

    public static String removeSurrogates(String query) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < query.length(); i++) {
            char c = query.charAt(i);
            // !isSurrogate(c) in Java 7
            if (!(Character.isHighSurrogate(c) || Character.isLowSurrogate(c))) {
                sb.append(firstChar);
            }
        }
        return sb.toString();
    }
    

    Breaking down the if statement

    You asked about this statement:

    if (!(Character.isHighSurrogate(c) || Character.isLowSurrogate(c))) {
        sb.append(firstChar);
    }
    

    One way to understand it is to break each operation into its own function, so you can see that the combination does what you’d expect:

    static boolean isSurrogate(char c) {
        return Character.isHighSurrogate(c) || Character.isLowSurrogate(c);
    }
    
    static boolean isNotSurrogate(char c) {
        return !isSurrogate(c);
    }
    
    ...
    
    if (isNotSurrogate(c)) {
        sb.append(firstChar);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am facing a situation in which I want to ensure that if a
I am facing a situation that I have problem understanding... I am writing a
This situation probably is not entirely uncommon to some of you: you have some
I'm facing an annoying situation where my serialization/deserialization isn't as expected. I want <OuterClass>
I'm facing this situation where I have an ID which comes from a database
I am facing a situation to preserve the formatting of plain text email when
I am new to sphinx. The situation I am facing is that I have
Here is the situation that I'm facing: I have two tables A and B.
I have run into a difficult situation. I do not want to do my
I am facing a very weird situation on Mac OS X. This has bee

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.