-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathStringUtils.java
More file actions
71 lines (61 loc) · 2.39 KB
/
StringUtils.java
File metadata and controls
71 lines (61 loc) · 2.39 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
package com.dtcc.exams.fundamentals;
public class StringUtils {
/**
* @param stringToBePadded - string value to be flushed right
* @param amountOfPadding - amount of padding to be flushed left
* @return `stringToBePadded` flushed right by left-padding
*/
public static String padLeft(String stringToBePadded, int amountOfPadding) {
return String.format("%" + amountOfPadding + "s", stringToBePadded);
}
/**
* @param stringToBePadded - string value to be flushed left
* @param amountOfPadding - amount of padding to be flushed right
* @return `stringToBePadded` flushed right by right-padding
*/
public static String padRight(String stringToBePadded, int amountOfPadding) {
return String.format("%" + -amountOfPadding + "s", stringToBePadded);
}
/**
* @param stringToBeRepeated - string value to be repeated
* @param numberOfTimeToRepeat - number of times to repeat `stringToBeRepeated`
* @return the string repeated and concatenated `n` times
*/
public static String repeatString(String stringToBeRepeated, int numberOfTimeToRepeat) {
String repeated = new String(new char[numberOfTimeToRepeat]).replace("\0", stringToBeRepeated);
return repeated;
}
/**
* @param string - string to be evaluated
* @return - true if string only contains alpha characters
*/
public static Boolean isAlphaString(String string) {
string = string.replaceAll("\\s+", "");
if(string.chars().allMatch(Character::isLetter))
return true;
else
return false;
}
/**
* @param string - string to be evaluated
* @return - true if string only contains numeric characters
*/
public static Boolean isNumericString(String string) {
string = string.replaceAll("\\s+", "");
if(string.chars().allMatch(Character::isDigit))
return true;
else
return false;
}
/**
* @param string - string to be evaluated
* @return - true if string only contains special characters
*/
public static Boolean isSpecialCharacterString(String string) {
string = string.replaceAll("\\s+", "");
if(string != null && string.chars().noneMatch(Character::isAlphabetic) && string.chars().noneMatch(Character::isDigit))
return true;
else
return false;
}
}