Thursday, March 06, 2014

Replace Series of String in Java

This particular exercise will help you replacing a series of string in you data.

Typically from an example point of view you can use it to replace key value pair in a String

Lets see the sample code to understand it better.

package helloworld;

import java.util.*;

public class CollectionsTest {

public static void main(String args[]) {
String test ="Name::Arpit::LastName::Rahi::Technology::SOA::";
String Mbody= "Employee info is ::@key1@::@Key2@::@key3@::@Key4@::@key5@::@key6@::@key7@::" ;
String[] Body =Mbody.split("::");
String[] keys = test.split("::");
int i = keys.length;
//System.out.println(i);
for (int l=0;l<i;l++) {
//System.out.println(Body[l]);
//System.out.println(keys[l]);
Mbody = Mbody.replaceAll(Body[l+1],keys[l]) ;

}

Mbody=Mbody.replace("::"," ");
System.out.println(Mbody);

}
}

Here MBody is the original message content which has some dummy value that needs to be replaced with the key value which is coming in the test string.

Now if you will execute this code you will get the following result.


Employee info is Name Arpit LastName Rahi Technology SOA @key7@

Few important points to note here is that the string which i am using to split is :: , one can use the string of his/her own choice.

Also i don't want to replace the first content of the message body hence the loop is started with second element of the message body.

Now lets take another example to understand how key value can be replaced.

We will use the concept of collection to do the replacement

package helloworld;

import java.util.*;

public class CollectionsTest {

public static void main(String args[]) {
String test ="@key1@:Arpit~@key2@:Rahi~@key3@:SOA";
String Mbody= "Employee info is EmpName:@key1@ EmpLastName:@key2@ and Skills:@key3@" ;
//String[] Body =Mbody.split("::");
String[] keys = test.split("~");

for (String key : keys)
{
Mbody=Mbody.replaceAll(key.substring(0,key.indexOf(":")),key.substring(key.indexOf(":")+1));


}

System.out.println(Mbody);

}
}

Run this code and you will get following response

Employee info is EmpName:Arpit EmpLastName:Rahi and Skills:SOA


Lets go inside the for loop to understand the code in a better way

For understanding the code i have tweaked the code a little bit to get more details

package helloworld;

import java.util.*;

public class CollectionsTest {

public static void main(String args[]) {
String test ="@key1@:Arpit~@key2@:Rahi~@key3@:SOA";
String Mbody= "Employee info is EmpName:@key1@ EmpLastName:@key2@ and Skills:@key3@" ;
//String[] Body =Mbody.split("::");
String[] keys = test.split("~");

for (String key : keys)
{
System.out.println(key);
System.out.println(key.substring(0,key.indexOf(":")));
System.out.println(key.substring(key.indexOf(":")+1));
Mbody=Mbody.replaceAll(key.substring(0,key.indexOf(":")),key.substring(key.indexOf(":")+1));
}

System.out.println(Mbody);

}
}


Now if you run this code you will get the following response

@key1@:Arpit
@key1@
Arpit
@key2@:Rahi
@key2@
Rahi
@key3@:SOA
@key3@
SOA
Employee info is EmpName:Arpit EmpLastName:Rahi and Skills:SOA


ReplaceALL is an inbuild function which replace the occurrence of first string with the second one.

the syntax for the function is

replaceAll(FirstString,SecondString)

Taking one loop at a time
the key value coming is @key1@Arpit
From this key value we are trying to find the first and second string

the first string is @key1@ and the second string is @key2@

The replaceAll command tries to find the first string @key1@ in the message body and replace it with the corresponding value in second string that is Arpit.

The loop repeats for all the key and completes the replacement.

No comments: