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;
}
}

Comments

Sign in to comment.
Marlinc   -  Dec 07, 2012
Great well some people want that
 Respond  
SunnyD   -  Dec 07, 2012
Lol, no. It's all Open Source here. Use it however you like.
 Respond  
Marlinc   -  Dec 07, 2012
So I was thinking about using this in IRC services I'm working on. Do I need to add a LICENSE or something? A reference to this post maybe?
 Respond  
SunnyD   -  Mar 08, 2012
Just a space saver I suppose. It'll come in handy to someone at some point. I used it as a scapegoat when attempting to figure out the packaging tree, that was shockingly easier than I made it out to be...
 Respond  
Hawkee   -  Mar 08, 2012
I guess I just don't use tokens often. If I need to split a string by a character I'll probably use the PHP split() function. Otherwise I just use regular expressions.
 Respond  
SunnyD   -  Mar 08, 2012
Thus far, regex has been my only tool for parsing strings in a manner consistent with what I needed, tokens. Usually it would require somewhat repetitive calls to regex to isolate exactly what I was looking for, whether it be parsing a raw numeric string from an IRC server, or html source code. While getTok() and numTok() are simple array calls, the others provide much more flexibility on a token to token basis; whether that be returning a token, returning the amount of tokens matched, or replacing a token at "N" position.
 Respond  
Hawkee   -  Mar 08, 2012
Great to see some code other than mIRC being posted. What advantage do the mIRC style token functions have over what Java provides by default?
 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.