commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Grzegorz Błaszczyk <d...@bankier.pl>
Subject New methods: camelCaseToUnderscoreSeparated(String, boolean) and underscoreSeparatedToCamelCase(String)
Date Sun, 02 Mar 2008 12:14:13 GMT
Hello,

I would like to add two new static methods for handling camel scape and 
underscore separated strings to org.apache.commons.lang.StringUtils 
class. Please give me a hand how to add it to the repository (commit to 
some branch or something) without breaking anything else (code below).

Grzegorz Blaszczyk


public static String camelCaseToUnderscoreSeparated(String name,
			boolean toLowerCase) {
		StringBuilder s = new StringBuilder();
		if (name == null) {
			return "";
		}
		int length = name.length();
		for (int i = 0; i < length; i++) {
			char ch = name.charAt(i);
			if (Character.isUpperCase(ch) && i > 0) {
				s.append("_");
			}
			if (ch == '.') {
				s.append("_");
			} else {
				s.append(toLowerCase ? Character.toLowerCase(ch) : Character
						.toUpperCase(ch));
			}
		}
		return s.toString();
}
	
public static String underscoreSeparatedToCamelCase(String name) {
		StringBuilder s = new StringBuilder();
		if (name == null) {
			return "";
		}
		int length = name.length();
		boolean upperCase = false;
		
		for (int i = 0; i < length; i++) {
			char ch = name.charAt(i);
			if (ch == '_') {
				upperCase = true;
			} else if (upperCase) {
				s.append(Character.toUpperCase(ch));
				upperCase = false;
			} else {
				s.append(ch);
			}
		}
		return s.toString();
}

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


Mime
View raw message