Last active
June 27, 2018 16:02
-
-
Save ivarprudnikov/392e3b62e26f4e715adc1038195073c8 to your computer and use it in GitHub Desktop.
Stringtemplate example showing how to extract individual characters from a "String"
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
@Grapes([ | |
@Grab('org.antlr:ST4:4.0.8'), | |
@GrabConfig(systemClassLoader = true)]) | |
import org.stringtemplate.v4.ST; | |
Word word = new Word("1234567890AB") | |
println word.getTomap() | |
/** | |
* Test for splitting up a long string into delimited sections. 12-digit number to | |
* be presented like "CAMS-000-00000000-0" | |
*/ | |
ST hello = new ST("CAMS-<cams.tomap.0><cams.tomap.1><cams.tomap.2>-<cams.tomap.3><cams.tomap.4><cams.tomap.5><cams.tomap.6><cams.tomap.7><cams.tomap.8><cams.tomap.9><cams.tomap.10>-<cams.tomap.11>"); | |
hello.add("cams", word); | |
println hello.render() // outputs CAMS-123-4567890A-B | |
class Word { | |
final String v | |
Word(String v) { | |
this.v = v | |
} | |
Map<String, String> getTomap() { | |
Map<String, String> out = new HashMap<>(); | |
String[] values = this.v.split("(?!^)"); | |
for (int i = 0; i < values.length; i++) { | |
out.put(i + "", values[i]) | |
} | |
return out | |
} | |
String toString() { | |
return this.v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment