61 lines
1.8 KiB
Plaintext
61 lines
1.8 KiB
Plaintext
module Substrings {
|
|
void run(String[] args = []) {
|
|
@Inject Console console;
|
|
if (args.size < 4) {
|
|
console.print(
|
|
$|
|
|
|Usage:
|
|
|
|
|
| xec Substrings <str> <offset> <count> <substr>
|
|
|
|
|
);
|
|
return;
|
|
}
|
|
|
|
String s = args[0];
|
|
Int n = new Int(args[1]);
|
|
Int m = new Int(args[2]);
|
|
String sub = args[3];
|
|
Char c = sub[0];
|
|
|
|
console.print($|
|
|
|{s .quoted()=}
|
|
|{substring(s, n, m ).quoted()=}
|
|
|{substring(s, n ).quoted()=}
|
|
|{substring(s ).quoted()=}
|
|
|{substring(s, c, m ).quoted()=}
|
|
|{substring(s, sub, m).quoted()=}
|
|
|
|
|
);
|
|
}
|
|
|
|
// starting from n characters in and of m length
|
|
static String substring(String s, Int n, Int m) {
|
|
assert 0 <= n <= n+m;
|
|
return n < s.size ? s[n..<(n+m).notGreaterThan(s.size)] : "";
|
|
}
|
|
|
|
// starting from n characters in, up to the end of the string
|
|
static String substring(String s, Int n) {
|
|
assert 0 <= n;
|
|
return s.substring(n);
|
|
}
|
|
|
|
// whole string minus the last character
|
|
static String substring(String s) {
|
|
return s.size > 1 ? s[0..<s.size-1] : "";
|
|
}
|
|
|
|
// starting from a known character within the string and of m length
|
|
static String substring(String s, Char c, Int m){
|
|
assert 0 <= m;
|
|
return substring(s, s.indexOf(c) ?: 0, m);
|
|
}
|
|
|
|
// starting from a known substring within the string and of m length
|
|
static String substring(String s, String sub, Int m){
|
|
assert 0 <= m;
|
|
return substring(s, s.indexOf(sub) ?: 0, m);
|
|
}
|
|
}
|