Solved

Replace Substings with Substrings

Posted on 2007-04-01
6
203 Views
Last Modified: 2010-03-31
With the following code below I can replace all the A's with T1.
But I am not sure how to replace B with T2 C with T3 at the same time.

String Text="AACB";
String output="";

String patternStrA = "A";
String replacementStrA = "T1";
   
// Compile regular expression
Pattern patternA = Pattern.compile(patternStrA);

// Replace all occurrences of pattern in input
Matcher matcherA = patternA.matcher(Text);

output = matcherA.replaceAll(replacementStrA);
 System.out.println(output);
0
Comment
Question by:mr_usmannaeem
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:keyurkarnik
Comment Utility
First - Do not  use pattern-matcher for this - they are expensive to use if you are not going to compile and keep them for future use. Here you need to check it once or twice at max, and you will need to create 26 or 52 matchers depending on the english alphabet and case sensitive behaviour.

You can do this as follows :

1. Parse the string characterwisein a for loop
2. Take every character, and get its ascii value
3. Subtract the ascii value from the ascii value of 'A'
4. Take the result and add 1 to it.
5. Now append the result to 'T', and append the resulting string to a new string (sue StringBuffer)

At the end of the loop you would have the full new string


0
 
LVL 12

Expert Comment

by:keyurkarnik
Comment Utility
Experts - please do not post any code here
0
 

Author Comment

by:mr_usmannaeem
Comment Utility
sample code would be helpful....please
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 12

Expert Comment

by:keyurkarnik
Comment Utility
tell you what -
write in some code according to the algo - i will hep modify it :)
0
 

Author Comment

by:mr_usmannaeem
Comment Utility
String Text="AACB";
String output="";

for(int i=0;i<Text.length();i++){
StringBuilder buf = new StringBuilder();

char temp=Text.charAt(i);
      
// I dont get how to the ascii part, but understood the structure of your algo
      
   buf.append(temp);
   output=buf.toString();
}
System.out.println(output);

Thanks for your help
0
 
LVL 12

Accepted Solution

by:
keyurkarnik earned 75 total points
Comment Utility
There is a problem here -
You have initialized buf every time inside the for loop - so you will get only the last character.
Same case with output



Here is what you need


String Text = "AACB";
      String output = "";
      char A = 'A';
     
      StringBuilder buf = new StringBuilder();
     
      for (int i = 0; i < Text.length(); i++)
      {
         

         int temp = Text.charAt(i) - A + 1;

         //       I dont get how to the ascii part, but understood the structure of your algo

         buf.append('T').append(temp);
      }
      output = buf.toString();
      System.out.println(output);

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

10 Experts available now in Live!

Get 1:1 Help Now