字符串标记化

时间:2012-10-05 08:59:25

标签: java string

我想将字符串private String requestDate="12/12/2012 02:02:23 AM";标记为

  

12122012_020223am _

如何使用stringTokenizer实现此目的?

此致

3 个答案:

答案 0 :(得分:2)

requestDate = requestDate.replaceAll("/", "");
requestDate = requestDate.replaceAll(" ", "_");
requestDate = requestDate.replaceAll(":", "");

会给你12122012_020223_AM

然后requestDate = requestDate.toLowerCase()

将给出12122012_020223_am

答案 1 :(得分:1)

使用SimpleDateFormat

Date date = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a").parse(requestDate);
String output = new SimpleDateFormat("MMddyyyy_hhmmssa_").format(date);
output = output.toLowerCase();

答案 2 :(得分:0)

如果你想要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 constructors采用字符串和分隔符