001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.common.util;
022:
023: public class Text {
024: public static final String UNDEFiNED = "Undefined";
025:
026: public static boolean isEmpty(String s) {
027: return s == null || s.length() == 0;
028: }
029:
030: /**
031: * Es:
032: * String source = "bla1 bla3";
033: *
034: * insertText(source, "bla2 ", 5);
035: *
036: * return -> "bla1 bla2 bla3"
037: */
038: public static String insertText(String source, String text,
039: int index) {
040: return left(source, index) + text + source.substring(index);
041: }
042:
043: public static String left(String source, int len) {
044: return source.substring(0, len);
045: }
046:
047: /**
048: * Es:
049: * String source = "bla1 bla2 bla3";
050: *
051: * right(source, 5);
052: *
053: * return -> " bla3"
054: */
055: public static String right(String source, int len) {
056: return source.substring(source.length() - len);
057: }
058:
059: /**
060: * Es:
061: * String source = "bla1 bla2 bla3";
062: *
063: * replaceText(source, "bl", "cr");
064: *
065: * return -> "cra1 cra2 cra3"
066: */
067: public static String replaceText(String source, String find,
068: String replace) {
069: for (int index = source.indexOf(find); index != -1; index = source
070: .indexOf(find, index)) {
071: source = replaceText(source, replace, index, find.length());
072: index = index + replace.length();
073: }
074:
075: return source;
076: }
077:
078: /**
079: * Es:
080: * String source = "bla1 bla2 bla3";
081: *
082: * replaceText(source, "cra", 5);
083: *
084: * return -> "bla1 cra"
085: */
086: public static String replaceText(String source, String text,
087: int start) {
088: return left(source, start) + text;
089: }
090:
091: /**
092: * Es:
093: * String source = "bla1 bla2 bla3";
094: *
095: * replaceText(source, "cr", 5, 7);
096: *
097: * return -> "bla1 cra3"
098: */
099: public static String replaceText(String source, String text,
100: int start, int len) {
101: return left(source, start) + text
102: + source.substring(start + len);
103: }
104:
105: public static String trimLeft(String source) {
106: while (source.startsWith(" ")) {
107: source = source.substring(1);
108: }
109:
110: return source;
111: }
112:
113: public static String trimRight(String source) {
114: while (source.endsWith(" ")) {
115: source = left(source, source.length() - 1);
116: }
117:
118: return source;
119: }
120:
121: public static String wrap(String s, int len) {
122: StringBuffer sb = new StringBuffer();
123: while (s.length() > len) {
124: sb.append(s.substring(0, len) + "\n");
125: s = s.substring(len);
126: }
127: sb.append(s);
128:
129: return sb.toString();
130: }
131: }
|