KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > db4ounit > util > Strings


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.db4ounit.util;
22
23 public class Strings
24 {
25     private static final char DATESEPARATOR = '-';
26     private static final char TIMESEPARATOR = ':';
27     private static final char DATETIMESEPARATOR = ' ';
28     
29     private String JavaDoc _string;
30     
31     private Strings(){}
32     
33     public Strings(String JavaDoc str){
34         _string = str;
35     }
36     
37     public Strings(char a_char, int a_count){
38         char[] chars = new char[a_count];
39         for (int i = 0; i < a_count;chars[i++] = a_char);
40         _string = new String JavaDoc(chars);
41     }
42     
43     public Strings(int i){
44         _string = "" + i;
45     }
46     
47     public Strings(long l){
48         _string = "" + l;
49     }
50     
51     public Strings(char c){
52         _string = "" + c;
53     }
54     
55     public void clear(){
56         _string = "";
57     }
58     
59     public String JavaDoc getString(){
60         return _string;
61     }
62     
63     protected String JavaDoc joinDate(String JavaDoc year, String JavaDoc month, String JavaDoc day ){
64         _string = year + DATESEPARATOR + month + DATESEPARATOR + day;
65         return _string;
66     }
67     
68     protected String JavaDoc joinTime(String JavaDoc hours, String JavaDoc minutes,String JavaDoc seconds){
69         _string = hours + TIMESEPARATOR + minutes + TIMESEPARATOR + seconds;
70         return _string;
71     }
72
73     protected String JavaDoc joinDateTime(String JavaDoc date, String JavaDoc time){
74         _string = date + DATETIMESEPARATOR + time;
75         return _string;
76     }
77     
78     public static String JavaDoc _left(String JavaDoc a_String, int a_chars){
79         return new Strings(a_String).left(a_chars);
80     }
81     
82     public String JavaDoc left(int a_chars){
83         if(a_chars > _string.length()){
84             a_chars = _string.length();
85         }else{
86             if (a_chars < 0){
87                 a_chars = 0;
88             }
89         }
90         return _string.substring(0,a_chars);
91     }
92     
93     public static boolean _left(String JavaDoc ofString, String JavaDoc isString){
94         return new Strings(ofString).left(isString);
95     }
96     
97     public boolean left(String JavaDoc compareString){
98         return left(compareString.length()).toUpperCase().equals(compareString.toUpperCase());
99     }
100     
101     public String JavaDoc padLeft(char a_char, int a_length){
102         return new Strings(new Strings(a_char,a_length).getString() + _string).right(a_length);
103     }
104     
105     public String JavaDoc PadRight(char a_char, int a_length){
106         return (_string + new Strings(a_char,a_length).getString()).substring(0,a_length);
107     }
108     
109     public void replace(String JavaDoc a_Replace, String JavaDoc a_With){
110         replace(a_Replace, a_With,0);
111     }
112     
113     public static String JavaDoc replace(String JavaDoc source, String JavaDoc replace, String JavaDoc with){
114         Strings s = new Strings(source);
115         s.replace(replace, with);
116         return s.getString();
117     }
118     
119     private void replace(String JavaDoc replace, String JavaDoc with, int start){
120         int pos = 0;
121         while((pos = _string.indexOf(replace,start)) > -1){
122             _string = _string.substring(0,pos) + with + _string.substring(pos + replace.length());
123         }
124     }
125     
126     public void replace (String JavaDoc begin, String JavaDoc end, String JavaDoc with, int start){
127         int from = _string.indexOf(begin, start);
128         if (from > -1){
129             int to = _string.indexOf(end,from + 1);
130             if(to > - 1){
131                 _string = _string.substring(0,from) + with + _string.substring(to + end.length());
132                 replace(begin, end, with, to);
133             }
134         }
135     }
136     
137     public static String JavaDoc _right(String JavaDoc ofString, int isChar){
138         return new Strings(ofString).right(isChar);
139     }
140     
141     public String JavaDoc right(int a_chars){
142         int l_take = _string.length() - a_chars;
143         if(l_take < 0){
144             l_take = 0;
145         }
146         return _string.substring(l_take);
147     }
148     
149     public static boolean _right(String JavaDoc ofString, String JavaDoc compareString){
150         return new Strings(ofString).right(compareString);
151     }
152     
153     public boolean right(String JavaDoc compareString){
154         int l_take = _string.length() - compareString.length();
155         if(l_take < 0){
156             l_take = 0;
157         }
158         String JavaDoc right = _string.substring(l_take).toUpperCase();
159         return right.equals(compareString.toUpperCase());
160     }
161     
162     public static String JavaDoc _splitRight(String JavaDoc a_String, String JavaDoc a_Splitter){
163         return new Strings(a_String).splitRight(a_Splitter);
164     }
165     
166     public String JavaDoc splitRight(String JavaDoc a_Splitter){
167         String JavaDoc l_Return = "";
168         int l_pos = _string.lastIndexOf(a_Splitter);
169         if(l_pos > 0){
170             l_Return = _string.substring(l_pos + a_Splitter.length());
171             _string = _string.substring(0,l_pos);
172         }
173         return l_Return;
174     }
175     
176     public String JavaDoc toString(){
177         return _string;
178     }
179     
180 }
181
Popular Tags