File tree Expand file tree Collapse file tree 2 files changed +61
-0
lines changed
main/java/hackerrank/java
test/java/hackerranktest/java Expand file tree Collapse file tree 2 files changed +61
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments