首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在java中将特定的单词与字符串分开。

在java中将特定的单词与字符串分开。
EN

Stack Overflow用户
提问于 2016-08-25 03:53:29
回答 4查看 89关注 0票数 0

java中,有没有办法将单词从字符串中拆分出来?

代码语言:javascript
运行
复制
String my ="StackOverFlow PF Apple FT Laptop HW."

PF =平台,FT =水果,HW =硬件。

预期输出应为

代码语言:javascript
运行
复制
StackOverFlow  is a Platform.
Apple  is a Fruit.
Laptop is a hardware.

我是这样做的:

代码语言:javascript
运行
复制
String[] words = my.split(" ");
  for(int u=0 ; u<words.length ; u++){
      System.out.println( words(u));
  }
EN

Stack Overflow用户

发布于 2016-08-25 04:23:59

考虑到你问题中的有限规格,没有理由分开。你可以像这样替换你的占位符:

代码语言:javascript
运行
复制
String my = "StackOverFlow PF Apple FT Laptop HW.";
my = my.replaceAll("PF[\\s.]?", "  is a Platform.\n");
my = my.replaceAll("FT[\\s.]?", "  is a Fruit.\n");
my = my.replaceAll("HW[\\s.]?", " is a hardware.\n");
System.out.print(my);

输出:

代码语言:javascript
运行
复制
StackOverFlow   is a Platform.
Apple   is a Fruit.
Laptop  is a hardware.
票数 0
EN
查看全部 4 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39131976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档