Java源码-一个简单的分词器(Tokenizing Telephone Numbers)

来源:互联网 发布:php个人简历源代码 编辑:程序博客网 时间:2024/04/30 20:22

上个项目是个互联网项目,一个网页上,可能七七八八的加载了很多项功能,包括全文检索功能中的“切词(分词)”逻辑,算是长见识了。

不过,完成下面这个练习后,分词(tokenization)对我而言亲和了许多,说白了,就是字符串拆分。


运行结果:

请输入字符串:86-139-178-67138-1650
请输入分隔符:-
切词结果:86,139,178,67138,1650,
串接结果:86139178671381650


代码如下:

import java.util.*;/**Java how to program, 10th edition 14.8 (Tokenizing Telephone Numbers) Write an application that inputs a telephone  number as a string in the form (555) 555-5555. The application should use String   method split to extract the area code as a token, the first three digits of the    phone number as a token and the last four digits of the phone number as a token.     The seven digits of the phone number should be concatenated into one string.      Both the area code and the phone number should be printed. Remember that you’ll      have to change delimiter characters during the tokenization process. *  @author pandenghuang@163.com*/public class Tokenization {   public static void main(String[] args)   {   Scanner input=new Scanner(System.in);   System.out.print("请输入字符串:");   String s=input.nextLine();   System.out.print("请输入分隔符:");   String separator=input.nextLine();   String[] tokens=s.split(separator);      System.out.printf("切词结果:");   for (String t:tokens)   System.out.print(t+",");      String combined="";   for (String t:tokens)   combined+=t;   System.out.printf("%n串接结果:%s%n",combined);   } }


0 0
原创粉丝点击