Solved

Replace Substings with Substrings

Posted on 2007-04-01
6
210 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
[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
  • 4
  • 2
6 Comments
 
LVL 12

Expert Comment

by:keyurkarnik
ID: 18834491
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
ID: 18834492
Experts - please do not post any code here
0
 

Author Comment

by:mr_usmannaeem
ID: 18835251
sample code would be helpful....please
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 12

Expert Comment

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

Author Comment

by:mr_usmannaeem
ID: 18835315
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
ID: 18835379
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

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…
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

728 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