Results 1 to 3 of 3
  1. #1
    HelpMePls is offline Member
    Join Date
    Feb 2011
    Rep Power

    Unhappy Swapping pair of character in a string

    For one assignment I've gotten today, I need to take the string and swap every 2 pairs of characters as encryption.

    eg. testing here

    After the encrypting,

    It will be "ettsni geher"

    How do i go about it??

    Thanks a million in advance! :(

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Rep Power


    Use the methods of the String class but remember String is immutable so you need to create a new String all the time.

    Or you can use StringBuilder or StringBuffer classes which are basically mutable Strings and have mostly the same methods that String has.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    Another way would be to look at the String methods for one that gives you an array of characters. (And for a suitable constructor so you can get a String back when you have finished working on the array).

    Working along the array and swapping the characters is something that suggests using a for loop.

Similar Threads

  1. Removing last character from a String
    By newToJava3 in forum New To Java
    Replies: 2
    Last Post: 11-28-2010, 01:43 AM
  2. compare String to the character .
    By Bulelakes in forum JDBC
    Replies: 12
    Last Post: 07-28-2010, 12:35 PM
  3. Taking a character off the end of a string.
    By in forum New To Java
    Replies: 5
    Last Post: 05-05-2010, 03:56 AM
  4. how to get next character/string
    By doha786 in forum New To Java
    Replies: 3
    Last Post: 03-28-2009, 05:04 AM
  5. How to store property file into key value pair
    By Java Tip in forum java.util
    Replies: 0
    Last Post: 04-05-2008, 11:16 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