Results 1 to 5 of 5
  1. #1
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default Reversing String

    Hi

    I have following String:

    Java Code:
    String s = "j1.2, s2.2, l3.2, p2.2, kl5.5";
    I want to reverse it. I can do this character by character but its hectic of course:(

    Is there a better way? Thanks in advance.

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    I would probably change the String to a character array and then just step through it backwards. That's probably what you are already doing. So to answer your question, not that I know of.

  3. #3
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

    Default

    Thanks.
    So there is no reverseString() method available. SAD

  4. #4
    staykovmarin is offline Senior Member
    Join Date
    Nov 2007
    Location
    Newport, WA
    Posts
    141
    Rep Power
    0

    Default

    Java Code:
    	public static String reverseString(String s) {
    		char[] strArr = s.toCharArray();
    		String output = "";
                    // read string backwards
    		for (int i = strArr.length-1; i >= 0; i --) {
    			output += strArr[i];
    		}
    
    		return output;
    	}

  5. #5
    mew
    mew is offline Member
    Join Date
    Nov 2007
    Posts
    70
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  2. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  3. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM
  4. Replies: 1
    Last Post: 08-07-2007, 07:29 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •