Skip to content

Commit d7f8bc5

Browse files
committed
Some more string util methods
1 parent e06c985 commit d7f8bc5

File tree

2 files changed

+90
-4
lines changed

2 files changed

+90
-4
lines changed

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

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,74 @@ public static boolean equalsIgnoreCase(final CharSequence str1, final CharSequen
179179
return equals(updatedStr1, updatedStr2);
180180
}
181181

182+
/**
183+
* Method to compare two strings
184+
*
185+
* @param str1
186+
* @param str2
187+
* @return {@link int}
188+
*/
182189
public static int compare(final String str1, final String str2) {
183-
return 0;
190+
return compare(str1, str2, true);
184191
}
185192

186-
public static int indexOf(final CharSequence seq, final int searchChar) {
193+
/**
194+
* Method to compare two strings when null has to be considered or not
195+
*
196+
* @param str1
197+
* @param str2
198+
* @param shouldConsiderNull
199+
* @return {@link int}
200+
*/
201+
public static int compare(final String str1, final String str2, final boolean shouldConsiderNull) {
202+
/* If both are same, return 0 */
203+
if (str1 == str2) {
204+
return 0;
205+
}
206+
/* If str1 is less, return < 0 */
207+
if (str1 == null) {
208+
return shouldConsiderNull ? -1 : 1;
209+
}
210+
/* If str2 is less, return > 0 */
211+
if (str2 == null) {
212+
return shouldConsiderNull ? 1 : -1;
213+
}
214+
return str1.compareTo(str2);
215+
}
187216

188-
return 0;
217+
/**
218+
* Method to find the index of first occurrence of character in a string
219+
*
220+
* @param seq
221+
* @param searchChar
222+
* @return {@link int}
223+
*/
224+
public static int indexOf(final CharSequence seq, final int searchChar) {
225+
return indexOf(seq, searchChar, 0);
189226
}
190227

228+
/**
229+
* Method to find the index of a character after a given position
230+
*
231+
* @param seq
232+
* @param searchChar
233+
* @param startPos
234+
* @return {@link int}
235+
*/
191236
public static int indexOf(final CharSequence seq, final int searchChar, final int startPos) {
192-
return 0;
237+
if (isEmpty(seq)) {
238+
throw new IllegalArgumentException("Invalid character sequence!");
239+
}
240+
if (startPos < seq.length()) {
241+
for (int i = startPos; i < seq.length(); i++) {
242+
/* If a negative start position is given, keep jumping until you reach 0 */
243+
if (i < 0) continue;
244+
if (seq.charAt(i) == searchChar) {
245+
return i;
246+
}
247+
}
248+
}
249+
return -1;
193250
}
194251

195252
public static int lastIndexOf(final CharSequence seq, final int searchChar) {

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,33 @@ public void testEqualsIgnoreCase() {
118118
Assert.assertTrue(StringUtils.equalsIgnoreCase("abc", "ABC"));
119119
}
120120

121+
/**
122+
* Method to test comparison of 2 strings
123+
*/
124+
@Test
125+
public void testCompare() {
126+
Assert.assertEquals(StringUtils.compare(null, null), 0);
127+
Assert.assertEquals(StringUtils.compare(null, "a"), -1);
128+
Assert.assertEquals(StringUtils.compare("a", null), 1);
129+
Assert.assertEquals(StringUtils.compare("abc", "abc"), 0);
130+
Assert.assertEquals(StringUtils.compare("a", "b"), -1);
131+
Assert.assertEquals(StringUtils.compare("b", "a"), 1);
132+
Assert.assertEquals(StringUtils.compare("ab", "abc"), -1);
133+
}
134+
135+
/**
136+
* Method to test index of in a string
137+
*/
138+
@Test(expected = IllegalArgumentException.class)
139+
public void testIndexOf() {
140+
Assert.assertEquals(StringUtils.indexOf("aadjsjh", 'a'), 0);
141+
Assert.assertEquals(StringUtils.indexOf("aabaabaa", 'b'), 2);
142+
Assert.assertEquals(StringUtils.indexOf("aabaabaa", 'b', 0), 2);
143+
Assert.assertEquals(StringUtils.indexOf("aabaabaa", 'b', 3), 5);
144+
Assert.assertEquals(StringUtils.indexOf("aabaabaa", 'b', 9), -1);
145+
Assert.assertEquals(StringUtils.indexOf("aabaabaa", 'b', -1), 2);
146+
Assert.assertEquals(StringUtils.indexOf(null, 'a'), -1);
147+
Assert.assertEquals(StringUtils.indexOf("", 'a'), -1);
148+
}
149+
121150
}

0 commit comments

Comments
 (0)