public class DaXie {
public static void main(String[] args) { String strin = "Hello Java World!!的";
String a[] = GetWorld1(strin);
for (int i = 0;i < a.length;i++){
System.out.println("第"+ i + "个单词" + a[i]);
}
GetWorld2(strin);
}
public static String[] GetWorld1(String strin){
int num = 0;int index = 0;//num表示单词数,index表示空格的索引位置
do {
++num;//至少有一个单词
++index;//至少有一个空格
index = strin.indexOf(' ',index);//搜索空格的索引位置
}while (index != -1);//若没有空格则结束循环 int end = 0;
String strArray[] = new String[num];
index = 0;
for (int i = 0;i < num;i++){
end = strin.indexOf(' ',index);
if (end == -1){
strArray[i] = strin.substring(index);
}else {
//注意 substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串。beginIndex - 起始索引(包括)。从0开始 endIndex - 结束索引(不包括)。
strArray[i] = strin.substring(index,end);
}
index = end+1;
}
return strArray;
}
//使用StringTokenizer
public static void GetWorld2(String hh){
System.out.println("演示StringToKenizer获取字符串中的单词:");
StringTokenizer oo = new StringTokenizer(hh);
while (oo.hasMoreTokens()){
System.out.println(oo.nextToken());
}
}
}

java通过StringToKenizer获取字符串中的单词根据空格分离-详情版的更多相关文章

  1. java通过StringToKenizer获取字符串中的单词根据空格分离-简写版

    public class StringToKenizer { public static void main(String[] args) { String strin = "Hello J ...

  2. Java实现 LeetCode 557 反转字符串中的单词 III(StringBuilder的翻转和分割)

    557. 反转字符串中的单词 III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode c ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. 获取字符串中每个字符出现的次数(利用TreeMap)

    案例:"aababcabcdabcde",获取字符串中每一个字母出现的次数要求结果:a(5)b(4)c(3)d(2)e(1)分析1:定义一个字符串(可以改进为键盘录入)2:定义一个 ...

  5. Leetcode 557.反转字符串中的单词III

    反转字符串中的单词III 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest ...

  6. 力扣(LeetCode)字符串中的单词数 个人题解

    统计字符串中的单词个数,这里的单词指的是连续的不是空格的字符. 请注意,你可以假定字符串里不包括任何不可打印的字符. 示例: 输入: "Hello, my name is John" ...

  7. LeetCode 557:反转字符串中的单词 III Reverse Words in a String III

    公众号:爱写bug(ID:icodebugs) 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. Given a string, you need to reve ...

  8. HashMap 统计一个字符串中每个单词出现的次数

    HashMap 统计一个字符串中每个单词出现的次数 import java.util.HashMap; import java.util.Map; public class Test { public ...

  9. [LeetCode] 151. Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example,Given s = "the sky is blue& ...

随机推荐

  1. poj3133 插头dp

    #include <iostream> #include <cstdio> #include <string.h> #include <vector> ...

  2. 清理Visual Studio 2017的项目历史记录或手工修改Visual Studio 2017的注册表设置

    Visual Studio 2017的"最近的文件列表"和"项目列表"总是删了之后重启电脑又出现(PS:这期间没有打开过项目,更没打开过VS). 一怒之下,按照 ...

  3. GoldenGate抽取Informix数据库安装及配置

    GoldenGate抽取Informix数据库安装及配置 本次测试架构 l  在中间机上安装informix csdk4.10版本,并编译配置unixODBC; l  在中间机上安装ogg for I ...

  4. micropython驱动sh1106点亮oled

    继上一帖给esp32刷入micropython之后,忍不住给以esp12e也刷了micropython 这里先说一下webrepl: 通过wifi可以和esp8266交互,以及便携的传输文件 首次使用 ...

  5. Codeforces Round #503 (by SIS, Div. 2) Solution

    从这里开始 题目列表 瞎扯 Problem A New Building for SIS Problem B Badge Problem C Elections Problem D The hat P ...

  6. Oracle 客户端 NLS_LANG 的设置

    参考链接1: https://blog.csdn.net/xinzhan0/article/details/78311417#t3 参考链接2: https://blog.csdn.net/xinzh ...

  7. Postman接口调试神器

    Postman起初源自googleBrowser的一款插件,现已经有单独软件,当然之前的插件也存在  它主要是用于接口的的调试 接口请求的流程 一.GET请求 GET请求:点击Params,输入参数及 ...

  8. nmon 性能监控网页结果显示——EasyNmon

    首先,看看最终展示的结果显示样式: 报告界面: 1.安装包下载地址:https://github.com/mzky/easyNmon 2.下载后有2个压缩文件: 其中,nmon16g_x86中含有不同 ...

  9. Microsoft Visual Studio已停止工作

    问题:今天在安装Visual Studio时,提示“Visual Studio installer 已停止工作” 解决办法:卸载原有的 .net  framework,在微软官网下载 .net fra ...

  10. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...