Solved

how to make replacement in a string

Posted on 2011-03-16
8
277 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example issue 6 46
check java version using powershell 13 182
How to determine if a string is a valid SHA value 7 36
Java class and jar 3 25
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

821 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