java.lang.StringBuilder.codePointCount()方法实例

算法 ww9992019com 发表于 1 周前

www.hj8828.com开户请拨15894452155中国女足青岛市南区vatQfx<div style="width:100%;float:left;" class="article-content">

                <input type="hidden" name="article_id" id="article_id" value="5099">
                <input type="hidden" name="catid" id="catid" value="0">

                <p>

<strong>java.lang.StringBuilder.codePointCount() </strong>方法返回在这个序列指定文本范围内的Unicode代码点的数量。</p>

<p>

该文本范围始于指定beginIndex延长到索引endIndex- 1处的char值。因此,文本范围的长度(以字符)是 endIndex- beginIndex</p>

<h2>

声明</h2>

<p>

以下是<strong>java.lang.StringBuilder.codePointCount()</strong>方法的声明</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">public</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> codePointCount</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> beginIndex</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> endIndex</span><span class="pun">)</span></pre>

<h2>

参数</h2>

<ul>

<li>

    <p>

        beginIndex -- 这是索引在文本范围的第一个字符。</p>

</li>

<li>

    <p>

        endIndex -- 这是文本范围的最后一个字符之后的索引。</p>

</li>

</ul>

<h2>

返回值</h2>

<p>

这个方法返回指定文本范围Unicode代码点的数量。</p>

<h2>

异常</h2>

<ul>

<li>

    <p>

        IndexOutOfBoundsException -- 如果beginIndex是负值,或endIndex比这个序列的长度大,或beginIndex大于endIndex。</p>

</li>

</ul>

<h2>

例子</h2>

<p>

下面的例子显示java.lang.StringBuilder.codePointCount()方法的使用。</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">package</span><span class="pln"> com</span><span class="pun">.</span><span class="pln">yiibai</span><span class="pun">;</span><span class="pln">

</span><span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.*;</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">StringBuilderDemo</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">static</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> main</span><span class="pun">(</span><span class="typ">String</span><span class="pun">[]</span><span class="pln"> args</span><span class="pun">)</span><span class="pln"> </span><span class="pun">{</span><span class="pln">

</span><span class="typ">StringBuilder</span><span class="pln"> str </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringBuilder</span><span class="pun">(</span><span class="str">"programming"</span><span class="pun">);</span><span class="pln">

</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"string = "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">);</span><span class="pln">



</span><span class="com">// returns the codepoint count from index 1 to 5</span><span class="pln">

</span><span class="kwd">int</span><span class="pln"> retval </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">.</span><span class="pln">codePointCount</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">5</span><span class="pun">);</span><span class="pln">

</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Count = "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> retval</span><span class="pun">);</span><span class="pln">



str </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">StringBuilder</span><span class="pun">(</span><span class="str">"amrood admin "</span><span class="pun">);</span><span class="pln">

</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"string = "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> str</span><span class="pun">);</span><span class="pln">

</span><span class="com">// returns the codepoint count from index 3 to 9</span><span class="pln">

retval </span><span class="pun">=</span><span class="pln"> str</span><span class="pun">.</span><span class="pln">codePointCount</span><span class="pun">(</span><span class="lit">3</span><span class="pun">,</span><span class="pln"> </span><span class="lit">9</span><span class="pun">);</span><span class="pln">

</span><span class="typ">System</span><span class="pun">.</span><span class="kwd">out</span><span class="pun">.</span><span class="pln">println</span><span class="pun">(</span><span class="str">"Count = "</span><span class="pln"> </span><span class="pun">+</span><span class="pln"> retval</span><span class="pun">);</span><span class="pln">

</span><span class="pun">}</span><span class="pln">

</span><span class="pun">}</span><span class="pln"> </span></pre>

<p>

让我们来编译和运行上面的程序,这将产生以下结果:</p>

<pre class="prettyprint prettyprinted">

<span class="kwd">string</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> programming

</span><span class="typ">Count</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">4</span><span class="pln">

</span><span class="kwd">string</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> amrood admin

</span><span class="typ">Count</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> </span><span class="lit">6</span></pre>

                <br>
                <p style="text-align:center;float:left;width:100%;margin-top:24px;margin-bottom:18px;">
                        <button class="layui-btn layui-btn-danger" id="btn-reward"> ¥ 我要打赏 </button>  
                        <button class="layui-btn" id="article-perfect">
                            <i class="layui-icon layui-icon-edit"></i>教程纠错
                        </button>
                        <button class="layui-btn layui-btn-normal" id="action-collection">
                            <i class="layui-icon layui-icon-rate"></i>收藏
                        </button>
                </p>



                <div style="width:100%;margin-bottom: 16px; height: 32px;margin-top:18px;float:left;">

                    <span style="float:left;">上一篇:<a href="http://f2e.im/u/ww9992019com" title="java.lang.StringBuilder.codePointBefore()方法实例"><i class="layui-icon"></i>
                            java.lang.StringBuilder.codePointBefore()方法实例</a></span>
                                            <span style="float:right;">下一篇:<a href="http://f2e.im/u/ww9992019com" style="float:right;" title="下一篇:java.lang.StringBuilder.delete()方法实例"><i class="layui-icon"></i>
                            java.lang.StringBuilder.delete()方法实例</a></span>
                                            <div id="googlead" style="float:left;margin-top:8px;">
                        <script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
                        <!-- yiibai下方 -->
                        <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1090193214637198" data-ad-slot="6494738921"></ins>
                        <script>
                            (adsbygoogle = window.adsbygoogle || []).push({});
                        </script>
                    </div>
                </div>

                <div style="width:100%;margin-top:18px;float:left;">


            </div>
暂无回复,说出你的观点吧
登录后即可参与回复