StringTokenizer的用法及示例

StringTokenizer类:

根据自定义字符为分界符进行拆分,并将结果进行封装提供对应方法进行遍历取值

StringTokenizer方法不区分标识符、数和带引号的字符串,它们也不识别并跳过注释;该方法用途类似于split方法,只是对结果进行了封装;


帮助文档上对此进行了说明:

StringTokenizer 是出于兼容性的原因而被保留的遗留类(在新代码中并不鼓励使用它)。

可使用 String 的 split 方法或 java.util.regex 包替代。


构造器: 
     · public StringTokenizer(String str,String delim,boolean returnTokens); 
     · public StringTokenizer(String str,String delim); 
     · public StringTokenizer(String str); 


StringTokenizer(String str)

构造一个用来解析str的StringTokenizer对象。java默认的分隔符是“空格”、“制表符(‘\t’)”、“换行符(‘\n’)”、“回车符(‘\r’)”。


StringTokenizer(String str,String delim)

构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符。


public StringTokenizer(String str,String delim,boolean returnTokens)

构造一个用来解析str的StringTokenizer对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。



     其中,str为需分析的字符串,delim为定界符,定界符里面的任何一个字符都是分隔符,如"+-*/   "表示碰到"+-*/   "中的任何一个都认为是到达了分隔符了。   
     常用的方法有: 
     · public boolean hasMoreTokens(); //判断字符串中是含还有token 
     · public String nextToken(); //StringTokenizer对象中的下一个token。 
     · public String nextToken(String delim) //delim为新的定界符

      public boolean hasMoreElements ()


在例中,使用StringTokenizer类将字符串“aa,bb,cc,dd,ff”分解为三个单词。 


 【例】
     impoort java.util.*;
        public class StringTokenizer
 Demo 
        { 
              public static void main(String[] args)
              { 
                  String str="
 hello,java,delphi,asp,php";
                  StringTokenizer st=new StringTokenizer(str,",");
                  while(st.hasMoreTokens()) { 
                      System.out.println(st.nextToken());
                  }
              }
          } 
    程序的运行结果为:
          aa
          bb
          cc
          dd

          ff





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ZSCPascal AppWizard has created this ZSCPascal application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ZSCPascal application. ZSCPascal.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CZSCPascalApp application class. ZSCPascal.cpp This is the main application source file that contains the application class CZSCPascalApp. ZSCPascal.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Developer Studio. resSCPascal.ico This is an icon file, which is used as the application s icon. This icon is included by the main resource file ZSCPascal.rc. resSCPascal.rc2 This file contains resources that are not edited by Microsoft Developer Studio. You should place all resources not editable by the resource editor in this file. ZSCPascal.reg This is an example .REG file that shows you the kind of registration settings the framework will set for you. You can use this as a .REG file to go along with your application or just delete it and rely on the default RegisterShellFileTypes registration. ZSCPascal.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. For the main frame window: MainFrm.h, MainFrm.cpp These files contain the frame class CMainFrame, which is derived from CMDIFrameWnd and controls all MDI frame features. resToolbar.bmp This bitmap file is used to create tiled images for the toolbar. The initial toolbar and status bar are constructed in the CMainFrame class. Edit this toolbar bitmap along with the array in MainFrm.cpp to add more toolbar buttons. AppWizard creates one document type and one view: ZSCPascalDoc.h, ZSCPascalDoc.cpp - the document These files contain your CZSCPascalDoc class. Edit these files to add your special document data and to implement file saving and loading (via CZSCPascalDoc::Serialize). ZSCPascalView.h, ZSCPascalView.cpp - the view of the document These files contain your CZSCPascalView class. CZSCPascalView objects are used to view CZSCPascalDoc objects. resSCPascalDoc.ico This is an icon file, which is used as the icon for MDI child windows for the CZSCPascalDoc class. This icon is included by the main resource file ZSCPascal.rc. Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ZSCPascal.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Developer Studio reads and updates this file. Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system s current language, you will need to copy the corresponding localized resources MFC40XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC40DEU.DLL contains resources translated to German.) If you don t do this, some of the UI elements of your application will remain in the language of the operating system.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值