Java Java String Format toJavaName(String name)

Here you can find the source of toJavaName(String name)

Description

Replace all illegal Java chars by '_'.

License

Open Source License

Declaration

public static String toJavaName(String name) 

Method Source Code

//package com.java2s;
/*//from  w  ww .  j  a v  a2s .  com
 * Copyright (c) 2007, 2014, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

public class Main {
    /**
     * Replace all illegal Java chars by '_'.
     */
    public static String toJavaName(String name) {
        if (name.length() > 0) {
            final StringBuffer result = new StringBuffer();

            char ch = name.charAt(0);
            result.append(Character.isJavaIdentifierStart(ch) ? ch : '_');

            final int n = name.length();
            for (int i = 1; i < n; i++) {
                ch = name.charAt(i);
                result.append(Character.isJavaIdentifierPart(ch) ? ch : '_');
            }
            return result.toString();
        }
        return name;
    }
}

Related

  1. toJavaImplClassName(String podName, String typeName)
  2. toJavaImplSig(String jsig)
  3. toJavaMethodName(String sqlNotation)
  4. toJavaMethodName(String... words)
  5. toJavaName(String name)
  6. toJavaPackageName(String name)
  7. toJavaPath(String path)
  8. toJavaPath(String path)
  9. toJavaPattern(String pattern)