我想将字符串标记private String requestDate="12/12/2012 02:02:23 AM";
为
12122012_020223am_
如何使用 stringTokenizer 实现这一点?
问候,
requestDate = requestDate.replaceAll("/", "");
requestDate = requestDate.replaceAll(" ", "_");
requestDate = requestDate.replaceAll(":", "");
会给你12122012_020223_AM
然后 requestDate = requestDate.toLowerCase()
会给12122012_020223_am
Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(requestDate);
String output = new SimpleDateFormat("MMddyyyy_hhmmssa_").format(date);
output = output.toLowerCase();
如果你想要 StringTokenise 这样的事情应该这样做
@Test
public void test() {
StringTokenizer stringTokenizer = new StringTokenizer("ab/c/d EF", "/");
StringBuilder b = new StringBuilder();
while (stringTokenizer.hasMoreTokens()) {
StringTokenizer stringTokenizer2 = new StringTokenizer(stringTokenizer.nextToken(), " ");
while (stringTokenizer2.hasMoreTokens()) {
b.append(stringTokenizer2.nextToken().toLowerCase());
b.append("_");
}
}
assertEquals("ab_c_d_ef_", b.toString());
}
StringTokenizer构造函数之一采用字符串和分隔符