Java Utililty Methods Java String Format

List of utility methods to do Java String Format

Description

The list of methods to do Java String Format are organized into topic(s).

Method

StringtoJavaIdentifier(String s)
to Java Identifier
if (s == null || s.length() == 0)
    return "V";
if (!Character.isJavaIdentifierStart(s.charAt(0)))
    s = "V" + s;
char[] ca = s.toCharArray();
for (int i = 1; i < ca.length; i++)
    if (!Character.isJavaIdentifierPart(ca[i]))
        ca[i] = '_';
...
StringtoJavaIdentifier(String source)
to Java Identifier
String result = source.replaceAll(" ", "_");
result = result.replaceAll("[)(*&^%$#@!\"':;<>?,./-]", "_");
return result;
StringtoJavaIdentifier(String str)
to Java Identifier
if (str == null || str.trim().equals(""))
    return null;
StringBuffer buf = new StringBuffer(str);
int bufIdx = 0;
while (bufIdx < buf.length()) {
    char c = buf.charAt(bufIdx);
    int tilded = TILDED_CHARS.indexOf(c);
    if (tilded != -1 && tilded < NON_TILDED_CHARS.length()) {
...
StringtoJavaIdentifier(String str)
Converts the given string to a Java valid identifier.
if (str == null || str.trim().equals(""))
    return null;
StringBuffer buf = new StringBuffer(str);
int bufIdx = 0;
while (bufIdx < buf.length()) {
    char c = buf.charAt(bufIdx);
    int tilded = TILDED_CHARS.indexOf(c);
    if (tilded != -1 && tilded < NON_TILDED_CHARS.length()) {
...
StringtoJavaIdentifier(String str, String repl)
Converts str to a Java identifier by replacing any illegal characters with repl .
StringBuilder buffer = new StringBuilder();
for (char c : str.toCharArray()) {
    boolean empty = buffer.length() == 0;
    if ((empty && Character.isJavaIdentifierStart(c)) || (!empty && Character.isJavaIdentifierPart(c))) {
        buffer.append(c);
    } else {
        buffer.append(repl);
return buffer.toString();
StringtoJavaIdentifier(String text, Boolean uppercaseFirst)
Think to use JavaConventions.validateXXX() instead of this one when possible
boolean isStart = true;
boolean nextIsUpperCase = true;
StringBuffer res = new StringBuffer();
for (char c : text.toCharArray()) {
    boolean isValid = false;
    if (isStart) {
        isValid = Character.isJavaIdentifierStart(c);
    } else {
...
StringtoJavaIdentifierCamelCase(String tmp)
to Java Identifier Camel Case
StringBuilder builder = new StringBuilder(tmp.length());
boolean mustUpperCase = true;
for (int i = 0; i < tmp.length(); i++) {
    char c = tmp.charAt(i);
    if (c == '_') {
        mustUpperCase = true;
    } else if (Character.isJavaIdentifierPart(c)) {
        if (mustUpperCase) {
...
StringtoJavaIdentifierString(String className)
to Java Identifier String
int[] codePoints = className.codePoints().map(c -> Character.isJavaIdentifierPart(c) ? c : '_').toArray();
return new String(codePoints, 0, codePoints.length);
StringtoJavaImplClassName(String podName, String typeName)
Given a Fantom qname, get the Java implementation class name: sys::Obj => fan.sys.FanObj sys::Float => fan.sys.FanFloat sys::Obj => fan.sys.FanObj
if (podName.equals("sys")) {
    switch (typeName.charAt(0)) {
    case 'B':
        if (typeName.equals("Bool"))
            return "fan.sys.FanBool";
        break;
    case 'D':
        if (typeName.equals("Decimal"))
...
StringtoJavaImplSig(String jsig)
Given a Java type signature, return the implementation class signature for methods and fields: java/lang/Object => fan/sys/FanObj java/lang/Long => fan/sys/FanInt Anything returns itself.
if (jsig.length() == 1) {
    switch (jsig.charAt(0)) {
    case 'Z':
        return "fan/sys/FanBool";
    case 'J':
        return "fan/sys/FanInt";
    case 'D':
        return "fan/sys/FanFloat";
...