From 57bd478e1601d9a45e0449db87573b641c8d2797 Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Mon, 22 Jun 2020 14:57:39 +0200 Subject: [PATCH] Added Numeric check to StringUtil --- src/zutil/StringUtil.java | 67 +++++++++++++++++++++++++++++--- src/zutil/parser/MathParser.java | 6 +-- test/zutil/StringUtilTest.java | 35 ++++++++++++++++- 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/zutil/StringUtil.java b/src/zutil/StringUtil.java index 4001c92..7318631 100755 --- a/src/zutil/StringUtil.java +++ b/src/zutil/StringUtil.java @@ -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; istart ;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 SPACES = new ArrayList<>(); + /** * @return A string containing a specific amount of spaces */ @@ -236,6 +245,7 @@ public class StringUtil { public static List split(String str, char delimiter){ ArrayList 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