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. 遇到NotificationService: Suppressing notification from package com.example.dell.servicebestpractice by u错误

    一般来说是手机设置没有允许通知

  2. Vue使用Typescript开发编译时提示“ERROR in ./src/main.ts Module build failed: TypeError: Cannot read property 'afterCompile' of undefined”的解决方法

    使用Typescript开发Vue,一切准备就绪.但npm start 时,提示“ ERROR in ./src/main.tsModule build failed: TypeError: Cann ...

  3. HBase 笔记3

    数据模型 Namespace 表命名空间: 多个表分到一个组进行统一的管理,需要用到表命名空间 表命名空间主要是对表分组,对不同组进行不同环境设定,如配额管理  安全管理 保留表空间: HBase中有 ...

  4. linux 下tftpf搭建

     什么是TFTP服务 TFTP(Trivial File Transfer Protocol,简单文件传输协议)   是TCP/IP协议族中的一个用来在客户机与服务器之间进行   简单文件传输的协 ...

  5. 玩转spring boot——负载均衡与session共享

     前言 当项目上线后,如果要修复bug或扩充功能,都需要重启tomcat服务.此时,正在使用应用的用户们就需要等待服务器的重启,而这就会造成不好的用户体验.还有,当仅仅只有一台tomcat服务时,如果 ...

  6. Delphi中有关窗口绘制

    Invalidate方法通知Windows应该重新绘制表单的整个表面.最重要的是Invalidate不会立即强制执行绘制操作. Windows只是存储请求,并且只会响应它当前程序完全执行后,并且只要系 ...

  7. Windbg程序调试系列2-内存泄露问题

    上篇文章给大家解释了Windbg的基本命令和说明,这一篇给大家介绍内存泄露场景的问题分析. 文章大纲: 描述问题背景和现象 确定问题是否是内存泄露 梳理问题分析思路 动手分析解决 总结 1. 先说问题 ...

  8. myeclipse项目在Tomcat服务器部署问题

    错误信息:Deployment of project mybook will replace this resource. Please specify the action you wish to ...

  9. 【js】字符串反转可实现的几种方式

    方式1: 这种方式比较简单,推荐使用 字符串转数组,反转数组,数组转字符串. split(""):根据空字符串拆分数组 reverse():数组反转元素位置 join(" ...

  10. centos6安装配置zabbix3被控端

    Centos 6.5 Zabbix 3.0.4 zabbix分为zabbix-server(主控端)和zabbix-agent(被控端),本文则介绍安装agent和server端添加监控: 主控端添加 ...