How do I swap the case of words in a String?
Author: Deron Eriksson
Description: This Java example shows how to swap the case of words in a String.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)
Swapping uppercase and lowercase characters in a String can be accomplished using the swapCase() method on the WordUtils utility class in the Commons LangS library. The SwapCaseTest class demonstrates this. SwapCaseTest.javapackage test; import java.io.IOException; import org.apache.commons.lang.WordUtils; public class SwapCaseTest { public static void main(String[] args) throws IOException { swapCase("this is a string"); swapCase("This is another string."); swapCase("HERE IS ANOTHER"); swapCase("oNE lAST sTRING"); } public static void swapCase(String string) { System.out.println(string + " | original"); System.out.println(WordUtils.swapCase(string) + " | WordUtils.swapCase(string)"); System.out.println(); } } The console output of SwapCaseTest is shown below. Resultsthis is a string | original THIS IS A STRING | WordUtils.swapCase(string) This is another string. | original tHIS IS ANOTHER STRING. | WordUtils.swapCase(string) HERE IS ANOTHER | original here is another | WordUtils.swapCase(string) oNE lAST sTRING | original One Last String | WordUtils.swapCase(string) Related Tutorials: |