Here you can find the source of formatWords(String words, String delimiter, boolean isHeaderUpperCase)
Parameter | Description |
---|---|
words | string list of words |
delimiter | delimiter |
isHeaderUpperCase | first letter is in upper case if true otherwise first letter is in lower case |
public static String formatWords(String words, String delimiter, boolean isHeaderUpperCase)
//package com.java2s; //License from project: LGPL import java.util.ArrayList; import java.util.List; public class Main { /**/* w ww . java 2 s .co m*/ * Formats a string list of words delimited to a camel forat: XxxxXxxxXxxx or xxxxXxxxXxxx * * @param words * string list of words * @param delimiter * delimiter * @param isHeaderUpperCase * first letter is in upper case if true otherwise first letter is in lower case * @return string of formatting result */ public static String formatWords(String words, String delimiter, boolean isHeaderUpperCase) { if (isNullOrEmpty(words)) { return words; } List<String> listWords = parseStringList(words, delimiter); return formatWords(listWords, isHeaderUpperCase); } /** * Formats a list of words delimited to a camel forat: XxxxXxxxXxxx or xxxxXxxxXxxx * * @param listWords * list of words * @param isHeaderUpperCase * first letter is in upper case if true otherwise first letter is in lower case * @return string of formatting result */ public static String formatWords(List<String> listWords, boolean isHeaderUpperCase) { StringBuffer sb = new StringBuffer(); for (String word : listWords) { sb.append(formatWord(word)); } String result = sb.toString(); if (!isHeaderUpperCase) { result = result.substring(0, 1).toLowerCase() + result.substring(1); } return result; } /** * Checks whether str is null or empty. (Do trim before checking). * * @param str * string to check * @return true if str is null or "" after trim. */ public static boolean isNullOrEmpty(String str) { return isNullOrEmpty(str, true); } /** * Checks whether str is null or empty. Do trim before checking if doTrim is true. * * @param str * string to check * @param doTrim * whether do trim before checking * @return true if str is null or "" */ public static boolean isNullOrEmpty(String str, boolean doTrim) { if (str == null) { return true; } else { if (doTrim) { return str.trim().equals(""); } return str.equals(""); } } /** * Parses a string delimited by delimiter into a list of string element. <br> * * (Do trim for each string element). <br> * * (Remove empty string element). <br> * * For example, stringList = "a,b,c , d, , e" delimiter = "," returns a list: [a,c,b,c,e] * * @param stringList * string to be parsed * @param delimiter * delimiter to parse * @return list of string element */ public static List<String> parseStringList(String stringList, String delimiter) { return parseStringList(stringList, delimiter, true); } /** * Parses a string delimited by delimiter into a list of string element.<br> * * (Do trim for each string element if isTrim is true). <br> * * (Remove empty string element). <br> * * For example, stringList = "a,b,c , d, , e" delimiter = "," returns a list: [a,c,b,c,e] * * @param stringList * string to be parsed * @param delimiter * delimiter to parse * @param isTrim * whether do trim for each string element * @return list of string element */ public static List<String> parseStringList(String stringList, String delimiter, boolean isTrim) { return parseStringList(stringList, delimiter, isTrim, true); } /** * Parses a string delimited by delimiter into a list of string element.<br> * * (Do trim for each string element if isTrim is true). <br> * * (Remove empty string element if isRemoveEmpty is true).<br> * * For example, stringList = "a,b,c , d, , e" delimiter = "," returns a list: [a,c,b,c,e] * * @param stringList * string to be parsed * @param delimiter * delimiter to parse * @param isTrim * whether do trim for each string element * @param isRemoveEmpty * whether remove empty string element * @return list of string element */ public static List<String> parseStringList(String stringList, String delimiter, boolean isTrim, boolean isRemoveEmpty) { String temp = stringList; List<String> result = new ArrayList<String>(); if (isNullOrEmpty(temp, false)) { return result; } if (isNullOrEmpty(delimiter, false)) { result.add(temp); return result; } if (isTrim) { while (temp.indexOf(delimiter) != -1) { result.add(temp.substring(0, temp.indexOf(delimiter)).trim()); temp = temp.substring(temp.indexOf(delimiter) + delimiter.length(), temp.length()); } result.add(temp.trim()); } else { while (temp.indexOf(delimiter) != -1) { result.add(temp.substring(0, temp.indexOf(delimiter))); temp = temp.substring(temp.indexOf(delimiter) + delimiter.length(), temp.length()); } result.add(temp); } if (isRemoveEmpty) { for (int i = 0; i < result.size(); i++) { if (result.get(i).equals("")) { result.remove(i); i--; } } } return result; } /** * Formats a single word to a format: Xxxxx First letter is upper case and followed by letters in lower case. * * @param word * word to format * @return word after formatting */ public static String formatWord(String word) { if (isNullOrEmpty(word)) { return word; } else { return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); } } }