Results 1 to 6 of 6
  1. #1
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default how to change a single index of a string

    i cannot figure out how to change a single index of a string... for instance if i have 849586M94 i would need to take out the m and change it to a six... i have everything else done i just dont know how to set the int to the index of m

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    The change can be simply done via the replace or replaceAll method. Much depends upon your specific needs. Also, please remember that Strings are immutable, and that there are no methods that "change" a String, but rather there are methods that can create and return a new String based on the original String.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Use the String methods:

    * indexOf() will find the location of the M
    * substring() will extract the portions before and after the M

    Then use string concatenation (+) to put the pieces together. String concatenation will work with ints as well as strings.

    -------------------

    Must you use String? Strings are immutable (they don't ever change their contents) which is why you have to break the string apart and put the pieces together again to form a new string. Perhaps StringBuilder would be easier to deal with.

  4. #4
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    yes i tried the replace method originally but maybe i am unsure what exactly it does.... if if i do .replace will it basically rebuild the string w/ my change? and keep doing that until it is done? so for instance i do a loop to check for letters in a string input and i want to change all the letters to numbers ill just do the .replace until it has finished looping and get what i am supposed to ...after i change the int to a string though right

  5. #5
    ftrengnr is offline Member
    Join Date
    Sep 2010
    Location
    Oklahoma
    Posts
    14
    Rep Power
    0

    Default

    tnx for the tips i will see what i can work out... i am interested to see how both directions will work

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    For this other task I really would use something mutable: an array of char perhaps.

Similar Threads

  1. Change of index in charArray
    By kovalensue in forum New To Java
    Replies: 19
    Last Post: 11-10-2010, 12:34 PM
  2. String index out of range: -1 Please Help
    By davetheant in forum New To Java
    Replies: 22
    Last Post: 09-11-2010, 01:32 AM
  3. Replies: 2
    Last Post: 03-27-2010, 08:22 PM
  4. find index of string in another string
    By Sdannenberg3 in forum New To Java
    Replies: 4
    Last Post: 03-04-2010, 11:14 AM
  5. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 12:51 PM

Tags for this Thread

Posting Permissions

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