zoukankan      html  css  js  c++  java
  • Java StringTokenizer Example

    In Java, you can use StringTokennizer class to split a String into different tokenas by defined delimiter.(space is the default delimiter). Here’re two StringTokennizer examples :

    Example 1

    Uses StringTokennizer to split a string by “space” and “comma” delimiter, and iterate the StringTokenizer elements and print it out one by one.

    package com.mkyong;
     
    import java.util.StringTokenizer;
     
    public class App {
        public static void main(String[] args) {
     
            String str = "This is String , split by StringTokenizer, created by mkyong";
            StringTokenizer st = new StringTokenizer(str);
     
            System.out.println("---- Split by space ------");
            while (st.hasMoreElements()) {
                System.out.println(st.nextElement());
            }
     
            System.out.println("---- Split by comma ',' ------");
            StringTokenizer st2 = new StringTokenizer(str, ",");
     
            while (st2.hasMoreElements()) {
                System.out.println(st2.nextElement());
            }
        }
    }

    Output

    ---- Split by space ------
    This
    is
    String
    ,
    split
    by
    StringTokenizer,
    created
    by
    mkyong
    ---- Split by comma ',' ------
    This is String 
     split by StringTokenizer
     created by mkyong

    Example 2

    Read a csv file and use StringTokenizer to split the string by “|” delimiter, and print it out.

    File : c:/test.csv

    package com.mkyong;
     
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.StringTokenizer;
     
    public class ReadFile {
     
        public static void main(String[] args) {
     
        BufferedReader br = null;
     
        try {
     
            String line;
     
            br = new BufferedReader(new FileReader("test.csv"));
     
            while ((line = br.readLine()) != null) {
               System.out.println(line);
     
               StringTokenizer stringTokenizer = new StringTokenizer(line, "|");
     
               while (stringTokenizer.hasMoreElements()) {
     
                Integer id = Integer.parseInt(stringTokenizer.nextElement().toString());
                Double price = Double.parseDouble(stringTokenizer.nextElement().toString());
                String username = stringTokenizer.nextElement().toString();
     
                StringBuilder sb = new StringBuilder();
                sb.append("
    Id : " + id);
                sb.append("
    Price : " + price);
                sb.append("
    Username : " + username);
                sb.append("
    *******************
    ");
     
                System.out.println(sb.toString());
               }
            }
     
            System.out.println("Done");
     
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
     
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
     
        }
    }

    Output

    1| 3.29| mkyong
     
    Id : 1
    Price : 3.29
    Username :  mkyong
    *******************
     
    2| 4.345| eclipse
     
    Id : 2
    Price : 4.345
    Username :  eclipse
    *******************
     
    Done
  • 相关阅读:
    写了一个单链表的代码,而且支持反转链表,分组反转链表
    【Redis】redis分布式锁(二)
    【Redis】redis分布式锁(一)
    【Flutter】跟着flutter教程学着写了一个简单的Demo
    【Zookeeper】Zookeeper集群环境搭建
    【TDengine】TDengine初探
    【Shell】一个可以服务拉起、停止和重启的shell脚本
    【Linux】xftp报“找不到匹配的outgoing encryption算法”的错误
    【Linux】Ubuntu如何开启ftp服务器
    【Jenkins】使用Jenkins编译打包后自动部署项目
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4047301.html
Copyright © 2011-2022 走看看