001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.util;
022:
023: /**
024: * @sharpen.ignore
025: */
026: public class Strings {
027:
028: private static final char DATESEPARATOR = '-';
029: private static final char TIMESEPARATOR = ':';
030: private static final char DATETIMESEPARATOR = ' ';
031:
032: private String _string;
033:
034: private Strings() {
035: }
036:
037: public Strings(String str) {
038: this ();
039: _string = str;
040: }
041:
042: public Strings(char a_char, int a_count) {
043: char[] chars = new char[a_count];
044: for (int i = 0; i < a_count; chars[i++] = a_char)
045: ;
046: _string = new String(chars);
047: }
048:
049: public Strings(int i) {
050: _string = "" + i;
051: }
052:
053: public Strings(long l) {
054: _string = "" + l;
055: }
056:
057: public Strings(char c) {
058: _string = "" + c;
059: }
060:
061: public void clear() {
062: _string = "";
063: }
064:
065: public String getString() {
066: return _string;
067: }
068:
069: protected String joinDate(String year, String month, String day) {
070: _string = year + DATESEPARATOR + month + DATESEPARATOR + day;
071: return _string;
072: }
073:
074: protected String joinTime(String hours, String minutes,
075: String seconds) {
076: _string = hours + TIMESEPARATOR + minutes + TIMESEPARATOR
077: + seconds;
078: return _string;
079: }
080:
081: protected String joinDateTime(String date, String time) {
082: _string = date + DATETIMESEPARATOR + time;
083: return _string;
084: }
085:
086: public static String _left(String a_String, int a_chars) {
087: return new Strings(a_String).left(a_chars);
088: }
089:
090: public String left(int a_chars) {
091: if (a_chars > _string.length()) {
092: a_chars = _string.length();
093: } else {
094: if (a_chars < 0) {
095: a_chars = 0;
096: }
097: }
098: return _string.substring(0, a_chars);
099: }
100:
101: public static boolean _left(String ofString, String isString) {
102: return new Strings(ofString).left(isString);
103: }
104:
105: public boolean left(String compareString) {
106: return left(compareString.length()).toUpperCase().equals(
107: compareString.toUpperCase());
108: }
109:
110: public String padLeft(char a_char, int a_length) {
111: return new Strings(new Strings(a_char, a_length).getString()
112: + _string).right(a_length);
113: }
114:
115: public String PadRight(char a_char, int a_length) {
116: return (_string + new Strings(a_char, a_length).getString())
117: .substring(0, a_length);
118: }
119:
120: public void replace(String a_Replace, String a_With) {
121: replace(a_Replace, a_With, 0);
122: }
123:
124: public static String replace(String source, String replace,
125: String with) {
126: Strings s = new Strings(source);
127: s.replace(replace, with);
128: return s.getString();
129: }
130:
131: private void replace(String replace, String with, int start) {
132: int pos = 0;
133: while ((pos = _string.indexOf(replace, start)) > -1) {
134: _string = _string.substring(0, pos) + with
135: + _string.substring(pos + replace.length());
136: }
137: }
138:
139: public void replace(String begin, String end, String with, int start) {
140: int from = _string.indexOf(begin, start);
141: if (from > -1) {
142: int to = _string.indexOf(end, from + 1);
143: if (to > -1) {
144: _string = _string.substring(0, from) + with
145: + _string.substring(to + end.length());
146: replace(begin, end, with, to);
147: }
148: }
149: }
150:
151: public static String _right(String ofString, int isChar) {
152: return new Strings(ofString).right(isChar);
153: }
154:
155: public String right(int a_chars) {
156: int l_take = _string.length() - a_chars;
157: if (l_take < 0) {
158: l_take = 0;
159: }
160: return _string.substring(l_take);
161: }
162:
163: public static boolean _right(String ofString, String compareString) {
164: return new Strings(ofString).right(compareString);
165: }
166:
167: public boolean right(String compareString) {
168: int l_take = _string.length() - compareString.length();
169: if (l_take < 0) {
170: l_take = 0;
171: }
172: String right = _string.substring(l_take).toUpperCase();
173: return right.equals(compareString.toUpperCase());
174: }
175:
176: public static String _splitRight(String a_String, String a_Splitter) {
177: return new Strings(a_String).splitRight(a_Splitter);
178: }
179:
180: public String splitRight(String a_Splitter) {
181: String l_Return = "";
182: int l_pos = _string.lastIndexOf(a_Splitter);
183: if (l_pos > 0) {
184: l_Return = _string.substring(l_pos + a_Splitter.length());
185: _string = _string.substring(0, l_pos);
186: }
187: return l_Return;
188: }
189:
190: public String toString() {
191: return _string;
192: }
193:
194: }
|