<ul class="pc-nav" id="note-nav">
<li><a href="//www.runoob.com/">首页</a></li>
<li><a href="/w3cnote">笔记首页</a></li>
<li><a href="/w3cnote_genre/android" title="Android 基础入门教程">Android</a></li>
<li><a href="/w3cnote_genre/net-2" title="互联网">互联网</a></li>
<li><a href="/w3cnote_genre/coderlife" title="程序员人生">程序员人生</a></li>
<li><a href="/w3cnote_genre/joke" title="程序员笑话">程序员笑话</a></li>
<li><a href="/w3cnote_genre/code" title="编程技术">编程技术</a></li>
<li><a href="https://s.click.taobao.com/VCtS1Lw" target="_blank" data-id="1111">红包 <img style="height: 12px; 24px;" src="//img.alicdn.com/tfs/TB1WzrtiSzqK1RjSZPcXXbTepXa-48-24.png"></a></li>
<!--
<li><a target="_blank" href="/shoppinglist" rel="nofollow">知识店铺</a></li>
-->
</ul>
<ul class="mobile-nav">
<li><a href="/w3cnote">首页</a></li>
<li><a href="/w3cnote_genre/android" target="_blank" title="Android 基础入门教程">Android</a></li>
<li><a href="/w3cnote_genre/coderlife" target="_blank" title="程序员人生">鸡汤</a></li>
<li><a href="/w3cnote_genre/joke" target="_blank" title="程序员笑话">逗乐</a></li>
<a href="javascript:void(0)" class="search-reveal">Search</a>
</ul>
</div>
</div>
<div class="col middle-column big-middle-column">
<div class="article">
<div class="article-heading">
<h2>Java StringTokenizer 类使用方法</h2> <h3><em>分类</em> <a href="/w3cnote_genre/code" title="编程技术">编程技术</a> </h3>
</div>
<div class="article-body note-body">
<div class="article-intro">
Java StringTokenizer 属于 java.util 包,用于分隔字符串。
StringTokenizer 构造方法:
- 1. StringTokenizer(String str) :构造一个用来解析 str 的 StringTokenizer 对象。java 默认的分隔符是空格("")、制表符( )、换行符( )、回车符( )。
- 2. StringTokenizer(String str, String delim) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符。
- 3. StringTokenizer(String str, String delim, boolean returnDelims) :构造一个用来解析 str 的 StringTokenizer 对象,并提供一个指定的分隔符,同时,指定是否返回分隔符。
StringTokenizer 常用方法:
- 1. int countTokens():返回nextToken方法被调用的次数。
- 2. boolean hasMoreTokens():返回是否还有分隔符。
- 3. boolean hasMoreElements():返回是否还有分隔符。
- 4. String nextToken():返回从当前位置到下一个分隔符的字符串。
- 5. Object nextElement():返回从当前位置到下一个分隔符的字符串。
- 6. String nextToken(String delim):与 4 类似,以指定的分隔符返回结果。
实例 1
public class Main
{
public static void main(String[] args)
{
String str = "runoob,google,taobao,facebook,zhihu";
// 以 , 号为分隔符来分隔字符串
StringTokenizer st=new StringTokenizer(str,",");
while(st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}
输出结果为:
runoob
google
taobao
facebook
zhihu
实例 2
public class Main
{
public static void main(String args[])
{
System.out.println("使用第一种构造函数:");
StringTokenizer st1 = new StringTokenizer("Hello Runoob How are you", " ");
while (st1.hasMoreTokens())
System.out.println(st1.nextToken());
</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用第二种构造函数:</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">StringTokenizer</span><span class="hl-code"> </span><span class="hl-identifier">st2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">StringTokenizer</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">JAVA : Code : String</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string"> :</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">st2</span><span class="hl-code">.</span><span class="hl-identifier">hasMoreTokens</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">st2</span><span class="hl-code">.</span><span class="hl-identifier">nextToken</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">使用第三种构造函数:</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-identifier">StringTokenizer</span><span class="hl-code"> </span><span class="hl-identifier">st3</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">StringTokenizer</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">JAVA : Code : String</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-quotes">"</span><span class="hl-string"> :</span><span class="hl-quotes">"</span><span class="hl-code">, </span><span class="hl-reserved">true</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-reserved">while</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">st3</span><span class="hl-code">.</span><span class="hl-identifier">hasMoreTokens</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">st3</span><span class="hl-code">.</span><span class="hl-identifier">nextToken</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span><span class="hl-code">
}
输出结果为:
使用第一种构造函数: Hello Runoob How are you 使用第二种构造函数: JAVA Code String 使用第三种构造函数: JAVA : Code : String
</div>
</div>
<div class="previous-next-links">
<div class="previous-design-link">← <a href="http://www.runoob.com/w3cnote/cpp-func-pointer.html" rel="prev"> C++ 函数指针 & 类成员函数指针</a> </div>
<div class="next-design-link"><a href="http://www.runoob.com/w3cnote/linux-mysql-import-export-data.html" rel="next"> Linux 导入、导出 MySQL 数据库命令</a> →</div>
</div>
<style>
.wrapper {
/*text-transform: uppercase; /
background: #ececec;
color: #555;
cursor: help;
font-family: "Gill Sans", Impact, sans-serif;
font-size: 20px;
position: relative;
text-align: center;
200px;
-webkit-transform: translateZ(0); / webkit flicker fix /
-webkit-font-smoothing: antialiased; / webkit text rendering fix */
}
.wrapper .tooltip {
white-space: nowrap;
font-size: 14px;
text-align: left;
background: #96b97d;
bottom: 100%;
color: #fff;
display: block;
left: -25px;
margin-bottom: 15px;
opacity: 0;
padding: 14px;
pointer-events: none;
position: absolute;
-webkit-transform: translateY(10px);
-moz-transform: translateY(10px);
-ms-transform: translateY(10px);
-o-transform: translateY(10px);
transform: translateY(10px);
-webkit-transition: all .25s ease-out;
-moz-transition: all .25s ease-out;
-ms-transition: all .25s ease-out;
-o-transition: all .25s ease-out;
transition: all .25s ease-out;
-webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
-o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
bottom: -20px;
content: " ";
display: block;
height: 20px;
left: 0;
position: absolute;
100%;
}
/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
border-left: solid transparent 10px;
border-right: solid transparent 10px;
border-top: solid #96b97d 10px;
bottom: -10px;
content: " ";
height: 0;
left: 20%;
margin-left: -13px;
position: absolute;
0;
}
.wrapper .tooltip1 {
margin-left: 50px;
padding-top: 0px;
}
.wrapper:hover .tooltip {
opacity: 1;
pointer-events: auto;
-webkit-transform: translateY(0px);
-moz-transform: translateY(0px);
-ms-transform: translateY(0px);
-o-transform: translateY(0px);
transform: translateY(0px);
}
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
display: none;
}
.lte8 .wrapper:hover .tooltip {
display: block;
}
<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;"><div id="comment-status" style="display:none;"></div>
<div class="comt">
<div class="comt-title">
<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i> <p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
</div>
<div class="comt-box">
<div class="simditor">
<div class="comt-ctrl">
<div class="comt-tips"><input type="hidden" name="comment_post_ID" value="20308" id="comment_post_ID">
<div class="comt-comterinfo">
<ul id="comment-author-info">
<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
</ul>
</div>
</div>
</form>
</div>
</div>
<div class="listcol last right-column">
<div class="sidebar-box cate-list">
<script async="" src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
- 在线实例
- ·HTML 实例
- ·CSS 实例
- ·JavaScript 实例
- ·Ajax 实例
- ·jQuery 实例
- ·XML 实例
- ·Java 实例
</dl>
<dl>
<dt>
字符集&工具
</dt>
<dd>
· <a target="_blank" href="/charsets/html-charsets.html">HTML 字符集设置</a>
</dd>
<dd>
· <a target="_blank" href="/tags/html-ascii.html">HTML ASCII 字符集</a>
</dd>
<dd>
· <a target="_blank" href="/tags/ref-entities.html">HTML ISO-8859-1</a>
</dd>
<dd>
· <a target="_blank" href="/tags/html-symbols.html">HTML 实体符号</a>
</dd>
<dd>
· <a target="_blank" href="/tags/html-colorpicker.html">HTML 拾色器</a>
</dd>
<dd>
· <a target="_blank" href="//c.runoob.com/front-end/53">JSON 格式化工具</a>
</dd>
</dl>
<dl>
<dt>
最新更新
</dt>
<dd>
·
<a href="http://www.runoob.com/w3cnote/scrapy-detail.html" title="Scrapy 入门教程">Scrapy 入门教程</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/w3cnote/c-structures-intro.html" title="C 结构体">C 结构体</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/w3cnote/matplotlib-tutorial.html" title="Matplotlib 教程">Matplotlib 教程</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/numpy/numpy-matplotlib.html" title="NumPy Matplotlib">NumPy Matplotlib</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/numpy/numpy-io.html" title="NumPy IO">NumPy IO</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/numpy/numpy-linear-algebra.html" title="NumPy 线性代数">NumPy 线性代数</a>
</dd>
<dd>
·
<a href="http://www.runoob.com/numpy/numpy-matrix.html" title="NumPy 矩阵库(Matrix)">NumPy 矩阵库(Ma...</a>
</dd>
</dl>
<dl>
<dt>
站点信息
</dt>
<dd>
·
<a target="_blank" href="//mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=ssbDyoOAgfLU3crf09venNHd3w" rel="external nofollow">意见反馈</a>
<!--<a class="wxpopup" onclick="popFunction()">合作联系
<span class="popuptext" id="myPopup">微信:<strong>centos5</strong></span>
</a>-->
</dd>
<dd>
·
<a target="_blank" href="/disclaimer">免责声明</a>
</dd>
<!--
<dd style="display: block;">
·
<a href="javascript:void(0)" onclick="dashangToggle()" style="font-weight:bold;color:#f00;" title="打赏,支持一下">打赏一下</a>
</dd>
-->
<dd>
·
<a target="_blank" href="/aboutus">关于我们</a>
</dd>
<dd>
·
<a target="_blank" href="/archives">文章归档</a>
</dd>
<!--
<dd>
·
<a href="https://www.xiaoyouxi100.com/?from=runoob" onclick="_hmt.push(['_trackEvent', '小游戏', 'click', 'xiaoyouxi100']);" target="_blank" title="小游戏">小游戏</a>
</dd>
-->
</dl>
<div class="search-share">
<div class="app-download">
<div>
<strong>关注微信</strong>
</div>
</div>
<div class="share">
<img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png">
</div>
</div>
</div>
<div class="shang_tit">
<p>感谢您的支持,我会继续努力的!</p>
</div>
<div class="shang_payimg">
<img src="//static.runoob.com/images/dashang/weipayimg.png" alt="扫码支持" title="扫一扫">
</div>
<div class="pay_explain">扫码打赏,你说多少就多少</div>
<div class="shang_payselect">
<div class="pay_item checked" data-id="weipay">
<span class="radiobox"></span>
<span class="pay_logo"><img src="//static.runoob.com/images/dashang/wechat.jpg" alt="微信"></span>
</div>
<div class="pay_item" data-id="alipay">
<span class="radiobox"></span>
<span class="pay_logo"><img src="//static.runoob.com/images/dashang/alipay.jpg" alt="支付宝"></span>
</div>
</div>
<div class="shang_info">
<p>打开<span id="shang_pay_txt">支付宝</span>扫一扫,即可进行扫码打赏哦</p>
<p><a href="//c.runoob.com/codedemo/5348" target="_blank"><span style=" font-size: 14px;color: #000;font-weight: bold;">点我查看本站打赏源码!</span></a></p>
</div>
</div>
<div id="cd-login"> <!-- 登录表单 -->
<div class="cd-form">
<p class="fieldset">
<label class="image-replace cd-username" for="signin-username">用户名</label>
<input class="full-width has-padding has-border" id="signin-username" name="username" type="text" placeholder="输入用户名">
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signin-password">密码</label>
<input class="full-width has-padding has-border" id="signin-password" name="password" type="password" placeholder="输入密码">
</p>
<p class="fieldset">
<input type="checkbox" id="remember-me" checked="">
<label for="remember-me">记住登录状态</label>
<a href="//www.runoob.com/reset-password" style="float: right;padding-right: 20px;" target="_blank">忘记密码?</a>
</p>
<input type="hidden" name="action" value="signin">
<p class="fieldset">
<input class="full-width2" type="submit" value="登 录">
</p>
<div class="err-msg"></div>
</div>
</div>
<div id="cd-signup"> <!-- 注册表单 -->
<div class="cd-form">
<p class="fieldset">
<label class="image-replace cd-password" for="verifycode">邀请码</label>
<input class="full-width has-padding has-border" id="signup-verifycode" name="verifycode" type="text" placeholder="输入邀请码">
</p>
<p class="fieldset">
<label class="image-replace cd-username" for="signup-username">用户名</label>
<input class="full-width has-padding has-border" id="signup-username" name="name" type="text" placeholder="输入用户名">
</p>
<p class="fieldset">
<label class="image-replace cd-email" for="signup-email">邮箱</label>
<input class="full-width has-padding has-border" name="email" id="signup-email" type="email" placeholder="输入mail">
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signup-password">密码</label>
<input class="full-width has-padding has-border" id="signup-password" name="password" type="password" placeholder="输入密码">
</p>
<p class="fieldset">
<label class="image-replace cd-password" for="signup-password2">重复输入密码</label>
<input class="full-width has-padding has-border" id="signup-password2" name="password2" type="password" placeholder="重复输入密码">
</p>
<!--
<p class="fieldset">
<input type="checkbox" id="accept-terms">
<label for="accept-terms">我已阅读并同意 <a href="javascript:;">用户协议</a></label>
</p>
-->
<input type="hidden" name="action" value="signup">
<p class="fieldset">
<input class="full-width2" type="submit" value="注册新用户">
</p>
<p class="fieldset">
<a href="//www.runoob.com/w3cnote/runoob-user-test-intro.html#invite" target="_blank">如何获取邀请码?</a>
</p>
<div class="err-msg"></div>
</div>
</div>
<a href="javascript:;" class="cd-close-form">关闭</a>
</div>
点我分享笔记