This class is meant to collect static methods for applying simple or more complex operations on strings.

If a String operation requires more than one line of code it should be located in this class for reusability. However it should only be placed in this class, if it really is reusable for other methods and classes. Very content specific operations are therefore excluded from this class or have to be generalized by providing appropriate parameters.

Version history

Version Change Date Tested since JRE Description
0.0.1 2015-01-15 1.8 Initial publish.
0.0.2 2015-04-16 1.6

Removed all references to things only available from JRE 1.7 since one client is still running on 1.6 and I need the tool to work on my clients laptop.

In the end I can always use an ant script to compile and package a library for different versions. Its just annoying to do things the old way :0(.

Also added new methods to get current timestamp as string and two methods to concatenate filter and log file strings from SQL resultsets.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
 
 */
package de.consulting.bolte.io;
 
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
 
/**
 * This is a class for applying simple or complex operations on Strings, which
 * occur more than once.
 
 * If a String operation requires more than one line of code it should be
 * located in this class for reusability.
 
 * However it should only be placed in this class, if it really is reusable for
 * other methods and classes. Very content specific operations are therefore
 * excluded from this class or have to be generalized by providing appropriate
 * parameters.
 
 * @author Alexander Bolte - Bolte Consulting (2015)
 */
public class StringOperation {
	/**
	 * Splits a provided String at each line break and returns the result as a
	 * collection.
	 
	 * The regular expression "\\n" is used to identify line breaks.
	 
	 * @param textWithLines
	 *            a String with line breaks, which should be converted into an
	 *            ArrayList.
	 * @return null if conversion fails, else an ArrayList with one list
	 *         element per line in handed parameter textWithLines.
	 */
	public static ArrayList convertLinesToArrayList(String textWithLines) {
		ArrayList ret = null;
		try {
			ret = new ArrayList(Arrays.asList(textWithLines
					.split("\\n")));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 
		return ret;
	}
 
	/**
	 * Concatenating two provided Strings including a line break in between them
	 * using the correct new line character as specified by Java class System.
	 
	 * @param src
	 *            the String, which another String should be appended to.
	 * @param append
	 *            the String, which should be appended to the src String.
	 * @return a concatenated String including a line break between both handed
	 *         Strings.
	 */
	public static String appendNewLine(String src, String append) {
		StringBuilder build = new StringBuilder();
 
		build.append(src);
		build.append(System.getProperty("line.separator"));
		build.append(append);
 
		return build.toString();
	}
 
	public static boolean isPhoneNumber(String phone) {
		return false;
	}
 
	/**
	 * Concatenates all values in all fields in handed ResultSet and returns the
	 * resulting String.
	 
	 * Calling this method will not advance the scanner for a handed ResultSet.
	 
	 * @param res
	 *            - an object of type ResultSet, which fields should be
	 *            concatenated.
	 * @param separator
	 *            - a String providing a separator for a returned concatenated
	 *            String.
	 * @return a concatenated String holding all values in provided ResultSet in
	 *         current line.
	 * @throws SQLException
	 */
	public static String concatenateResultsetFields(ResultSet res,
			String separator) throws SQLException {
		ResultSetMetaData resMeta = null;
		String line = null;
 
		// Loop through all fields in a ResultSet and append all values in
		// current row to a new line to write into target text file.
		resMeta = res.getMetaData();
		for (int i = 0; i < resMeta.getColumnCount(); i++) {
			if (res.getString(i + 1) != null) {
				line += res.getString(i + 1) + separator;
			} else {
				line += ";";
			}
		}
		// Remove the last separator.
		line = line.substring(0, line.length() - 1);
		resMeta = null;
 
		return line;
	}
 
	/**
	 * Concatenates all values in a field in a provided ResultSet object in one
	 * String.
	 
	 * It is not adviseable to use this method for ResultSets with many records.
	 * One reason is general bad performance of String operations another is
	 * limited capacity of Strings.
	 
	 * @param res
	 *            - an object of type ResultSet.
	 * @param separator
	 *            - a String providing a separator for all concatenated values
	 *            in a returned String.
	 * @param enclose
	 *            - a String providing a character to enclose / surround each
	 *            values in a returned String.
	 * @param fieldPos
	 *            - an Integer providing the position of a field in a ResultSet.
	 * @return a separated, concatenated String holding all values from defined
	 *         field in a provided ResultSet.
	 * @throws SQLException
	 */
	public static String concatenateResultSetRows(ResultSet res,
			String separator, String enclose, int fieldPos) throws SQLException {
		String list = "";
 
		// Collect all values in a separated list.
		while (res.next()) {
			list += enclose + res.getString(fieldPos) + enclose + separator;
		}
		// Remove the last comma from list.
		list = list.substring(0, list.length() - 1);
 
		return list;
	}
 
	/**
	 * Returns the current system time / date as a String formatted using the
	 * following pattern.
	 
	 * "yyyy-MM-dd HH-mm-ss"
	 
	 * @return a String providing the current timestamp in format
	 *         "yyyy-MM-dd HH-mm-ss".
	 */
	public static String getCurrTimestampAsString() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
		String ts = null;
 
		ts = df.format(new Date());
		return ts;
	}
}