import java.util.*; //-- //import InsertText; /** * Provides one or two static methods dealing with * blankness in pieces of text. * * @author http://bumble.sf.net */ public class BlankText extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private String text; //-------------------------------------------- public BlankText() { this.text = ""; } //-------------------------------------------- public BlankText(String sText) { this.text = sText; } //-------------------------------------------- /** what is a newline character is tricky */ public static boolean lastLineIsBlank(String sText) { char cLast; if (sText.length() == 0) { return false; } if (!sText.endsWith(NEWLINE)) { return false; } sText = EndText.removeSuffix(sText, NEWLINE); while (sText.length() > 0) { if (sText.endsWith(NEWLINE)) { return true; } cLast = sText.charAt(sText.length() - 1); if (!Character.isWhitespace(cLast)) { return false; } sText = EndText.removeLast(sText); } //-- while return false; } //-------------------------------------------- public static boolean lastCharacterIsBlank(String sText) { if (sText.length() == 0) { return false; } char cLast; cLast = sText.charAt(sText.length() - 1); if (Character.isWhitespace(cLast)) { return true; } else { return false; } } //-------------------------------------------- public static boolean firstCharacterIsBlank(String sText) { char cFirst; cFirst = sText.charAt(0); if (Character.isWhitespace(cFirst)) { return true; } else { return false; } } //-------------------------------------------- /** makes some space characters visible */ public static String displayBlanks(String sText) { char cCurrent; StringBuffer sbReturn = new StringBuffer(""); sbReturn.append(NEWLINE); sbReturn.append(""); for (int ii = 0; ii < sText.length(); ii++) { cCurrent = sText.charAt(ii); if (cCurrent == '!') { sbReturn.append("\\n"); sbReturn.append(cCurrent); } else if (cCurrent == '!') { sbReturn.append("\\r"); sbReturn.append(cCurrent); } else if (cCurrent == ' ') { sbReturn.append("."); } else if (cCurrent == '\t') { sbReturn.append(","); } else { sbReturn.append(cCurrent); } } //-- for sbReturn.append(""); return sbReturn.toString(); } //-- m: displayBlanks //-------------------------------------------- /** replaces a character in a string with a string */ public static String replace(String sText, char cOld, String sNew) { char cCurrent; StringBuffer sbReturn = new StringBuffer(""); if (sText.indexOf(cOld) == -1) { return sText; } for (int ii = 0; ii < sText.length(); ii++) { cCurrent = sText.charAt(ii); if (cCurrent == cOld) { sbReturn.append(sNew); } else { sbReturn.append(cCurrent); } } //-- for return sbReturn.toString(); } //-- method: replace //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java BlankText ."); sbUsageMessage.append(NEWLINE); if (args.length == 0) { System.out.println(sbUsageMessage); System.exit(-1); } StringBuffer sbText = new StringBuffer(""); sbText.append("This is the first line "); sbText.append(NEWLINE); sbText.append("This is the second line "); sbText.append(NEWLINE); sbText.append(" more writing here "); sbText.append(NEWLINE); sbText.append(" "); sbText.append(NEWLINE); char cReplace = args[0].charAt(0); String sNew = args[1]; System.out.println("Using text:"); System.out.println(sbText); System.out.println(""); System.out.print(".displayBlanks() >"); System.out.println(""); //System.out.println(BlankText.displayBlanks(sbText.toString())); System.out.print(".lastLineIsBlank() >"); System.out.println(BlankText.lastLineIsBlank(sbText.toString())); System.out.print(".firstCharacterIsBlank() >"); System.out.println(BlankText.firstCharacterIsBlank(sbText.toString())); System.out.print(".lastCharacterIsBlank() >"); System.out.println(BlankText.lastCharacterIsBlank(sbText.toString())); } //-- main() } //-- BlankText class