Results 1 to 5 of 5
Like Tree2Likes
  • 2 Post By Junky

Thread: String.replace ?!

  1. #1
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default String.replace ?!

    public String repeatSeparator(String word, String sep, int count) {

    String v = "";
    for(int i=1;i<=count;i++){
    v += word + sep;
    }
    v = v.replace(v.substring(v.lastIndexOf(sep), v.length()), "");
    return v;
    }

    =======================
    this is my code and i wanna delete last sep .... how can i do that?
    Last edited by HearT.Hunt3r; 08-21-2011 at 04:31 PM.

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

  3. #3
    Join Date
    Jul 2011
    Posts
    47
    Rep Power
    0

    Default

    i would do that by replace function but i couldn't and i did this and fixed :
    v = v.substring(0, v.lastIndexOf(sep));
    Last edited by HearT.Hunt3r; 08-22-2011 at 03:07 PM.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Alternatively you could add the separator before each word except the first. Also, you might want to look into using a StringBuilder instead of all those String concatenations.
    Fubarable and sunde887 like this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,417
    Rep Power
    25

    Default

    Or add sep conditional on the value of i. Don't do it in the last iteration of the loop

Similar Threads

  1. Java String Replace
    By aham in forum New To Java
    Replies: 4
    Last Post: 05-17-2011, 08:46 AM
  2. Replace String
    By Raeghin in forum New To Java
    Replies: 1
    Last Post: 07-28-2009, 03:58 PM
  3. replace a string using Text i/o
    By PureAwesomeness in forum New To Java
    Replies: 9
    Last Post: 07-06-2009, 02:38 AM
  4. string replace problem
    By soni in forum Advanced Java
    Replies: 8
    Last Post: 07-06-2008, 01:21 AM
  5. String replace method
    By venkata.tarigopula in forum Advanced Java
    Replies: 1
    Last Post: 07-10-2007, 08:14 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
  •