String how to partially replace special char/string

Discussion in 'Java' started by John_Woo, Jul 6, 2006.

  1. John_Woo

    John_Woo Guest

    Hi,

    I'm trying to partially replace a string with a substring, like

    old = "1..2..3..4"
    new="1*2*3..4"

    two ways to implement:

    1. old.replaceAll ("\\..","*");

    2. Pattern pattern = Pattern.compile("[..]{2}");
    Matcher matcher = pattern.matcher(old);

    matcher.replaceAll("*");

    but these two result in

    1*2*3*4

    Can any one tell how to force to only replacing the first #1,2,
    occurrances?

    --
    Thanks
    John
    Toronto
     
    John_Woo, Jul 6, 2006
    #1
    1. Advertising

  2. John_Woo

    Alex Whitney Guest

    Split the string up using ".." as the delimeter. Insert a * between
    each element. Between the nth element and n-1, insert ".." instead.

    Scanner scan = new Scanner (old);
    scan.useDelimiter (Pattern.compile("[..]{2}");

    String newStr = "";
    newStr = scan.next(); // Run time error if old is empty.
    while (scan.hasNext())
    {
    String next = scan.next();

    if (scan.hasNext())
    newStr = newStr + "*" + next;
    else
    newStr = newStr + ".." + next;
    }



    John_Woo wrote:
    > Hi,
    >
    > I'm trying to partially replace a string with a substring, like
    >
    > old = "1..2..3..4"
    > new="1*2*3..4"
    >
    > two ways to implement:
    >
    > 1. old.replaceAll ("\\..","*");
    >
    > 2. Pattern pattern = Pattern.compile("[..]{2}");
    > Matcher matcher = pattern.matcher(old);
    >
    > matcher.replaceAll("*");
    >
    > but these two result in
    >
    > 1*2*3*4
    >
    > Can any one tell how to force to only replacing the first #1,2,
    > occurrances?
    >
    > --
    > Thanks
    > John
    > Toronto
     
    Alex Whitney, Jul 6, 2006
    #2
    1. Advertising

  3. John_Woo

    Alex Whitney Guest

    Oops.

    Scanner scan = new Scanner (old);
    scan.useDelimiter (Pattern.compile("[..]{2}");

    String newStr = "";
    newStr = scan.next(); // Run time error if old doesn't have enough
    data...
    newStr = newStr + "*" + scan.next();
    newStr = newStr + "*" + scan.next();
    while (scan.hasNext())
    {
    String next = scan.next();

    if (!scan.hasNext())
    newStr = newStr + ".." + next;
    }



    Alex Whitney wrote:
    > Split the string up using ".." as the delimeter. Insert a * between
    > each element. Between the nth element and n-1, insert ".." instead.
    >
    > Scanner scan = new Scanner (old);
    > scan.useDelimiter (Pattern.compile("[..]{2}");
    >
    > String newStr = "";
    > newStr = scan.next(); // Run time error if old is empty.
    > while (scan.hasNext())
    > {
    > String next = scan.next();
    >
    > if (scan.hasNext())
    > newStr = newStr + "*" + next;
    > else
    > newStr = newStr + ".." + next;
    > }
    >
    >
    >
    > John_Woo wrote:
    > > Hi,
    > >
    > > I'm trying to partially replace a string with a substring, like
    > >
    > > old = "1..2..3..4"
    > > new="1*2*3..4"
    > >
    > > two ways to implement:
    > >
    > > 1. old.replaceAll ("\\..","*");
    > >
    > > 2. Pattern pattern = Pattern.compile("[..]{2}");
    > > Matcher matcher = pattern.matcher(old);
    > >
    > > matcher.replaceAll("*");
    > >
    > > but these two result in
    > >
    > > 1*2*3*4
    > >
    > > Can any one tell how to force to only replacing the first #1,2,
    > > occurrances?
    > >
    > > --
    > > Thanks
    > > John
    > > Toronto
     
    Alex Whitney, Jul 6, 2006
    #3
  4. "John_Woo" <> writes:

    > I'm trying to partially replace a string with a substring, like
    >
    > old = "1..2..3..4"
    > new="1*2*3..4"
    >
    > two ways to implement:
    >
    > 1. old.replaceAll ("\\..","*");


    You probably also want to escape the second ".".

    > 2. Pattern pattern = Pattern.compile("[..]{2}");


    What are you expecting this pattern to match? It matches
    exactly the same as "\\.\\." or "[.][.]".

    > Can any one tell how to force to only replacing the first #1,2,
    > occurrances?


    1) Use String.replaceFirst twice.
    2) Use split+join:
    String[] parts = old.split("\\.\\.",3);
    newString = parts[0] + "*" + parts[1] + "*" + parts[2];

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 6, 2006
    #4
  5. John_Woo

    shakah Guest

    John_Woo wrote:
    > Hi,
    >
    > I'm trying to partially replace a string with a substring, like
    >
    > old = "1..2..3..4"
    > new="1*2*3..4"
    >
    > two ways to implement:
    >
    > 1. old.replaceAll ("\\..","*");
    >
    > 2. Pattern pattern = Pattern.compile("[..]{2}");
    > Matcher matcher = pattern.matcher(old);
    >
    > matcher.replaceAll("*");
    >
    > but these two result in
    >
    > 1*2*3*4
    >
    > Can any one tell how to force to only replacing the first #1,2,
    > occurrances?
    >
    > --
    > Thanks
    > John
    > Toronto


    What about a simple:

    old.replaceFirst("\\.\\.", "*").replaceFirst("\\.\\.", "*") ;

    ?
     
    shakah, Jul 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    558
  2. wwj
    Replies:
    24
    Views:
    2,521
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    480
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    590
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,064
    Ian Collins
    May 9, 2006
Loading...

Share This Page