Here you can find the source of toIdentifier(String str)
Parameter | Description |
---|---|
name | a parameter |
public static String toIdentifier(String str)
//package com.java2s; /* Copyright (C) 2009 Mobile Sorcery AB // w ww . j ava 2 s . c o m This program is free software; you can redistribute it and/or modify it under the terms of the Eclipse Public License v1.0. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Eclipse Public License v1.0 for more details. You should have received a copy of the Eclipse Public License v1.0 along with this program. It is also available at http://www.eclipse.org/legal/epl-v10.html */ public class Main { /** * Given a string, returns a proper C identifier. * @param name * @return */ public static String toIdentifier(String str) { // C = Java identifiers :) StringBuffer buf = new StringBuffer(); if (!str.isEmpty()) { char first = str.charAt(0); if (Character.isJavaIdentifierPart(first) && !Character.isJavaIdentifierStart(first)) { buf.append("_"); } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isJavaIdentifierPart(ch)) { buf.append(Character.toUpperCase(ch)); } else { buf.append("_"); } } } return buf.toString(); } public static boolean isEmpty(String text) { return text == null || text.isEmpty(); } }