Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to make replacement in a string

Posted on 2011-03-16
8
Medium Priority
?
299 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 200 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 200 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 1600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

604 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