Skip to content

Commit e06c985

Browse files
author
Deepak Malik
committed
Some more string utils methods
1 parent 9e4f28d commit e06c985

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

src/com/deepak/data/structures/Utils/StringUtils.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class StringUtils {
1818
* @return {@link boolean}
1919
*/
2020
public static boolean isEmpty(final CharSequence cs) {
21+
/* String is empty is it is null or length is 0 */
2122
return cs == null || cs.length() == 0;
2223
}
2324

@@ -28,9 +29,11 @@ public static boolean isEmpty(final CharSequence cs) {
2829
* @return {@link boolean}
2930
*/
3031
public static boolean isAnyEmpty(final CharSequence... css) {
32+
/* Input has to be array */
3133
if (ArrayUtils.isEmpty(css)) {
3234
return false;
3335
}
36+
/* Check for each character */
3437
for (CharSequence cs : css) {
3538
if (isEmpty(cs)) {
3639
return true;
@@ -48,9 +51,11 @@ public static boolean isAnyEmpty(final CharSequence... css) {
4851
*/
4952
public static boolean isBlank(final CharSequence cs) {
5053
int strLen;
54+
/* If string is null or length is 0 */
5155
if (cs == null || (strLen = cs.length()) == 0) {
5256
return true;
5357
}
58+
/* There should not be any white space character */
5459
for (int i = 0; i < strLen; i++) {
5560
if (Character.isWhitespace(cs.charAt(i)) == false) {
5661
return false;
@@ -66,9 +71,11 @@ public static boolean isBlank(final CharSequence cs) {
6671
* @return {@link boolean}
6772
*/
6873
public static boolean isAnyBlank(final CharSequence... css) {
74+
/* Input has to be array */
6975
if (ArrayUtils.isEmpty(css)) {
7076
return true;
7177
}
78+
/* Check for each character, if it is blank */
7279
for (CharSequence cs : css) {
7380
if (isBlank(cs)) {
7481
return true;
@@ -84,6 +91,7 @@ public static boolean isAnyBlank(final CharSequence... css) {
8491
* @return {@link String}
8592
*/
8693
public static String trim(final String str) {
94+
/* If string is null, return null else trim */
8795
return str == null ? null : str.trim();
8896
}
8997

@@ -95,15 +103,80 @@ public static String trim(final String str) {
95103
* @return {@link String}
96104
*/
97105
public static String truncate(final String str, final int maxWidth) {
98-
return str;
106+
return truncate(str, 0, maxWidth);
99107
}
100108

109+
/**
110+
* Method to truncate the string when max width
111+
* and offset is given
112+
*
113+
* @param str
114+
* @param offset
115+
* @param maxWidth
116+
* @return {@link String}
117+
*/
118+
public static String truncate(final String str, final int offset, final int maxWidth) {
119+
/* Offset and max width can't be less then 0 */
120+
if (offset < 0) {
121+
throw new IllegalArgumentException("offset cannot be negative");
122+
}
123+
if (maxWidth < 0) {
124+
throw new IllegalArgumentException("maxWidth cannot be negative");
125+
}
126+
/* If string is null, return null */
127+
if (str == null) {
128+
return null;
129+
}
130+
/* If offset is greater then list length, return empty string */
131+
if (offset > str.length()) {
132+
return "";
133+
}
134+
/* If valid scenario, find where we have to stop */
135+
if (str.length() > maxWidth) {
136+
final int ix = offset + maxWidth > str.length() ? str.length() : offset + maxWidth;
137+
return str.substring(offset, ix);
138+
}
139+
return str.substring(offset);
140+
}
141+
142+
/**
143+
* Method to check if two strings are equal
144+
*
145+
* @param cs1
146+
* @param cs2
147+
* @return {@link boolean}
148+
*/
101149
public static boolean equals(final CharSequence cs1, final CharSequence cs2) {
150+
/* If there reference is same, they are equal */
151+
if (cs1 == cs2) {
152+
return true;
153+
}
154+
/* If any one of them is null, they can't be equal */
155+
if (cs1 == null || cs2 == null) {
156+
return false;
157+
}
158+
/* If there lengths are different, they can't be equal */
159+
if (cs1.length() != cs2.length()) {
160+
return false;
161+
}
162+
/* Now, both of them has to be string and equal in value */
163+
if (cs1 instanceof String && cs2 instanceof String) {
164+
return cs1.equals(cs2);
165+
}
102166
return false;
103167
}
104168

169+
/**
170+
* Method to check if two strings are equal when cases are ignored
171+
*
172+
* @param str1
173+
* @param str2
174+
* @return {@link boolean}
175+
*/
105176
public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequence str2) {
106-
return false;
177+
String updatedStr1 = String.valueOf(str1).toLowerCase();
178+
String updatedStr2 = String.valueOf(str2).toLowerCase();
179+
return equals(updatedStr1, updatedStr2);
107180
}
108181

109182
public static int compare(final String str1, final String str2) {

test/com/deepak/data/structures/Utils/StringUtilsTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,42 @@ public void testTrim() {
8080
Assert.assertEquals(StringUtils.trim(" abc "), "abc");
8181
}
8282

83+
/**
84+
* Test case to check truncation of string
85+
*/
86+
@Test
87+
public void testTruncate() {
88+
Assert.assertEquals(StringUtils.truncate(null, 0), null);
89+
Assert.assertEquals(StringUtils.truncate(null, 2), null);
90+
Assert.assertEquals(StringUtils.truncate("", 4), "");
91+
Assert.assertEquals(StringUtils.truncate("abcdefg", 4), "abcd");
92+
Assert.assertEquals(StringUtils.truncate("abcdefg", 6), "abcdef");
93+
Assert.assertEquals(StringUtils.truncate("abcdefg", 7), "abcdefg");
94+
Assert.assertEquals(StringUtils.truncate("abcdefg", 8), "abcdefg");
95+
}
96+
97+
/**
98+
* Method to test equals
99+
*/
100+
@Test
101+
public void testEquals() {
102+
Assert.assertTrue(StringUtils.equals(null, null));
103+
Assert.assertTrue(StringUtils.equals("abc", "abc"));
104+
Assert.assertFalse(StringUtils.equals(null, "abc"));
105+
Assert.assertFalse(StringUtils.equals("abc", null));
106+
Assert.assertFalse(StringUtils.equals("abc", "ABC"));
107+
}
108+
109+
/**
110+
* Method to test equals ignore case
111+
*/
112+
@Test
113+
public void testEqualsIgnoreCase() {
114+
Assert.assertTrue(StringUtils.equalsIgnoreCase(null, null));
115+
Assert.assertTrue(StringUtils.equalsIgnoreCase("abc", "abc"));
116+
Assert.assertFalse(StringUtils.equalsIgnoreCase(null, "abc"));
117+
Assert.assertFalse(StringUtils.equalsIgnoreCase("abc", null));
118+
Assert.assertTrue(StringUtils.equalsIgnoreCase("abc", "ABC"));
119+
}
120+
83121
}

0 commit comments

Comments
 (0)