Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Locale;

public class Main {
    public static String camelCaseToDash(String string) {
        StringBuilder sb = new StringBuilder(2 * string.length());
        boolean prevLowerCase = false, prevIsAlpha = false;
        for (int i = 0; i < string.length(); ++i) {
            boolean nextLowerCase = i < string.length() - 1 ? Character.isLowerCase(string.charAt(i + 1)) : false;
            char c = string.charAt(i);
            if (Character.isUpperCase(c)) {
                if ((prevLowerCase || nextLowerCase) && prevIsAlpha)
                    sb.append('-');
                sb.append(String.valueOf(c).toLowerCase(Locale.ENGLISH));
            } else if (c == '.') {
                sb.append('-');
            } else {
                sb.append(c);
            }
            prevLowerCase = Character.isLowerCase(c);
            prevIsAlpha = Character.isAlphabetic(c);
        }
        return sb.toString();
    }
}