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

  • SEARCH
  • Home
  • 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 1072991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:56:01+00:00 2026-05-16T20:56:01+00:00

What is the best way to approach translating this code into Java? (the section

  • 0

What is the best way to approach translating this code into Java?
(the section called Convert Alpha-numeric Phone Number to All Numeric)
http://lawrence.ecorp.net/inet/samples/regexp-format.php

Since Java doesn’t have a lambda yet… what is the best approach for the String.replace ?

  • 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-16T20:56:01+00:00Added an answer on May 16, 2026 at 8:56 pm

    Wow, the original code is a bit over-verbose. I’d do:

    return phoneStr.replace(/[a-zA-Z]/g, function(m) {
        var ix= m[0].toLowerCase().charCodeAt(0)-0x61; // ASCII 'a'
        return '22233344455566677778889999'.charAt(ix);
    });
    

    And consequently in Java, something like:

    StringBuffer b= new StringBuffer();
    Matcher m= Pattern.compile("[A-Za-z]").matcher(phoneStr);
    while (m.find()) {
        int ix= (int) (m.group(0).toLowerCase().charAt(0)-'a');
        m.appendReplacement(b, "22233344455566677778889999".substring(ix, ix+1));
    }
    m.appendTail(b);
    return b.toString();
    

    Replacing with Java’s Matcher is clumsy enough that you might just prefer to use an array for this case:

    char[] c= phoneStr.toLowerCase().toCharArray();
    for (int i= 0; i<c.length; i++)
        if (c[i]>='a' && c[i]<='z')
            c[i]= "22233344455566677778889999".charAt((int) (c[i]-'a'));
    return new String(c);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering the best way to approach this. I have a JavaScript file in
I'm not sure what the best way to approach this is. I have a
I'm trying to figure out the best way to approach this. I have a
wondering about the best way to approach this particular problem and if any libraries
I was wondering what the best way to approach this problem in ASP.NET MVC
What's the best way to approach this problem: I have a list of navigation
Maybe this isn't the best way to approach this... Working on an app that
I'm wondering what the best way to approach this problem is... I have an
I'm stuck trying to work out the best possible way to approach this. Basically,
The best way I think to explain this is to tell you what my

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.