Results 1 to 3 of 3
  1. #1
    DannyGroff is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default inserting a character at multiple places in a string

    Here is what I am trying to do: I am reading a txt file and wanting to convert it to a csv file.
    I read in the file a line at a time and need to insert a , at various places within the line. These are not evenly spaced or anything. It would be like having a 60 character string and wanting to put a , after the 10,12,25,38,40,45,56 characters.

    I guess I could read it in character by character and do it that way, but would need to reset the count at the end of each line. Thx for all your help with this.

    Danny Groff

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,392
    Rep Power
    5

    Default Re: inserting a character at multiple places in a string

    If there is no deterministic way of inserting the commas, then you will probably just have to do it with insert positions. However, keep in mind that as you insert a comma, future insert positions are likely to be affected. It might be better to simply do an iterative substring append to a new string followed by an append of a comma. Check out StringBuilder in the JDK API.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DannyGroff is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: inserting a character at multiple places in a string

    Thanks for the info. I had already gotten it by reading the file character by character. But I will have to look into the StringBuilder. I hadn't seen anything about that. It looks kinda cool. Thx again.

Similar Threads

  1. Replies: 1
    Last Post: 11-06-2012, 02:29 PM
  2. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  3. Replies: 0
    Last Post: 11-06-2012, 02:09 PM
  4. Array, method to change character places
    By aramiky818 in forum New To Java
    Replies: 6
    Last Post: 09-12-2011, 12:38 AM
  5. Replies: 0
    Last Post: 02-07-2008, 08:57 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
  •