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 8672467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:12:41+00:00 2026-06-12T19:12:41+00:00

For a code generation tool I’m working on, I need to take a string

  • 0

For a code generation tool I’m working on, I need to take a string and generate a valid java variable name from it, but I’m not sure about the best way to do it.

For example:

"123 this is some message !" => _123_this_is_some_message ( or something similar)

Thanks

  • 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-06-12T19:12:42+00:00Added an answer on June 12, 2026 at 7:12 pm

    Assuming you replace all invalid characters by _ something like the code below could work (rough example). You might want to add some logic for name collisions etc. It is based on the JLS #3.8:

    An identifier is an unlimited-length sequence of Java letters and Java digits, the first of which must be a Java letter.
    […]
    A “Java letter” is a character for which the method Character.isJavaIdentifierStart(int) returns true.
    A “Java letter-or-digit” is a character for which the method Character.isJavaIdentifierPart(int) returns true.

    public static void main(String[] args) {
        String s = "123 sdkjh s;sdlkjh d";
        StringBuilder sb = new StringBuilder();
        if(!Character.isJavaIdentifierStart(s.charAt(0))) {
            sb.append("_");
        }
        for (char c : s.toCharArray()) {
            if(!Character.isJavaIdentifierPart(c)) {
                sb.append("_");
            } else {
                sb.append(c);
            }
        }
    
        System.out.println(sb);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a tool for rapid code generation from <DockPanel DockPanel.Dock=Top><TextBlock Text= /><TextBox Style={StaticResource
I am writing a code generation tool that will take in a XSD file
I'm working on a code generation tool where the output is display in a
Please suggest me any opensource tool to generate C# code from UML designer,Or any
Working on a code generation tool to help creating boiler code for our project.
I'm writing a simple code generation application to build POCO's from a DB2 database
I am working with NHibernate, and a few code generation tools. MyGeneration is one
I am writing a code generation tool that frequently will have lines like StringBuilder
Is it possible to configure Hibernate reverse engineering and code generation tool in such
What are the leading frameworks for java code generation? I am not looking for

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.