String how to partially replace special char/string

J

John_Woo

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?
 
A

Alex Whitney

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;
}
 
A

Alex Whitney

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 said:
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 said:
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?
 
L

Lasse Reichstein Nielsen

John_Woo said:
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
 
S

shakah

John_Woo said:
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?

What about a simple:

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

?
 

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,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top