Added Numeric check to StringUtil
This commit is contained in:
parent
8d350d31c5
commit
57bd478e16
3 changed files with 96 additions and 12 deletions
|
|
@ -55,6 +55,9 @@ public class StringUtil {
|
|||
return value+" "+sizes[total];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return a human readable String with year/month/day/hour/min/sec/milisec delimitation.
|
||||
*/
|
||||
public static String formatTimeToString(long milisec){
|
||||
StringBuilder str = new StringBuilder();
|
||||
long tmp;
|
||||
|
|
@ -165,25 +168,28 @@ public class StringUtil {
|
|||
if( str == null || str.isEmpty() )
|
||||
return str;
|
||||
int start=0, stop=str.length();
|
||||
|
||||
// The beginning
|
||||
for(int i=0; i<str.length() ;i++){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
char c = str.charAt(i);
|
||||
if(c <= ' ' || c == trim)
|
||||
start = i+1;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
// The end
|
||||
for(int i=str.length()-1; i>start ;i--){
|
||||
char c = str.charAt( i );
|
||||
if( c <= ' ' || c == trim )
|
||||
char c = str.charAt(i);
|
||||
if(c <= ' ' || c == trim)
|
||||
stop = i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
if( start >= str.length() )
|
||||
|
||||
if(start >= str.length())
|
||||
return "";
|
||||
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
||||
|
||||
return str.substring(start, stop);
|
||||
}
|
||||
|
||||
|
|
@ -195,15 +201,18 @@ public class StringUtil {
|
|||
public static String trimQuotes(String str){
|
||||
if( str == null )
|
||||
return null;
|
||||
|
||||
str = str.trim();
|
||||
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||
str = str.substring(1, str.length()-1);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
private static ArrayList<String> SPACES = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* @return A string containing a specific amount of spaces
|
||||
*/
|
||||
|
|
@ -236,6 +245,7 @@ public class StringUtil {
|
|||
public static List<String> split(String str, char delimiter){
|
||||
ArrayList<String> splitList = new ArrayList<>();
|
||||
int from = 0, to = 0;
|
||||
|
||||
while (to >= 0) {
|
||||
to = str.indexOf(delimiter, from + 1);
|
||||
if (to < 0)
|
||||
|
|
@ -244,6 +254,51 @@ public class StringUtil {
|
|||
splitList.add(str.substring(from, to));
|
||||
from = to;
|
||||
}
|
||||
|
||||
return splitList;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true only if the String contains correct numerical characters.
|
||||
*/
|
||||
public static boolean isNumber(String str) {
|
||||
if (str == null || str.length() < 1)
|
||||
return false;
|
||||
|
||||
for (int i=0; i<str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
|
||||
if (i == 0 && c == '-')
|
||||
continue;
|
||||
else if (!Character.isDigit(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true only if the String contains correct numerical characters and decimal notation.
|
||||
*/
|
||||
public static boolean isDecimalNumber(String str) {
|
||||
if (str == null || str.length() < 1)
|
||||
return false;
|
||||
|
||||
boolean parsedPunctuation = false;
|
||||
|
||||
for (int i=0; i<str.length(); i++) {
|
||||
char c = str.charAt(i);
|
||||
|
||||
if (i == 0 && c == '-')
|
||||
continue;
|
||||
else if (!parsedPunctuation && c == '.') {
|
||||
parsedPunctuation = true;
|
||||
continue;
|
||||
}
|
||||
else if (!Character.isDigit(c))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ public class MathParser {
|
|||
MathOperation current = null;
|
||||
|
||||
if (!Character.isWhitespace(c)) {
|
||||
if (isNumber(c)) {
|
||||
if (Character.isDigit(c)) {
|
||||
temp.append(c);
|
||||
} else {
|
||||
Math container = new MathNumber();
|
||||
|
|
@ -95,10 +95,6 @@ public class MathParser {
|
|||
parse(functionString, temp, previous, rootNode);
|
||||
}
|
||||
|
||||
private static boolean isNumber(char c) {
|
||||
return Character.isDigit(c);
|
||||
}
|
||||
|
||||
private static MathOperation getOperation(char c) {
|
||||
switch (c) {
|
||||
case '+':
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
|||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
|
||||
public class StringUtilTest {
|
||||
|
|
@ -80,4 +80,37 @@ public class StringUtilTest {
|
|||
assertEquals("animal,monkey,dog", StringUtil.join(",", Arrays.asList("animal", "monkey", "dog")));
|
||||
assertEquals("12345", StringUtil.join("", 1,2,3,4,5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isNumber() {
|
||||
assertFalse(StringUtil.isNumber(null));
|
||||
assertFalse(StringUtil.isNumber(""));
|
||||
assertFalse(StringUtil.isNumber("a"));
|
||||
assertFalse(StringUtil.isNumber("-monkey"));
|
||||
assertFalse(StringUtil.isNumber("-123monkey"));
|
||||
assertTrue(StringUtil.isNumber("0"));
|
||||
assertTrue(StringUtil.isNumber("-1"));
|
||||
assertTrue(StringUtil.isNumber("-500"));
|
||||
assertTrue(StringUtil.isNumber("1"));
|
||||
assertTrue(StringUtil.isNumber("500"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isDecimalNumber() {
|
||||
assertFalse(StringUtil.isDecimalNumber(null));
|
||||
assertFalse(StringUtil.isDecimalNumber(""));
|
||||
assertFalse(StringUtil.isDecimalNumber("a"));
|
||||
assertFalse(StringUtil.isDecimalNumber("-monkey"));
|
||||
assertFalse(StringUtil.isDecimalNumber("-123monkey"));
|
||||
assertTrue(StringUtil.isDecimalNumber("0"));
|
||||
assertTrue(StringUtil.isDecimalNumber("-1"));
|
||||
assertTrue(StringUtil.isDecimalNumber("-500"));
|
||||
assertTrue(StringUtil.isDecimalNumber("1"));
|
||||
assertTrue(StringUtil.isDecimalNumber("500"));
|
||||
assertTrue(StringUtil.isDecimalNumber("0.0"));
|
||||
assertTrue(StringUtil.isDecimalNumber("-1.1"));
|
||||
assertTrue(StringUtil.isDecimalNumber("-500.2"));
|
||||
assertTrue(StringUtil.isDecimalNumber("1.3"));
|
||||
assertTrue(StringUtil.isDecimalNumber("500.500"));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue