Solved

Replace Substings with Substrings

Posted on 2007-04-01
6
208 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSV file parsing thru Java 13 39
jmss example java 2 32
How to configure empty element in XML Document parser? 15 44
jmeter usage 4 35
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 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