Solved

how to make replacement in a string

Posted on 2011-03-16
8
283 Views
Last Modified: 2012-05-11
Hi,
I have a string in Java and I would like to add "m" to the beginning of all instances of these numbers in the string. (numbers can be either 5, 6 or 7 digits)

e.g.
System.out.println(java.lang.String list); 

'12345, 123456, 1234567, 333333, 444444'

Open in new window


Now I want to make list:

System.out.println(java.lang.String list);

'm12345,m123456,m1234567,m333333,m444444'

Open in new window


How can I make it?

Thanks,
0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 35153583
you can say

String [] s1 = s.split(",");

String s0 = "";

for(int j=0; j<s1.length; j++)s0+= "m" + s1[j] + ",";

s0 = s0.substring(0,s0.length()-1);
0
 
LVL 92

Expert Comment

by:objects
ID: 35153584
String[] numbers = s.split(",");
StringBuilder sb = new StringBuilder();
for (String number : numbers) {
   sb.append('m');
   sb.append(number);
   sb.append(',');
}
String newstring = sb.substring(0, sb.length()-1).toString();
0
 
LVL 47

Expert Comment

by:for_yan
ID: 35153586


String [] s1 = s.split(",");

String s0 = "";

for(int j=0; j<s1.length; j++)s0+= "m" + s1[j] .trim()+ ",";

s0 = s0.substring(0,s0.length()-1);
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Assisted Solution

by:JoeNuvo
JoeNuvo earned 50 total points
ID: 35153587
if the pattern of data always exact as your given
(seperate by comma and one space)

you can do   list =  "m" + list.replace(", " , ",m");
0
 
LVL 24

Assisted Solution

by:jimyX
jimyX earned 50 total points
ID: 35153610
You can use ReplaceAll and add the new char "m" at the beginning to your string, as follows:

YourString = "m" + YourString.replaceall(" ","m");
0
 
LVL 35

Accepted Solution

by:
mccarl earned 400 total points
ID: 35153631
And just another way to skin the cat...

list = list.replaceAll("(\\d+)", "m$1");

If you also did what to explicitly remove the whitespace between the comma, as your above example shows, then it is a simple extension to the above...

list = list.replaceAll("\\s*(\\d+)", "m$1");

I know, some people may say that using regular expressions can be overkill, or have less performance than other methods, but if it fits for your particular situation, I say use them, it certainly makes it a lot more simple, and once you learn a bit of regex syntax, I think it makes it more clear as to your intentions... The above is quite clearly saying, replace all groups of one or more digits with an m followed by those digits.

Also, it allows you to extend the above to make it as strict as you like, etc.
0
 
LVL 24

Expert Comment

by:jimyX
ID: 35153894
@ Tolgar
Please ignore my post.

@ JoeNuvo
Next time I will be faster :-)
0
 

Author Closing Comment

by:Tolgar
ID: 35156155
One line short suggestions are generally better than other approaches which include loops in them. That's why I picked these three suggestions. And regexp one is more clear as mentioned.
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question