1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package ubic.basecode.util;
20
21 import java.io.IOException;
22 import java.io.StringReader;
23 import java.util.Collection;
24
25 import com.opencsv.CSVReader;
26 import com.opencsv.exceptions.CsvValidationException;
27 import org.apache.commons.lang3.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34 public class StringUtil {
35
36 private static final Logger log = LoggerFactory.getLogger( StringUtil.class );
37
38
39
40
41
42
43
44 public static String append( String appendee, String appendant, String separator ) {
45 if ( StringUtils.isBlank( appendee ) ) {
46 return appendant;
47 }
48 return appendee + separator + appendant;
49
50 }
51
52
53
54
55
56
57
58 public static String commonPrefix( Collection<String> strings ) {
59
60 String shortest = shortestString( strings );
61
62 if ( shortest == null || shortest.length() == 0 ) return null;
63
64 String test = shortest;
65 while ( test.length() > 0 ) {
66 boolean found = true;
67 for ( String string : strings ) {
68 if ( !string.startsWith( test ) ) {
69 found = false;
70 break;
71 }
72 }
73 if ( found ) return test;
74 test = test.substring( 0, test.length() - 1 );
75 }
76 return null;
77 }
78
79
80
81
82
83
84
85 public static String commonSuffix( Collection<String> strings ) {
86 String shortest = shortestString( strings );
87
88 if ( shortest == null || shortest.length() == 0 ) return null;
89
90 String test = shortest;
91 while ( test.length() > 0 ) {
92 boolean found = true;
93 for ( String string : strings ) {
94 if ( !string.endsWith( test ) ) {
95 found = false;
96 break;
97 }
98 }
99 if ( found ) return test;
100 test = test.substring( 1 );
101 }
102 return null;
103 }
104
105
106
107
108
109
110
111
112 public static boolean containsValidCharacter( String s ) {
113
114 if ( s != null ) {
115
116 for ( int i = 0; i < s.length(); i++ ) {
117
118 Character cha = s.charAt( i );
119
120 if ( !( isLatinLetter( cha ) || Character.isDigit( cha ) || cha == '=' || cha == ',' || cha == '('
121 || cha == ')' || cha == '\'' || Character.isWhitespace( cha ) || cha == '/' || cha == '?'
122 || cha == '+' || cha == ':' || cha == '-' || cha == '<' || cha == '>' || cha == '"'
123 || cha == '%' || cha == '.' || cha == '*' || cha == '[' || cha == ']' || cha == ';'
124 || cha == '_' || cha == '\\' || cha == '|' || cha == '&' || cha == '^' || cha == '#'
125 || cha == '{' || cha == '}' || cha == '!' || cha == '~' || cha == '@' || cha == '—'
126 || cha == '×' || cha == '–' || cha == ' ' ) ) {
127
128
129
130 log.warn( "Illegal character found: " + cha + " found on description: " + s );
131
132 return false;
133 }
134 }
135 }
136 return true;
137 }
138
139
140
141
142
143
144 public static String[] csvSplit( String line ) {
145
146 @SuppressWarnings("resource")
147 CSVReader reader = new CSVReader( new StringReader( line ) );
148
149 try {
150 return reader.readNext();
151 } catch ( IOException | CsvValidationException e ) {
152 throw new RuntimeException( e );
153 }
154 }
155
156
157
158
159
160
161
162 public static String cvs2tsv( String line ) {
163
164 StringBuffer newLine = new StringBuffer( line );
165
166 boolean change = true;
167
168 for ( int position = 0; position < newLine.length(); position++ ) {
169
170 if ( newLine.charAt( position ) == ',' && change ) {
171 newLine.setCharAt( position, '\t' );
172 } else if ( newLine.charAt( position ) == '"' ) {
173
174 if ( change ) {
175 change = false;
176 } else {
177 change = true;
178 }
179 }
180 }
181 return newLine.toString().replaceAll( "\"", "" );
182 }
183
184 public static boolean isLatinLetter( char c ) {
185 return ( c >= 'A' && c <= 'Z' ) || ( c >= 'a' && c <= 'z' );
186 }
187
188
189
190
191
192
193
194
195
196 public static String makeValidForR( String s ) {
197
198
199 if ( s.matches( "^\\.?[0-9].+" ) ) {
200 s = "X" + s;
201 }
202
203
204
205
206 return s.replaceAll( "[\\W]+", "." );
207 }
208
209
210
211
212
213
214 public static Long twoStringHashKey( String stringi, String stringj ) {
215
216 if ( stringi.hashCode() < stringj.hashCode() ) {
217 return new Long( stringi.hashCode() | ( long ) stringj.hashCode() << 32 );
218 }
219 return new Long( stringj.hashCode() | ( long ) stringi.hashCode() << 32 );
220 }
221
222 private static String shortestString( Collection<String> strings ) {
223 String shortest = null;
224 for ( String string : strings ) {
225 if ( shortest == null || string.length() < shortest.length() ) shortest = string;
226 }
227 return shortest;
228 }
229
230 }