Solved

Replace Substings with Substrings

Posted on 2007-04-01
6
207 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
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
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.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
Tomcat: Unable to run tomcat service. 2 23
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 57
printf performancy 11 69
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

860 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