String replacement( 2 slashes-> 4 slashes )

Q

qazmlp

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!
 
R

Roedy Green

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.
 
G

GV

qazmlp said:
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);
}

}
 
?

=?ISO-8859-1?Q?Daniel_Sj=F6blom?=

GV said:
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();
}
 
M

Michael Dunn

: 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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top