-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathStringUtil.java
More file actions
61 lines (55 loc) · 1.66 KB
/
StringUtil.java
File metadata and controls
61 lines (55 loc) · 1.66 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
package com.not2excel.api.util;
import java.util.Random;
/**
* @author Richmond Steele
* @since 12/18/13
* All rights Reserved
* Please read included LICENSE file
*/
public final class StringUtil
{
private static final Random random = new Random();
public static boolean containsChar(final char c, final Character[] charSet)
{
for (final char test : charSet)
{
if (test == c)
{ return true; }
}
return false;
}
/**
* Generates a random string
* @param length - length of the random string
* @param charArray - include a char array if you wish the random string to only include those chars
* @return random string
*/
public static String randomString(final int length, final Character[]... charArray)
{
final StringBuilder stringBuilder = new StringBuilder();
final int min = 33, max = 126;
if (charArray.length > 0)
{
final Character[] chars = charArray[0];
stringBuilder.append(chars[random.nextInt(chars.length - 1)]);
}
else
{
for (int i = 0; i < length; i++)
{
final char c = (char) (min + (int) (Math.random() * ((max - min) + 1)));
stringBuilder.append(c);
}
}
return stringBuilder.toString();
}
/**
* Capatilizes the first letter of a string and lower cases the rest
* @param string
* @return
*/
public static String capatilizeFirstLetterOnly(final String string)
{
return string.substring(0, 1).toUpperCase().concat(string.substring(1).toLowerCase());
}
}