Skip to content
NPi2Loup edited this page Jul 29, 2014 · 2 revisions

StringUtil

It's a small tool that offers transformations on Strings

Examples

StringUtil.isEmpty

return True if string is null or contains only blanks characters

	boolean resultTrue = StringUtil.isEmpty(null);
	resultTrue = StringUtil.isEmpty("");
	resultTrue = StringUtil.isEmpty("  ");
	final boolean resultFalse = StringUtil.isEmpty(" abc");

StringUtil.replace

This function replaces a string fragment by another

		final String result = StringUtil.replace("azertYYuiop", "YY", "y"); //result = "azertyuiop"

StringUtil.constToCamelCase

This function transform CONST to camelCase
The last boolean argument determines if the first character in upper or lowe case.

String result = StringUtil.constToCamelCase("YELLOW_SUBMARINE", true); //==>YellowSubmarine
result = StringUtil.constToCamelCase("YELLOW_SUBMARINE_49", true); //==>YellowSubmarine49
result = StringUtil.constToCamelCase("U_S_A", true); //=>USA
/*! you can not use ambigous CONST such as PLAYER1 */

StringUtil.camelToConstCase

This function transform camelCase to CONST

String result = StringUtil.camelToConstCase("YellowSubmarine"); //==>YELLOW_SUBMARINE
result = StringUtil.camelToConstCase("YellowSubmarine49"); //==>YELLOW_SUBMARINE_49
result = StringUtil.constToCamelCase("USA", true); //=>U_S_A

StringUtil.isSimpleLetterOrDigit

This function check if a character is a letter [a..z] or [A..Z] or a digit [0..9]
All other letters will return false : any accent, +, -, , *.....

boolean resultTrue = StringUtil.isSimpleLetterOrDigit('a');
resultTrue = StringUtil.isSimpleLetterOrDigit('t');
resultTrue = StringUtil.isSimpleLetterOrDigit('B');
resultTrue = StringUtil.isSimpleLetterOrDigit('5');
//----	
final boolean resultFalse = StringUtil.isSimpleLetterOrDigit('+');

StringUtil.format

This function allow to format with single quotes
the first argument can be

  • a String
  • a stringBuilder

The two following samples return "hello world"

String result = StringUtil.format("hello world");
result = StringUtil.format("hello {0}", "world");
/*! In the following sample quotes are kept*/
result = StringUtil.format("your command number '{0}' is ...", "Z45F"); //your command number 'Z45F' is ...