Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It'sIts implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. Its implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java? What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementationimplementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)
Learn from the existing implementations, they usually have solutions to corner cases and common pitfalls. For example, Apache Commons Lang StringUtils
also has a reverse
function. It's implementation is quite simple:
public static String reverse(final String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
It uses StringBuilder.reverse
whose javadoc mentions some special cases:
If there are any surrogate pairs included in the sequence, these are treated as single characters for the reverse operation. Thus, the order of the high-low surrogates is never reversed.
Here are a few tests:
@Test
public void testCstring() {
assertEquals("\uD800\uDC00", CString.reverse("\uD800\uDC00")); // fails
assertEquals("\uD800\uDC00", CString.reverse("\uDC00\uD800")); // OK
}
@Test
public void testStringUtils() throws Exception {
assertEquals("\uD800\uDC00", StringUtils.reverse("\uD800\uDC00"));
assertEquals("\uD800\uDC00", StringUtils.reverse("\uDC00\uD800"));
}
I don't know too much about these surrogates but you should check it and handle them in your code. I suppose the JDK's implementation is more reliable and it's not coincidence that the handle them in the way they mention in the javadoc.
There is a good question (with great answers) about surrogates on Stack Overflow: What is a surrogate pair in Java?
(See also: Effective Java, 2nd edition, Item 47: Know and use the libraries)