import java.net.*; import java.io.*; import java.util.*; /** * Provides one or two static methods to alter text * in a string. * * @author http://bumble.sf.net */ public class BetweenText extends Object { //-------------------------------------------- private static String NEWLINE = System.getProperty("line.separator"); //-------------------------------------------- private String text; //-------------------------------------------- public BetweenText() { this.text = ""; } //-------------------------------------------- public BetweenText(String sText) { this.text = sText; } //-------------------------------------------- /** remove text between two characters. if the characters are * the same, nothing is done. The characters themselves are * not removed */ public static String removeBetween(String sText, char cStart, char cEnd) { char cCurrent; boolean bIsBetween = false; StringBuffer sbReturn = new StringBuffer(""); if (cStart == cEnd) { return sText; } if (sText.indexOf(cStart) == -1) { return sText; } if (sText.indexOf(cEnd) == -1) { return sText; } for (int ii = 0; ii < sText.length(); ii++) { cCurrent = sText.charAt(ii); if (cCurrent == cEnd) { bIsBetween = false; } if (!bIsBetween) { sbReturn.append(cCurrent); } if (cCurrent == cStart) { //-- if there are no following end characters, //-- do not start removing if (sText.indexOf(cEnd, ii) != -1) { bIsBetween = true; } } } //-- for return sbReturn.toString(); } //-- method: removeBetween //-------------------------------------------- /** remove text between two characters. if the characters are * the same, nothing is done. The characters themselves are * removed */ public static String removeBetweenIncluding(String sText, char cStart, char cEnd) { char cCurrent; boolean bIsBetween = false; StringBuffer sbReturn = new StringBuffer(""); if (cStart == cEnd) { return sText; } if (sText.indexOf(cStart) == -1) { return sText; } if (sText.indexOf(cEnd) == -1) { return sText; } for (int ii = 0; ii < sText.length(); ii++) { cCurrent = sText.charAt(ii); if (cCurrent == cStart) { //-- if there are no following end characters, //-- do not start removing if (sText.indexOf(cEnd, ii) != -1) { bIsBetween = true; } } if (!bIsBetween) { sbReturn.append(cCurrent); } if (cCurrent == cEnd) { bIsBetween = false; } } //-- for return sbReturn.toString(); } //-- method: removeBetween //-------------------------------------------- /** a main method for testing */ public static void main(String[] args) throws Exception { StringBuffer sbUsageMessage = new StringBuffer(""); sbUsageMessage.append("test usage: java BetweenText startChar endChar"); sbUsageMessage.append(NEWLINE); StringBuffer sbMessage = new StringBuffer(""); if (args.length < 2) { System.out.println(sbUsageMessage); System.exit(-1); } String sText = "abcdefghijklmnopqrst abcdefg 876543 "; char cStart = args[0].charAt(0); char cEnd = args[1].charAt(0); System.out.print("Using text >"); System.out.println(sText); System.out.println(""); System.out.print(".removeBetween() >"); System.out.println(BetweenText.removeBetween(sText, cStart, cEnd)); System.out.print(".removeBetweenIncluding() >"); System.out.println(BetweenText.removeBetweenIncluding(sText, cStart, cEnd)); } //-- main() } //-- BetweenText class