Solved

how to make replacement in a string

Posted on 2011-03-16
8
253 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now