Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 947003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:00:44+00:00 2026-05-15T23:00:44+00:00

How can i find different characters in the strings at same positions? Ex: String

  • 0

How can i find different characters in the strings at same positions? Ex:

String string1 = "Anand has 2 bags and 4 apples";
String n = /* ??? */;
String n2 = /* ??? */;
String string2 = "Anand has " + n + " bags and " + n2 + " apples";

I want n = "2" and n1 = "4".

Please let me know how we can do this? (Space added between words only for clarity purpose . But i can not use Space as delimiter)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T23:00:44+00:00Added an answer on May 15, 2026 at 11:00 pm

    You can use the StringTemplate class that i developed (I’d developed a URITemplate class to match restlike uris but have modified it to use strings as well)

      // Licensed Apache2 (http://www.apache.org/licenses/LICENSE-2.0.txt) 
      import java.util.List;
    
      import java.net.URL;
      import java.net.URLConnection;
    
      import java.util.Map;
      import java.util.ArrayList;
      import java.util.LinkedHashMap;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;
    
      /**
       * <pre>
       *    StringTemplate t = new StringTemplate("/catalog/{categoryId}/products/{productId}/summary");
       *    t.matches("/catalog/23/products/12375/summary"); // returns true
       *    t.match("/catalog/23/products/12375/summary");   // returns a map {categoryId=23, productId=12375}
       * </pre>
       * 
       * @author anaik
       */
      public class StringTemplate {
         /** The meta pattern for  template to match sequence such as: {someVar} */
         private static final Pattern patternPattern = Pattern.compile("\\{([^\\{\\}]+)\\}");
         /** The  pattern string */
         private String stringPattern;
         /** The generated pattern when the stringPattern is parsed */
         private Pattern thisStringPattern;
         /** Variable names found in this pattern in that order */
         private List<String> vars = new ArrayList<String>();
    
         /**
          * Creates a new StringTemplate from the specified pattern
          * @param Pattern
          */
         private StringTemplate(String stringPattern)  {
            this.stringPattern = stringPattern;
            initialize();
         }
    
         /**
          * Gets the names of variables - those defined in {variable-name} constructs - in this StringTemplate
          * in the order they were specified
          * @return a list of variables or an empty list if no variables were found
          */
         public List<String> getVars() {
            return vars;
         }
    
         /**
          * Determine whether the specified <tt>actualString</code> matches with this StringTemplate
          * @param actualString The actual  to match
          * @return true iff successfull match
          */
         public boolean matches(String actualString)  {
            return thisStringPattern.matcher(actualString).matches();
         }
    
         /**
          * Matches the <tt>actualString</tt> with this StringTemplate and extracts values for all the variables
          * in this template and returns them as an ordered map (keys defined in the same order as that of
          * the StringTemplate. If the match was unsuccessfull, an empty map is returned. Note that this method
          * should be ideally be called after {@link #matches(java.lang.String) } to check whether the 
          * specified actually matches the template
          */
         public Map<String, String> match(String actualString) {
            Matcher m = thisStringPattern.matcher(actualString);
            Map<String, String> map = new LinkedHashMap<String, String>();
            if(m.matches())   {
               int gc = m.groupCount();
               for(int i = 0; i < gc; i++)   {
                  int g = i + 1;
                  map.put(vars.get(i), actualString.substring(m.start(g), m.end(g)));
               }
            }
            return map;
         }
    
         private void initialize()  {
            Matcher m = patternPattern.matcher(stringPattern);
            StringBuffer builder = new StringBuffer();
    
            while(m.find())   {
               String var = m.group(1);
               vars.add(var);
               m.appendReplacement(builder, "(.*)");
            }
            m.appendTail(builder);
            String genPattern = builder.toString();
            thisStringPattern = Pattern.compile(genPattern);
         }
    
         public static void main(String[] args) throws Throwable  {
            StringTemplate t = new StringTemplate(args[0]);
            System.out.println("Matches with actual Class Identifier: java.lang.String: " + t.matches(args[1]));
            System.out.println("Var values: " + t.match(args[1]));
         }
      }
    

    Compile this and test as follows:

    tmp$ java StringTemplate "Anand has {n} bags and {n1} apples" "Anand has 23 bags and 500 apples"
    

    This is the output

     Matches with actual URI: true
     Var values: {n=23, n1=500}
    

    The matches(String) returns the map containing the template variable names and values.
    This class can be used for matching any string with any number of vars. Its liscensed apache2

    If your input string contains regex characters, you will have to escape them:

      input = input.replaceAll("\\$", "\\\\\\$");
      input = input.replaceAll("\\(", "\\\\(");
      input = input.replaceAll("\\)", "\\\\)");
      StringTemplate st = new StringTemplate(input);
    

    Note that you need more accurate regexps for conditions where input string already has characters like “\$”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to be able to check if I can edit a cell in
I've been toying around with a lot of different layout structures and can't seem
I have two series of N points I want to graph in two different
Given a set of strings, for example: EFgreen EFgrey EntireS1 EntireS2 J27RedP1 J27GreenP1 J27RedP2
How do perl strings represented internally? What encoding is used? How do I handle
I'm writing a language interpreter in C, and my string type contains a length
I have a number of tracks recorded by a GPS, which more formally can
I've got problems with special characters being inserted and displayed properly. It's an ASP.NET
Is it possible to group in jqgrid without using subgrids ? I just want
I've recently been writing some basic command-line programs (I want to keep my skills

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.