java驼峰转下划线

 private String underscoreName(String name)
  {
    StringBuilder result = new StringBuilder();
    if ((name != null) && (name.length() > 0)) {
      result.append(name.substring(0, 1).toLowerCase());
      for (int i = 1; i < name.length(); ++i) {
        String s = name.substring(i, i + 1);
        if (s.equals(s.toUpperCase())) {
          result.append("_");
          result.append(s.toLowerCase());
        }
        else {
          result.append(s);
        }
      }
    }
    return result.toString();
  }

 

 

 

private static String trans(String str) {
		List record = new ArrayList();
		for (int i = 0; i < str.length(); i++) {
			char tmp = str.charAt(i);
			if ((tmp <= 'Z') && (tmp >= 'A')) {
				record.add(i);// 记录每个大写字母的位置
			}
		}
		if (null == record || record.size() == 0) {
			return str;
		}
		str = str.toLowerCase();
		char[] charofstr = str.toCharArray();
		String[] t = new String[record.size()];
		for (int i = 0; i < record.size(); i++) {
			t[i] = "_" + charofstr[(Integer) record.get(i)];
		}
		String result = "";
		int flag = 0;
		for (int i = 0; i < str.length(); i++) {
			if ((flag < record.size()) && (i == (Integer) record.get(flag))) {
				result += t[flag];
				flag++;
			} else
				result += charofstr[i];
		}
		return result;
	}

 

你可能感兴趣的:(java驼峰转下划线)