Add support for String.isBlank() (#482)

This commit is contained in:
Jörg Hohwiller 2020-03-10 09:28:54 +01:00 committed by GitHub
parent a45d1e0d07
commit c2d27fdb1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 0 deletions

View File

@ -146,6 +146,16 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
public boolean isEmpty() { public boolean isEmpty() {
return characters.length == 0; return characters.length == 0;
} }
public boolean isBlank() {
for (int i = 0; i < characters.length; i++) {
if (characters[i] != ' ') {
return false;
}
}
return true;
}
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) { public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {
if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > length() || dstBegin < 0 if (srcBegin < 0 || srcBegin > srcEnd || srcEnd > length() || dstBegin < 0

View File

@ -17,6 +17,7 @@ package org.teavm.classlib.java.lang;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -311,4 +312,12 @@ public class StringTest {
assertEquals(0, str.length()); assertEquals(0, str.length());
assertEquals("", str); assertEquals("", str);
} }
@Test
public void testIsBlank() {
assertTrue(new String(new char[0]).isBlank());
assertTrue(new String(new char[] { ' ', ' ' }).isBlank());
assertFalse(new String(new char[] { ' ', 'x', ' ' }).isBlank());
assertFalse(new String(new char[] { 'a', ' ' }).isBlank());
}
} }