Here you can find the source of toUnderscoreName(String name)
Parameter | Description |
---|---|
filteredName | the string containing original name |
public static String toUnderscoreName(String name)
//package com.java2s; //License from project: Apache License public class Main { /**//ww w.j av a 2s .c om * Convert a name in camelCase to an underscored name in lower case. * Any upper case letters are converted to lower case with a preceding underscore. * @param filteredName the string containing original name * @return the converted name */ public static String toUnderscoreName(String name) { if (name == null) return null; String filteredName = name; if (filteredName.indexOf("_") >= 0 && filteredName.equals(filteredName.toUpperCase())) { filteredName = filteredName.toLowerCase(); } if (filteredName.indexOf("_") == -1 && filteredName.equals(filteredName.toUpperCase())) { filteredName = filteredName.toLowerCase(); } StringBuffer result = new StringBuffer(); if (filteredName != null && filteredName.length() > 0) { result.append(filteredName.substring(0, 1).toLowerCase()); for (int i = 1; i < filteredName.length(); i++) { String preChart = filteredName.substring(i - 1, i); String c = filteredName.substring(i, i + 1); if (c.equals("_")) { result.append("_"); continue; } if (preChart.equals("_")) { result.append(c.toLowerCase()); continue; } if (c.matches("\\d")) { result.append(c); } else if (c.equals(c.toUpperCase())) { result.append("_"); result.append(c.toLowerCase()); } else { result.append(c); } } } return result.toString(); } }