Stringを操作するいくつかのMethod
public static String join(String[] array, String sep) {
if (array == null) {
return null;
}
if (array.length == 0) {
return "";
}
if (sep == null) {
sep = "";
}
// , 。
int capacity = 0;
for (int i = 0; i < array.length; i++) {
array[i] = String.valueOf(array[i]);
capacity = capacity + array[i].length() + sep.length();
}
char[] result = new char[capacity - sep.length()];
int begin = 0;
for (int i = 0; i < array.length; i++) {
array[i].getChars(0, array[i].length(), result, begin);
begin = begin + array[i].length();
if (begin == result.length) {
break;
}
sep.getChars(0, sep.length(), result, begin);
begin = begin + sep.length();
}
return new String(result);
}
使用結果:
StringExt.join(null, *) = null
StringExt.join([], *) = ""
StringExt.join([null], *) = "null"
StringExt.join(["a", "b", "c"], "--") = "a--b--c"
StringExt.join(["a", "b", "c"], null) = "abc"
StringExt.join(["a", "b", "c"], "") = "abc"
StringExt.join([null, "", "a"], ",") = "null,,a"
2番目
public static String join2(String[] array, String sep) {
if (array == null) {
return null;
}
if (array.length == 0) {
return "";
}
if (sep == null) {
sep = "";
}
StringBuilder sb = new StringBuilder();
for (String tkey : array) {
sb.append(tkey);
sb.append(sep);
}
int len = sb.length();
sb.delete(len - sep.length(), len);
return sb.toString();
}
2つのjoinの使用結果は同じで、1つ目は少し速いかもしれませんが、2つ目はもっと簡単に見えます.
org.apache.commons.lang.StringUtilsには似たような方法があります.
/**
*
* @param source
* @param separator
* @return
*/
public static String[] split(String source, String... separator) {
if (source == null) {
return null;
}
if (source.length() == 0) {
return new String[] { "" };
}
if (separator == null) {
return splitfields(source, null, -1);
}
ArrayList<String> result = new ArrayList<String>();
char[] arrsource = source.toCharArray();
char[][] arrseps = new char[separator.length][];
for (int i = 0; i < arrseps.length; i++) {
arrseps[i] = separator[i].toCharArray();
if (arrseps[i].length == 0) {
return splitfields(source, null, -1);
}
}
int preend = 0;
int begin = 0;
boolean cansep = false;
int end = 0;
for (int i = 0; i < arrsource.length; i++) {
begin = i;
NextSep: for (int j = 0; j < arrseps.length; j++) {
end = i + arrseps[j].length;
if (end > arrsource.length) {
cansep = false;
continue;
}
int m = i, n = 0;
for (; m < arrsource.length && n < arrseps[j].length; m++, n++) {
if (arrsource[m] != arrseps[j][n]) {
cansep = false;
continue NextSep;
}
}
cansep = true;
break;
}
if (cansep) {
int len = begin - preend;
char[] preChar = new char[len];
System.arraycopy(arrsource, preend, preChar, 0, len);
result.add(new String(preChar));
preend = end;
cansep = false;
i = end - 1;
begin = i;
}
}
int len = begin - preend + 1;
char[] preChar = new char[len];
System.arraycopy(arrsource, preend, preChar, 0, len);
result.add(new String(preChar));
return result.toArray(new String[0]);
}
使用結果:
StringExt.split(null, *) = null
StringExt.split("", *) = [""]
StringExt.split("ab de fg", null) = ["ab", "cd", "ef"]
StringExt.split("ab de fg", null) = ["ab", "cd", "ef"]
StringExt.split("ab de fg", null) = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", *, "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", *, "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", *, "") = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "", *) = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "", *) = ["ab", "cd", "ef"]
StringExt.split("ab de fg", "", *) = ["ab", "cd", "ef"]
StringExt.split("ab:cd:ef", ":") = ["ab", "cd", "ef"]
StringExt.split("ab,,,cd,,,ef", ",,,") = ["ab", "cd", "ef"]
StringExt.split("ab,,,cd;;;ef", ",,,", ";;;") = ["ab", "cd", "ef"]
StringExt.split("ab,,,,,,ef", ",,,") = ["ab", "", "ef"]
splitの分割子は複数でもよく、ファイルを分割するのに便利です.
org.apache.commons.lang.StringUtilsには似たようなsplitメソッドがありますが、csv形式のファイルを分割するには、あまり使いにくいです.
1;2;3;4;5
a;b;;d;e
9;8;7;6;5
「;」を使うなら分割して、第2列はそろっていないで、各2列の中で分割して空のを舍ててて、apacheがどうしてこのように実现するのか分からないで、Stringとためですか.splitは一致していますか?
public static String[] split(String source) {
return split(source, null, -1);
}
public static String[] split(String source, String sep) {
return split(source, sep, -1);
}
public static String[] split(String source, String sep, int maxsplit) {
if (source == null) {
return null;
}
if (source.length() == 0) {
return new String[] { "" };
}
if (sep != null && sep.length() != 0)
return splitfields(source, sep, maxsplit);
ArrayList<String> list = new ArrayList<String>();
char[] chars = source.toCharArray();
int n = chars.length;
if (maxsplit < 0)
maxsplit = n;
int splits = 0;
int index = 0;
while (index < n && splits < maxsplit) {
while (index < n && Character.isWhitespace(chars[index]))
index++;
if (index == n)
break;
int start = index;
while (index < n && !Character.isWhitespace(chars[index]))
index++;
list.add(source.substring(start, index));
splits++;
}
while (index < n && Character.isWhitespace(chars[index]))
index++;
if (index < n) {
list.add(source.substring(index, n));
}
return list.toArray(new String[0]);
}
private static String[] splitfields(String source, String sep, int maxsplit) {
ArrayList<String> list = new ArrayList<String>();
int length = source.length();
if (maxsplit < 0)
maxsplit = length;
int lastbreak = 0;
int splits = 0;
int sepLength = sep.length();
while (splits < maxsplit) {
int index = source.indexOf(sep, lastbreak);
if (index == -1)
break;
splits += 1;
list.add(source.substring(lastbreak, index));
lastbreak = index + sepLength;
}
if (lastbreak <= length) {
list.add(source.substring(lastbreak, length));
}
return list.toArray(new String[0]);
}
public static String[] splitlines(String source) {
return splitlines(source, false);
}
public static String[] splitlines(String source, boolean keepends) {
ArrayList<String> list = new ArrayList<String>();
char[] chars = source.toCharArray();
int n = chars.length;
int j = 0;
for (int i = 0; i < n;) {
/* Find a line and append it */
while (i < n && chars[i] != '
' && chars[i] != '\r'
&& Character.getType(chars[i]) != Character.LINE_SEPARATOR)
i++;
/* Skip the line break reading CRLF as one line break */
int eol = i;
if (i < n) {
if (chars[i] == '\r' && i + 1 < n && chars[i + 1] == '
')
i += 2;
else
i++;
if (keepends)
eol = i;
}
list.add(source.substring(j, eol));
j = i;
}
if (j < n) {
list.add(source.substring(j, n));
}
return list.toArray(new String[0]);
}
splitlinesは行ごとに分割され、改行は「」または「r」であってもよい.
org.apache.commons.lang.StringUtilsにも似たような方法があります.
以上の方法は、自分で実現したものもあれば、ソースコードからコピーしたものもあります.
整列の実装、左揃え、右揃えなど
public static String ljust(String src, int width) {
return expand(src, width, ' ', true);
}
public static String rjust(String src, int width) {
return expand(src, width, ' ', false);
}
public static String ljust(String src, int width, char fillchar) {
return expand(src, width, fillchar, true);
}
public static String rjust(String src, int width, char fillchar) {
return expand(src, width, fillchar, false);
}
public static String expand(String src, int width, char fillchar, boolean postfix) {
String result = src;
if (result.length() < width) {
char[] temp = new char[width - result.length()];
for (int i = 0; i < temp.length; i++) {
temp[i] = fillchar;
}
if (postfix) {
result = result + new String(temp);
} else {
result = new String(temp) + result;
}
}
return result;
}
置き換えの実現、へへ、面白いでしょう:
public static String repalce(String source, String oldStr, String newStr) {
String[] split = split(source, oldStr);
return join(split, newStr);
}
これ
行
留
に与える
広
告げる
商
ああ