classlib: add String.strip

This commit is contained in:
Alexey Andreev 2023-05-16 10:12:36 +02:00
parent 697ad73762
commit 81f78fab01
2 changed files with 20 additions and 0 deletions

View File

@ -475,6 +475,18 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return substring(lower, upper + 1); return substring(lower, upper + 1);
} }
public TString strip() {
var lower = 0;
var upper = length() - 1;
while (lower <= upper && Character.isWhitespace(charAt(lower))) {
++lower;
}
while (lower <= upper && Character.isWhitespace(charAt(upper))) {
--upper;
}
return substring(lower, upper + 1);
}
@Override @Override
public String toString() { public String toString() {
return (String) (Object) this; return (String) (Object) this;

View File

@ -201,6 +201,14 @@ public class StringTest {
assertEquals("", " ".trim()); assertEquals("", " ".trim());
} }
@Test
public void stripWorks() {
assertEquals("ab", " ab ".strip());
assertEquals("ab", "ab".strip());
assertEquals("", " \t".strip());
assertEquals("ab", "\t\n \u2008ab\r\f".strip());
}
@Test @Test
public void convertedToCharArray() { public void convertedToCharArray() {
char[] array = "123".toCharArray(); char[] array = "123".toCharArray();