Created
March 13, 2016 09:38
-
-
Save ryugoo/0abcf50fcbd497acf2b7 to your computer and use it in GitHub Desktop.
String utility for Android
This file contains hidden or 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
package com.r384ta.android.betterstructure.util; | |
import android.support.annotation.IntDef; | |
import android.support.annotation.Nullable; | |
public class StringUtils { | |
public static final int DEFAULT = 0; | |
public static final int ASCII_TRIM = 1; | |
public static final int FULL_TRIM = 2; | |
public static boolean isEmpty(@Nullable CharSequence text) { | |
return text == null || text.length() == 0 || isEmpty(text, DEFAULT); | |
} | |
public static boolean isEmpty(@Nullable CharSequence text, @EmptyType int type) { | |
if (text == null || text.length() == 0) return true; | |
if (type == DEFAULT) { | |
return text.toString().isEmpty(); | |
} else if (type == ASCII_TRIM) { | |
return text.toString().trim().isEmpty(); | |
} else { | |
return trim(text.toString()).isEmpty(); | |
} | |
} | |
public static String trim(@Nullable String text) { | |
if (text == null) return null; | |
char[] val = text.toCharArray(); | |
int len = val.length; | |
int st = 0; | |
while ((st < len) && (val[len - 1] <= ' ' || val[len - 1] == ' ')) { | |
len--; | |
} | |
return ((len < val.length)) ? text.substring(st, len) : text; | |
} | |
@IntDef(value = {DEFAULT, ASCII_TRIM, FULL_TRIM}) | |
public @interface EmptyType { | |
} | |
} |
This file contains hidden or 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
package com.r384ta.android.betterstructure; | |
import com.r384ta.android.betterstructure.util.StringUtils; | |
import org.junit.Test; | |
import static com.r384ta.android.betterstructure.util.StringUtils.ASCII_TRIM; | |
import static com.r384ta.android.betterstructure.util.StringUtils.FULL_TRIM; | |
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertNull; | |
import static org.junit.Assert.assertTrue; | |
public class StringUtilsTest { | |
@Test | |
public void testIsEmpty() throws Exception { | |
// Basic | |
assertTrue(StringUtils.isEmpty(null)); | |
assertTrue(StringUtils.isEmpty("")); | |
assertFalse(StringUtils.isEmpty("こんにちは")); | |
assertFalse(StringUtils.isEmpty(" ")); | |
assertFalse(StringUtils.isEmpty(" ")); | |
// Ascii trim | |
assertTrue(StringUtils.isEmpty(null, ASCII_TRIM)); | |
assertTrue(StringUtils.isEmpty("", ASCII_TRIM)); | |
assertFalse(StringUtils.isEmpty("こんにちは", ASCII_TRIM)); | |
assertTrue(StringUtils.isEmpty(" ", ASCII_TRIM)); | |
assertFalse(StringUtils.isEmpty(" ", ASCII_TRIM)); | |
// Full trim | |
assertTrue(StringUtils.isEmpty(null, FULL_TRIM)); | |
assertTrue(StringUtils.isEmpty("", FULL_TRIM)); | |
assertFalse(StringUtils.isEmpty("こんにちは", FULL_TRIM)); | |
assertTrue(StringUtils.isEmpty(" ", FULL_TRIM)); | |
assertTrue(StringUtils.isEmpty(" ", FULL_TRIM)); | |
} | |
@Test | |
public void testTrim() throws Exception { | |
assertNull(StringUtils.trim(null)); | |
assertEquals(StringUtils.trim(""), ""); | |
assertEquals(StringUtils.trim("こんにちは"), "こんにちは"); | |
assertEquals(StringUtils.trim(" "), ""); | |
assertEquals(StringUtils.trim(" "), ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment