-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package de.jlo.talendcomp.sap; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class TextSplitter { | ||
|
||
public static List<String> split(String text, char delimiter) { | ||
List<String> result = new ArrayList<>(); | ||
if (text != null) { | ||
text = text.trim(); | ||
boolean inLiteral = false; | ||
int i = 0; | ||
StringBuilder sb = new StringBuilder(); | ||
for (; i < text.length(); i++) { | ||
char c = text.charAt(i); | ||
if (c == '\'') { | ||
if (inLiteral == false) { | ||
inLiteral = true; | ||
} else { | ||
inLiteral = false; | ||
} | ||
} | ||
if (inLiteral) { | ||
sb.append(c); | ||
} else { | ||
if (c == delimiter) { | ||
// found delimiter to cut | ||
// but ignore 2 following delimiter | ||
String part = sb.toString(); | ||
if (part.trim().isEmpty() == false) { | ||
result.add(part); | ||
} | ||
sb.setLength(0); // reset string buffer | ||
} else { | ||
sb.append(c); | ||
} | ||
} | ||
} | ||
String lastPart = sb.toString(); | ||
if (lastPart.trim().isEmpty() == false) { | ||
result.add(lastPart); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/test/java/de/jlo/talendcomp/sap/impl/TestTextSplitter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package de.jlo.talendcomp.sap.impl; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
import de.jlo.talendcomp.sap.TextSplitter; | ||
|
||
public class TestTextSplitter { | ||
|
||
@Test | ||
public void test1() { | ||
String input = "';';T1;T2; ;T3"; | ||
List<String> result = TextSplitter.split(input, ';'); | ||
for (String s : result) { | ||
System.out.println(">" + s); | ||
} | ||
assertTrue("Count parts incorrect: " + result.size(), result.size() == 4); | ||
} | ||
|
||
@Test | ||
public void test2() { | ||
String input = "';'"; | ||
List<String> result = TextSplitter.split(input, ';'); | ||
for (String s : result) { | ||
System.out.println(">" + s); | ||
} | ||
assertTrue("Count parts incorrect: " + result.size(), result.size() == 1); | ||
} | ||
|
||
@Test | ||
public void test3() { | ||
String input = "abcd efg"; | ||
List<String> result = TextSplitter.split(input, ';'); | ||
for (String s : result) { | ||
System.out.println(">" + s); | ||
} | ||
assertTrue("Count parts incorrect: " + result.size(), result.size() == 1); | ||
} | ||
|
||
@Test | ||
public void test4() { | ||
String input = ""; | ||
List<String> result = TextSplitter.split(input, ';'); | ||
for (String s : result) { | ||
System.out.println(">" + s); | ||
} | ||
assertTrue("Count parts incorrect: " + result.size(), result.size() == 0); | ||
} | ||
|
||
@Test | ||
public void test5() { | ||
String input = " ; "; | ||
List<String> result = TextSplitter.split(input, ';'); | ||
for (String s : result) { | ||
System.out.println(">" + s); | ||
} | ||
assertTrue("Count parts incorrect: " + result.size(), result.size() == 0); | ||
} | ||
|
||
} |