1,264 バイト追加
、 2020年2月15日 (土) 07:33
==Java 文字列の左右トリミング==
[Java]
===String.trim() のトリミング対象文字 ' 'より小さい文字コード に準拠===
public String trim(String value, String type) {
if (value != null) {
if ("both".equals(type)) {
value = value.trim();
} else {
if ("left".equals(type)) {
int pos = 0;
for (int i=0; i<value.length(); i++) {
char c = value.charAt(i);
if (c <= ' ') {
pos++;
} else {
break;
}
}
if (pos > 0) {
value = value.substring(pos);
}
} else if
("right".equals(type)) {
int pos = 0;
for (int i=value.length()-1; i>=0; i--) {
char c = value.charAt(i);
if (c <= ' ') {
pos = i;
} else {
break;
}
}
if (pos >= 0 && pos < value.length()) {
value = value.substring(0, pos);
}
}
}
}
return value;
}