DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Automatic Code Transformation With OpenRewrite
  • The Human Side of Logs: What Unstructured Data Is Trying to Tell You
  • 5 Subtle Indicators Your Development Environment Is Under Siege
  1. DZone
  2. Data Engineering
  3. Data
  4. The Right Way to Reverse a String in Java

The Right Way to Reverse a String in Java

Learn the right way to reverse a String.

By 
Jonatan Ivanov user avatar
Jonatan Ivanov
·
Updated Dec. 06, 18 · Tutorial
Likes (26)
Comment
Save
Tweet
Share
46.4K Views

Join the DZone community and get the full member experience.

Join For Free

Facts and Terminology

As you probably know, Java uses UTF-16 to represent String. The char data type and the Character class are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. Therefore, in the UTF-16 representation, there are characters (Code Points) that are represented by one- and some other characters that are represented by two char values (Code Units).

Please check out the Java String length confusion article and the JavaDoc of the Character class for more details and a more-detailed explanation.

Example

Character: A
"UTF-16 representation" in Java: "\u0041"

Character: Mathematical double-struck capital A (Unfortunately, the DZone editor has the same issue as what is described below, it prints ?? instead of the real character after save: ��)
"UTF-16 representation" in Java: "\uD835\uDD38"

The first one is straightforward; the second one is a little bit more interesting; this single character (Code Point) is represented by two Unicode escapes. This means a couple of things:

  • This single character is represented by two char (or Character) values (Code Units)

  • The length() of this String is two (see: Java String length confusion)

  • The toCharArray() method returns a char array (char[]), which has two elements (0xD835 and 0xDD38 respectively)

  • Both charAt(0) and charAt(1) return something (no StringIndexOutOfBoundsException), but these values are not valid characters

  • If you do any character manipulation, you need to consider this case and handle these characters, which consist of two char (surrogates)

  • Therefore, most of the character manipulation code we ever wrote is probably broken.

This basically means that you probably do not want to do any character manipulation (see below).

Broken String Reverse

By this point, you might have a good guess what is wrong with this (very commonly used) solution to reverse a String:

static String reverse(String original) {
    String reversed = "";
    for (int i = original.length() - 1;  0 <= i; i--) {
        reversed += original.charAt(i);
    }

    return reversed;
}


Let's see it in action:

String str = "\uD835\uDD38BC"; // Three characters: A, B, C (4 chars)
System.out.println(str); // prints ABC (A is the double-struck A)
System.out.println(reverse(str)); // prints CB??


If you run the reverse method above, it will produce a String like this: "CB\uDD38\uD835". C and B are ok but \uDD38\uD835 is invalid, that's why you see ?? when you print it. The method should not have reversed them; the valid result would be "CB\uD835\uDD38" (CBA(double-struck A)).

Solution

Usually, not writing code to solve problems is a good idea:

static String reverse(String original) {
    return new StringBuilder(original).reverse().toString();
}


If you want to take a (small) step further, here's a Java 8+ one-liner:

Function<String, String> reverse = s -> new StringBuilder(s).reverse().toString();


If you are curious how this is implemented under the hood, check out what StringBuilder's reverse() method does (it is in theAbstractStringBuilder class).

So what broken String manipulations have you seen? Let us know in the comments below!

Data Types Java (programming language) Strings

Published at DZone with permission of Jonatan Ivanov. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Long Road to Java Virtual Threads
  • Exploring Exciting New Features in Java 17 With Examples
  • Proper Java Exception Handling
  • Generics in Java and Their Implementation

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!