Solved

how to make replacement in a string

Posted on 2011-03-16
8
287 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
Independent Software Vendors: 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 36

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

691 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