-
-
Save ron623/66c5f1c3585ba9074a2e to your computer and use it in GitHub Desktop.
trim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String trim() { | |
int len = count; // 文字数(終了インデックス) | |
int st = 0; // 開始インデックス | |
int off = offset; // 最初のインデックス | |
char[] val = value; // String文字列 | |
// Stringオブジェクトの長さが0より大きく、(最初のindex+0)番目の文字がスペースより小さい場合 | |
while ((st < len) && (val[off + st] <= ' ')) { | |
// indexを1つずらし、次の文字をチェック。。 | |
// スペースじゃなくなるまで行う | |
st++; // 開始インデックス | |
} | |
// Stringオブジェクトの長さが0より大きく、(最初のインデックス+文字数-1)番目の文字がスペースより小さい場合 | |
while ((st < len) && (val[off + len - 1] <= ' ')) { | |
// indexを1つずらし、前の文字をチェック。 | |
// スペースじゃなくなるまで行う。 | |
len--; // 最後のインデックス | |
} | |
// 文字の最初か最後に1文字以上スペースを含む場合はsubstring | |
// スペースを含まない場合は元の文字列の参照を返す | |
return ((st > 0) || (len < count)) ? substring(st, len) : this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment