-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringUtils.java
More file actions
89 lines (77 loc) · 1.86 KB
/
Copy pathStringUtils.java
File metadata and controls
89 lines (77 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package org.todivefor.string.utils;
import java.text.DecimalFormatSymbols;
public class StringUtils
{
/**
* Method determines if input string is an integer
* Method is overloaded, accepts isInteger(String s)
* or isInteger(String s, int radix)
* @param s
* @return
*/
public static boolean isInteger(String s)
{
return isInteger(s,10);
}
public static boolean isInteger(String s, int radix)
{
if(s.isEmpty()) return false;
for(int i = 0; i < s.length(); i++)
{
if(i == 0 && s.charAt(i) == '-')
{
if(s.length() == 1) return false;
else continue;
}
if(Character.digit(s.charAt(i),radix) < 0) return false;
}
return true;
}
/**
* Method determines if a string is numeric
* Method uses try/catch which can be CPU intensive
* May want to consider: isStringNumeric( String str)
* @param str
* @return
*/
public static boolean isNumeric(String str)
{
try
{
@SuppressWarnings("unused")
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
/**
* Method determines if string is numeric without try/catch
* Treats "" as valid
* @param str
* @return
*/
public static boolean isStringNumeric( String str )
{
DecimalFormatSymbols currentLocaleSymbols = DecimalFormatSymbols.getInstance();
char localeMinusSign = currentLocaleSymbols.getMinusSign();
if ( !Character.isDigit( str.charAt( 0 ) ) && str.charAt( 0 ) != localeMinusSign ) return false;
boolean isDecimalSeparatorFound = false;
char localeDecimalSeparator = currentLocaleSymbols.getDecimalSeparator();
for ( char c : str.substring( 1 ).toCharArray() )
{
if ( !Character.isDigit( c ) )
{
if ( c == localeDecimalSeparator && !isDecimalSeparatorFound )
{
isDecimalSeparatorFound = true;
continue;
}
return false;
}
}
return true;
}
}