Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d0c8657

Browse files
committed
1 parent 9c9446a commit d0c8657

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package hackerrank.java;
2+
3+
public class StringToken {
4+
private String sentence;
5+
6+
public StringToken(final String sentence) {
7+
this.sentence = sentence;
8+
}
9+
10+
public String[] tokenFilter() {
11+
12+
if( sentence == null || sentence.isEmpty()
13+
|| sentence.trim().isEmpty()) {
14+
return new String[]{};
15+
}
16+
String[] splitStr = sentence.trim().split("[,\\s\\'\\?\\_\\@\\!\\.]+");
17+
18+
return splitStr;
19+
}
20+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package hackerranktest.java;
2+
3+
import hackerrank.java.StringToken;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
8+
/**
9+
*
10+
* Problem
11+
* https://www.hackerrank.com/challenges/java-string-tokens/
12+
*
13+
*/
14+
public class StringTokensTest {
15+
16+
@Test
17+
void givenASentencePrintWithoutTokens() {
18+
String given = "He is a very very good boy, isn't he?";
19+
String expected = "Heisaveryverygoodboyisnthe";
20+
21+
StringToken obj = new StringToken(given);
22+
String[] actualArr = obj.tokenFilter();
23+
String actual = String.join("", actualArr);
24+
25+
Assertions.assertEquals(10, actualArr.length);
26+
Assertions.assertEquals(expected, actual);
27+
}
28+
29+
@Test
30+
void givenAnEmptyStringPrintReturnEmptyString() {
31+
String given = "";
32+
String expected = "";
33+
34+
StringToken obj = new StringToken(given);
35+
String[] actualArr = obj.tokenFilter();
36+
String actual = String.join("", actualArr);
37+
38+
Assertions.assertEquals(0, actualArr.length);
39+
Assertions.assertEquals(expected, actual);
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /