Java Token Package
Platform: Java
Published Mar 07, 2012
Updated Mar 07, 2012
In an attempt to cut down in the amount of regex, I decided to make Java replicas of mIRC's token identifiers. This is intended to be integrated as a package to be referenced using Java's "import" command. Method commands are as follows:
Java Token Commands
---------------------------------
- getTok(STRING, N(int), DELIMITER);
- numTok(STRING, DELIMITER);
- findTok(STRING, TOKEN, N(int), DELIMITER);
- wildTok(STRING, PATTERN, N(int), DELIMITER);
- putTok(STRING, TOKEN, N(int), DELIMITER);
I will add to these as I need them, I put in the most useful for now. Calling these works as any constructor does. Store the constructor in a variable and call the methods. or you can call them through "Token.getTok();"
Note: See --
http://www.jarticles.com/package/package_eng.html -- on how to setup packaging infrastructures to utilize this snippet as it was designed to. Also note, all Java rules apply; this rule is especially present while using matching patterns for wildTok(); However, the DELIMITER parameter, is represented by a character rather than a number.
public class Token {
public static void main(String[] args) { }
public String getTok(String s, int n, String d) { //getTok(String, N, Delimiter);
String[] gT = s.split(d);
return gT[n];
}
public int numTok(String s, String d) { //numTok(String, Delimiter);
String[] nT = s.split(d);
return nT.length;
}
public int findTok(String s, String t, int n, String d) { //findTok(String, Token, N, Delimiter);
int a = 0, total = 0, match = 0;
String[] fT = s.split(d);
try {
while (a <= fT.length) {
if (fT[a].equalsIgnoreCase(t)) {
match = a;
total++;
if (total == n) { a = fT.length; }
}
a++;
}
} catch (Exception e) { }
if (n == 0) { return total; }
else { return match; }
}
public String wildTok(String s, String p, int n, String d) { //wildTok(String, Pattern, N, Delimiter);
String[] wT = s.split(d);
int a = 0, total = 0;
String match = "";
try {
while (a <= wT.length) {
if (wT[a].matches(p)) {
match = wT[a];
total++;
if (total == n) { a = wT.length; }
}
a++;
}
} catch (Exception e) { }
if (n == 0) { return Integer.toString(total); }
else { return match; }
}
public String putTok(String s, String t, int n, String d) { //putTok(String, Token, N, Delimiter);
String[] pT = s.split(d);
String out = "";
pT[n] = t;
try {
for (int a=0;a<=pT.length;a++) {
if (a == pT.length) { out = out + pT[a]; }
else { out = out + pT[a] + " "; }
}
} catch (Exception e) { }
return out;
}
}