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 value+" "+sizes[total];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return a human readable String with year/month/day/hour/min/sec/milisec delimitation.
|
||||||
|
*/
|
||||||
public static String formatTimeToString(long milisec){
|
public static String formatTimeToString(long milisec){
|
||||||
StringBuilder str = new StringBuilder();
|
StringBuilder str = new StringBuilder();
|
||||||
long tmp;
|
long tmp;
|
||||||
|
|
@ -165,6 +168,7 @@ public class StringUtil {
|
||||||
if( str == null || str.isEmpty() )
|
if( str == null || str.isEmpty() )
|
||||||
return str;
|
return str;
|
||||||
int start=0, stop=str.length();
|
int start=0, stop=str.length();
|
||||||
|
|
||||||
// The beginning
|
// The beginning
|
||||||
for(int i=0; i<str.length() ;i++){
|
for(int i=0; i<str.length() ;i++){
|
||||||
char c = str.charAt(i);
|
char c = str.charAt(i);
|
||||||
|
|
@ -173,6 +177,7 @@ public class StringUtil {
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The end
|
// The end
|
||||||
for(int i=str.length()-1; i>start ;i--){
|
for(int i=str.length()-1; i>start ;i--){
|
||||||
char c = str.charAt(i);
|
char c = str.charAt(i);
|
||||||
|
|
@ -181,9 +186,10 @@ public class StringUtil {
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(start >= str.length())
|
if(start >= str.length())
|
||||||
return "";
|
return "";
|
||||||
//System.out.println("str: \""+str+"\" start: "+start+" stop: "+stop);
|
|
||||||
return str.substring(start, stop);
|
return str.substring(start, stop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -195,15 +201,18 @@ public class StringUtil {
|
||||||
public static String trimQuotes(String str){
|
public static String trimQuotes(String str){
|
||||||
if( str == null )
|
if( str == null )
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
str = str.trim();
|
str = str.trim();
|
||||||
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
if( str.length() >= 2 && str.charAt(0)=='\"' && str.charAt(str.length()-1)=='\"'){
|
||||||
str = str.substring(1, str.length()-1);
|
str = str.substring(1, str.length()-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static ArrayList<String> SPACES = new ArrayList<>();
|
private static ArrayList<String> SPACES = new ArrayList<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return A string containing a specific amount of spaces
|
* @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){
|
public static List<String> split(String str, char delimiter){
|
||||||
ArrayList<String> splitList = new ArrayList<>();
|
ArrayList<String> splitList = new ArrayList<>();
|
||||||
int from = 0, to = 0;
|
int from = 0, to = 0;
|
||||||
|
|
||||||
while (to >= 0) {
|
while (to >= 0) {
|
||||||
to = str.indexOf(delimiter, from + 1);
|
to = str.indexOf(delimiter, from + 1);
|
||||||
if (to < 0)
|
if (to < 0)
|
||||||
|
|
@ -244,6 +254,51 @@ public class StringUtil {
|
||||||
splitList.add(str.substring(from, to));
|
splitList.add(str.substring(from, to));
|
||||||
from = to;
|
from = to;
|
||||||
}
|
}
|
||||||
|
|
||||||
return splitList;
|
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;
|
MathOperation current = null;
|
||||||
|
|
||||||
if (!Character.isWhitespace(c)) {
|
if (!Character.isWhitespace(c)) {
|
||||||
if (isNumber(c)) {
|
if (Character.isDigit(c)) {
|
||||||
temp.append(c);
|
temp.append(c);
|
||||||
} else {
|
} else {
|
||||||
Math container = new MathNumber();
|
Math container = new MathNumber();
|
||||||
|
|
@ -95,10 +95,6 @@ public class MathParser {
|
||||||
parse(functionString, temp, previous, rootNode);
|
parse(functionString, temp, previous, rootNode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isNumber(char c) {
|
|
||||||
return Character.isDigit(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static MathOperation getOperation(char c) {
|
private static MathOperation getOperation(char c) {
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '+':
|
case '+':
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
|
||||||
public class StringUtilTest {
|
public class StringUtilTest {
|
||||||
|
|
@ -80,4 +80,37 @@ public class StringUtilTest {
|
||||||
assertEquals("animal,monkey,dog", StringUtil.join(",", Arrays.asList("animal", "monkey", "dog")));
|
assertEquals("animal,monkey,dog", StringUtil.join(",", Arrays.asList("animal", "monkey", "dog")));
|
||||||
assertEquals("12345", StringUtil.join("", 1,2,3,4,5));
|
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