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

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

不过,完成下面这个练习后,分词(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值