String replacement( 2 slashes-> 4 slashes )

Discussion in 'Java' started by qazmlp, Apr 7, 2004.

  1. qazmlp

    qazmlp Guest

    I have a string like this: "one\\two\\three\\"
    I want to replace 2 continuous slashes that figure in the string with
    4 continuous slashes.
    After replacement, the string will look like
    this:"one\\\\two\\\\three\\\\"

    Can this be done with the String functions available in Java-1.2?
    If yes, kindly help me how to do it.

    Thanks!
     
    qazmlp, Apr 7, 2004
    #1
    1. Advertising

  2. qazmlp

    Roedy Green Guest

    On 6 Apr 2004 22:39:13 -0700, (qazmlp) wrote
    or quoted :

    >I have a string like this: "one\\two\\three\\"


    Backslashes drive you crazy in java since you must double them in
    string literals and quadruple them in regex literals.
    http://mindprod.com/jgloss/literals.html


    I suggest solving the problem first with forward slashes.

    Then go back and flip every / to \\


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 7, 2004
    #2
    1. Advertising

  3. qazmlp

    Roedy Green Guest

    On 6 Apr 2004 22:39:13 -0700, (qazmlp) wrote
    or quoted :

    >I have a string like this: "one\\two\\three\\"
    >I want to replace 2 continuous slashes that figure in the string with
    >4 continuous slashes.
    >After replacement, the string will look like
    >this:"one\\\\two\\\\three\\\\"
    >
    >Can this be done with the String functions available in Java-1.2?


    Using just the String methods, you would create a StringBuffer, and
    scan for the //. Then, in a loop, tack on bits and pieces to your
    string buffer. When done use toString.

    This is pedestrian, but fast.

    The alternative is to use a regex. The catch here is the learning
    curve. See http://mindprod.com/jgloss/regex.html

    If you are going to do much fancy string manipulation, it is worth
    taking a day or two to play with regexes till you understand them.

    Work on simple problems and gradually work up. They are slippery
    buggers.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Apr 7, 2004
    #3
  4. qazmlp

    GV Guest

    "qazmlp" <> wrote in message
    news:...
    > I have a string like this: "one\\two\\three\\"
    > I want to replace 2 continuous slashes that figure in the string with
    > 4 continuous slashes.
    > After replacement, the string will look like
    > this:"one\\\\two\\\\three\\\\"
    >
    > Can this be done with the String functions available in Java-1.2?
    > If yes, kindly help me how to do it.
    >
    > Thanks!


    Something like this?

    import java.util.StringTokenizer;

    public class StringReplacer {

    private static String sDelim = ":";
    private String fReplacement;

    public StringReplacer(String replacement){
    if (replacement == null){
    throw new NullPointerException("replacement cannot be null");
    }
    fReplacement = replacement;
    }

    public String replace(String orig, String remove){
    String result = null;
    String delimited;
    StringBuffer buff = new StringBuffer();

    int index = orig.indexOf(remove);
    if (index > -1){
    buff.append(orig.substring(0,index));
    int begin = index + remove.length();
    index = orig.indexOf(remove, begin);
    while (index > 0){
    buff.append(sDelim).append(orig.substring(begin, index));
    begin = index + remove.length();
    index = orig.indexOf(remove, begin);
    }
    buff.append(sDelim).append(orig.substring(begin,
    orig.length()));
    delimited = buff.toString();
    } else {
    delimited = orig;
    }

    buff.delete(0, buff.length());
    StringTokenizer tokenizer = new StringTokenizer(delimited, sDelim,
    true);
    while (tokenizer.hasMoreTokens()){
    String token = tokenizer.nextToken();
    if (token.equals(sDelim)){
    token = fReplacement;
    }
    buff.append(token);
    }
    result = buff.toString();

    return result;
    }

    public static void main(String[] args) {
    String original = (args.length > 0) ? args[0] :
    "one\\two\\three\\\\";
    StringReplacer replacer = new StringReplacer("\\\\\\\\");
    String remove = "\\\\";
    String result = replacer.replace(original, remove);
    System.out.println("Result: " + result);
    }

    }
     
    GV, Apr 7, 2004
    #4
  5. GV wrote:
    > "qazmlp" <> wrote in message
    > news:...
    >
    >>I have a string like this: "one\\two\\three\\"
    >>I want to replace 2 continuous slashes that figure in the string with
    >>4 continuous slashes.
    >>After replacement, the string will look like
    >>this:"one\\\\two\\\\three\\\\"
    >>
    >>Can this be done with the String functions available in Java-1.2?
    >>If yes, kindly help me how to do it.
    >>
    >>Thanks!

    >
    >
    > Something like this?


    Just some comments here.

    > import java.util.StringTokenizer;
    >
    > public class StringReplacer {
    >
    > private static String sDelim = ":";
    > private String fReplacement;
    >
    > public StringReplacer(String replacement){
    > if (replacement == null){
    > throw new NullPointerException("replacement cannot be null");
    > }
    > fReplacement = replacement;
    > }
    >
    > public String replace(String orig, String remove){
    > String result = null;
    > String delimited;
    > StringBuffer buff = new StringBuffer();
    >
    > int index = orig.indexOf(remove);
    > if (index > -1){
    > buff.append(orig.substring(0,index));
    > int begin = index + remove.length();
    > index = orig.indexOf(remove, begin);
    > while (index > 0){
    > buff.append(sDelim).append(orig.substring(begin, index));
    > begin = index + remove.length();
    > index = orig.indexOf(remove, begin);
    > }
    > buff.append(sDelim).append(orig.substring(begin,
    > orig.length()));
    > delimited = buff.toString();
    > } else {
    > delimited = orig;
    > }
    >
    > buff.delete(0, buff.length());
    > StringTokenizer tokenizer = new StringTokenizer(delimited, sDelim,
    > true);
    > while (tokenizer.hasMoreTokens()){
    > String token = tokenizer.nextToken();
    > if (token.equals(sDelim)){
    > token = fReplacement;
    > }
    > buff.append(token);
    > }
    > result = buff.toString();
    >
    > return result;
    > }


    You are going through an awful lot of trouble here. A replacement method
    can be written as simply as (and it's faster to boot):

    public static String replaceAll(String str, String pattern, String
    replacement)
    {
    StringBuffer buff = new StringBuffer(str.length());

    // where we found pattern last time
    int index = 0;
    // the end of the last find
    int mark = 0;
    // pattern length
    int pLen = pattern.length();

    // while there are patterns to replace
    while ((index = str.indexOf(pattern, index) >= 0)
    {
    // append original head
    buff.append(str.substring(mark, index));
    // swap in the replacement
    buff.append(replacement);
    // set us up for the next iteration
    index += pLen;
    mark = index;
    }

    buff.append(str.substring(mark, str.length());

    return buff.toString();
    }
    --
    Daniel Sjöblom
    Remove _NOSPAM to reply by mail
     
    =?ISO-8859-1?Q?Daniel_Sj=F6blom?=, Apr 7, 2004
    #5
  6. qazmlp

    Michael Dunn Guest

    "qazmlp" <> wrote in message news:...
    : I have a string like this: "one\\two\\three\\"
    : I want to replace 2 continuous slashes that figure in the string with
    : 4 continuous slashes.
    : After replacement, the string will look like
    : this:"one\\\\two\\\\three\\\\"
    :
    : Can this be done with the String functions available in Java-1.2?
    : If yes, kindly help me how to do it.
    :
    : Thanks!

    for java v1.4+

    String str = "one\\\\two\\\\three\\\\";
    System.out.println("normal:- " + str);
    str = str.replaceAll("\\\\","\\\\\\\\");
    System.out.println("replaced:- " + str);
     
    Michael Dunn, Apr 7, 2004
    #6
    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. Mladen Adamovic
    Replies:
    0
    Views:
    741
    Mladen Adamovic
    Dec 4, 2003
  2. Mladen Adamovic
    Replies:
    3
    Views:
    14,620
    Mladen Adamovic
    Dec 5, 2003
  3. Dustan
    Replies:
    2
    Views:
    608
    Dustan
    May 7, 2009
  4. ridcully
    Replies:
    4
    Views:
    125
  5. Dan Wilkin
    Replies:
    1
    Views:
    263
    robic0
    Jul 17, 2006
Loading...

Share This Page