Results 1 to 3 of 3
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Replacing at an index

    I am using Strings and want to replace a character at a particular index. How this can be done. I am not able to find the required method.

    Thanks.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Code:
    		String str = "there are 4 apples";
    		int index = 10 ;		
    		str = str.substring(0,index) + "5" + str.substring(index+1);
    		System.out.println(str);
    dont worry newbie, we got you covered.

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Why not just use the replace method

    Java Code:
    //We want to replace index 1 with the character 'a', so here's what we do
    String s = "Hello";
    char oldChar = s.charAt(1);  //returns the character at index 1
    String newString = s.replace(oldChar, 'a');
    This is what you wanted right?

Similar Threads

  1. Help with Flesch Index
    By L_22 in forum Advanced Java
    Replies: 1
    Last Post: 03-31-2008, 05:30 PM
  2. z-Index problem
    By mjdousti in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2007, 01:34 AM
  3. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 03:08 PM
  4. problems with array index
    By mary in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 04:30 PM
  5. Accessing index.dat files
    By vissu007 in forum Networking
    Replies: 1
    Last Post: 07-01-2007, 04:47 PM

Posting Permissions

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