The Artima Developer Community
Sponsored Link

Java Answers Forum
String reversal

3 replies on 1 page. Most recent reply: Oct 28, 2002 1:28 AM by Jody Brown

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Nimesh

Posts: 7
Nickname: nm
Registered: Oct, 2002

String reversal Posted: Oct 25, 2002 6:41 AM
Reply to this message Reply
Advertisement
Hi!
Can anyone tell me how to create a function which returns an initial string in reverse. I think you have to use another string where the characters of the initial string are appended backwards.

E.g Initial string = "Hello"
Returned string = "olleH"

Thanks
NM


Jody Brown

Posts: 8
Nickname: jody
Registered: Mar, 2002

Re: String reversal Posted: Oct 25, 2002 7:22 AM
Reply to this message Reply
Create a StringBuffer object from your string then call the reverse() method on it. This simply reverses the contents of it.

Simple when you know how.

Nimesh

Posts: 7
Nickname: nm
Registered: Oct, 2002

Re: String reversal Posted: Oct 26, 2002 8:18 AM
Reply to this message Reply
OK thanx.
I got it working...but how do I get it into a function so that it returns the reversed string. I've only been doing Java for 3 days so any examples of code would be great.

Thanx. NM

Jody Brown

Posts: 8
Nickname: jody
Registered: Mar, 2002

Re: String reversal Posted: Oct 28, 2002 1:28 AM
Reply to this message Reply
A simple utility method like the following would suffice:
public String reverseString(String stringToBeReversed)
{
   StringBuffer sb = new 
         StringBuffer(stringToBeReversed);
   
   return sb.reverse().toString();
}

Flat View: This topic has 3 replies on 1 page
Topic: java net question Previous Topic   Next Topic Topic: Parallel Port Comunication in Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use