以下是 Java 判断字符串是否为空的三种方法.
方法一: 最多人使用的一个方法, 直观, 方便, 但效率很低.
方法二: 比较字符串长度, 效率高, 是我知道的最好一个方法.
方法三: Java SE 6.0 才开始提供的方法, 效率和方法二几乎相等, 但出于兼容性考虑, 推荐使用方法二.
1:if(s == null || s.equals(""));
2:if(s == null || s.length() <= 0);
3:if(s == null || s.isEmpty());
评论
19 楼
akfucc
2010-01-24
<div class="quote_title">xiaoyiz 写道</div>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p> </p>
<p>无语,</p>
<p>String s= null;</p>
<p>if(s == null || s.length()==0){//...}</p>
<p>你别告诉我这会报错。。</p>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p> </p>
<p>无语,</p>
<p>String s= null;</p>
<p>if(s == null || s.length()==0){//...}</p>
<p>你别告诉我这会报错。。</p>
18 楼
wang_fei4321
2010-01-23
为什么第一种方法性能低呢?
17 楼
belover
2010-01-23
当然考虑性能没有什么不好! 不过在这个层面考虑性能?效率上会有很大差异吗?
16 楼
nowopen
2010-01-23
<div class="quote_title">xiaoyiz 写道</div>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p> </p>
<p>
</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p>被你打败了!</p>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p> </p>
<p>
</p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p>被你打败了!</p>
15 楼
vvggsky
2010-01-23
<div class="quote_title">xiaoyiz 写道</div>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p>误人子弟</p>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p>误人子弟</p>
14 楼
YiSingQ
2010-01-23
<div class="quote_title">xiaoyiz 写道</div>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p>赞同反过来的写法,但是在遗留的就系统中一般采用第一种,现在是一般都在用isEmpty()咯。。。</p>
<div class="quote_div">
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
</div>
<p>赞同反过来的写法,但是在遗留的就系统中一般采用第一种,现在是一般都在用isEmpty()咯。。。</p>
13 楼
yourfei
2010-01-23
如果你使用了Spring,它里面也有StringUtils.hasText()
12 楼
xiaoyiz
2010-01-23
<div class="quote_title">ronalke 写道</div>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; font-size: x-small;"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
<div class="quote_div">
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span>以下是</span><span lang="EN-US"><span style="font-family: Times New Roman;"> Java </span></span><span>判断字符串是否为空的三种方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法一</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>最多人使用的一个方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>直观</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>方便</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但效率很低</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法二</span><span lang="EN-US"><span style="font-family: Times New Roman;">: </span></span><span>比较字符串长度</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率高</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>是我知道的最好一个方法</span></span><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">.<br></span></span><span>方法三</span><span lang="EN-US"><span style="font-family: Times New Roman;">: Java SE 6.0 </span></span><span>才开始提供的方法</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>效率和方法二几乎相等</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>但出于兼容性考虑</span><span lang="EN-US"><span style="font-family: Times New Roman;">, </span></span><span>推荐使用方法二</span><span style="font-family: Times New Roman;"><span lang="EN-US">.</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.equals(""));</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.length() <= 0);</span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(s == null || s.isEmpty());</span></span></span></p>
</div>
<p> </p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">1</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || "".equals(s));</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">2</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null==s || 0>=s.length() );</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: x-small;"><span style="background-color: #ffffff;"><span style="color: #0000ff;"><span lang="EN-US"><span style="font-family: Times New Roman;">3</span></span><span>:</span><span lang="EN-US"><span style="font-family: Times New Roman;">if(null ==s || s.isEmpty());</span></span></span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; font-size: x-small;"></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Times New Roman; color: #ff0000; font-size: x-small;"><span lang="EN-US">要反着写更安全,如果s本身为null 那么s.length直接抛出空指针一场了。。。 囧~</span></span></p>
11 楼
zhangzhimin2008
2010-01-23
一直用stringutils
10 楼
li445970924
2010-01-23
方法2比较好 规范些
9 楼
ivan18248
2010-01-23
一直用第一个看了楼主的用用二和三
8 楼
sorehead
2010-01-23
7 楼
毕竟红尘
2010-01-23
是common-lang里面的工具方法...
6 楼
liusu
2010-01-23
给点测试数据?
5 楼
tsxm
2010-01-23
我好像一直用方法一啊
4 楼
leon_a
2010-01-22
魔力猫咪 写道
anky_end 写道
直接用stringutils的静态方法。。。
这是哪个静态方法,我查了半天JDK没发现。
apache.commons.lang
3 楼
魔力猫咪
2010-01-22
anky_end 写道
直接用stringutils的静态方法。。。
这是哪个静态方法,我查了半天JDK没发现。
2 楼
xzqttt
2010-01-22
一直在用方法3
没有必要细究这个吧
没有必要细究这个吧
1 楼
anky_end
2010-01-22
直接用stringutils的静态方法。。。
相关推荐
判断字符串是否为空判断字符串是否为空判断字符串是否为空
### Java 判断字符串是否为空的三种方法 在Java编程中,经常需要判断字符串是否为空(null 或者空字符串)。为了确保程序的健壮性和避免NullPointerException等异常,掌握正确的字符串判断方法至关重要。本文将详细...
### JAVA判断字符串是否为数字字符 在日常的Java开发工作中,我们经常需要验证用户输入的数据是否符合预期格式,比如验证电话号码、身份证号等是否仅包含数字。这种情况下,一个简单有效的工具方法就显得尤为重要了...
可以判断字符串数组,字符串类型,List对象集合,Map集合map,object>,map和多个String参数,对象类型,是否为空。 java判断对象属性是否全部为空,只要有一个属性不为空,那么就不是所有的属性值都为空。
Java判断字符串是否为IP地址的方法 Java判断字符串是否为IP地址的方法是指通过编写特定的方法来判断输入的字符串是否符合IP地址的格式。这种方法在实际开发中非常有用,特别是在网络编程和网络安全等领域。 在Java...
在上面的代码中,我们首先使用 StringUtils.isBlank() 方法来判断字符串是否为空或长度为 0 或由空白符构成。如果字符串为空或长度为 0 或由空白符构成,返回 false。否则,我们使用 matches() 方法来判断字符串是否...
以下是一个简单的Java方法,用于判断字符串`str`是否可以表示一个整数: ```java // 判断整数(int) private boolean isInteger(String str) { if (null == str || "".equals(str)) { return false; } // 正则...
其中,`IsMatch` 方法用于判断指定的字符串是否与正则表达式匹配。 #### 2. IsMatch 方法详解 在提供的代码示例中,使用了 `IsMatch` 方法来进行字符串格式验证。具体代码如下: ```csharp public int IsDate...
### 知识点详解 #### 一、字符串与数字判断 ...总之,判断字符串是否只包含数字是一项基本而重要的任务,在多种编程场景中都有广泛应用。理解并掌握本文所述的方法和技术,将有助于开发者更高效地完成相关工作。
在Java编程语言中,处理字符串是常见的任务,其中包括判断字符串是否为空以及判断字符串是否只包含数字。在这篇文章中,我们将深入探讨这两种情况的检查方法。 首先,我们来讨论如何判断一个字符串是否为空。在Java...
本文主要讨论了如何正确判断Java中的String对象是否为null、空值("")以及它们的地址是否相等。在处理字符串时,了解这些概念对于避免程序出错至关重要。 首先,我们需要区分`null`和空字符串`""`。`null`表示变量...
在Java中使用正则表达式来判断字符串是否符合整数、小数或实数的格式是一种常见且有效的做法。在编程中,我们经常需要对输入的字符串进行格式验证,以确保它们符合预期的数值格式,尤其是在处理财务数据、用户输入...
在Java编程语言中,判断字符串`String`是否为空是一个常见的需求。这通常涉及到对字符串对象的检查,确保它们既不是`null`也不是空字符串。在本文中,我们将深入探讨几种不同的方法来完成这个任务,并分析它们的效率...
本文将深入解析Java中如何判断字符串是否为数字的方法,以及存在的问题和解决方案。 首先,我们可以从简单的基础开始,即判断字符串是否为空或null。在Java中,一个空字符串("")和null值显然不是数字。因此,我们...
在Java编程语言中,有时我们...总结来说,Java中判断字符串是否为正整数主要依赖于正则表达式和字符串匹配。这个实例提供了一种简单而直观的实现方式,但在实际项目中,你可能需要根据具体需求进行适当的优化和扩展。
3. **判断字符串是否为空**: `isEmpty()` 和 `isNotBlank()` 是两个重要的判断方法。`isEmpty()` 通常用于检查字符串是否为 null 或长度为 0,而 `isNotBlank()` 不仅检查空和长度,还会考虑字符串中是否有空白...
在Java编程中,判断字符串非空是一个常见的任务,特别是在处理用户输入或数据验证时。从提供的代码示例中,我们可以看到几种不同的方法来检查字符串是否为空。这些方法包括使用基本的逻辑运算符、Apache Commons ...
4. **性能优化**:使用 `String#matches` 方法结合正则表达式可以更高效地判断字符串是否符合数字格式。 ### 三、改进后的实现方案 #### 3.1 公共方法抽象 首先,我们可以定义一个公共方法 `isEmptyOrWhitespace` ...
java字符转换类代码,可以实现判断字符串是否为空,并删除首尾空格,字符串替换函数,代码转换,GBK转换为ISO-8859-1,代码转换 从srcCode转换为destCode,代码转换,GBK转换为big5,替换非法字符,标记本身等于分隔...
* 判断字符串是否包含重复字符 * @param str 输入的字符串 * @return 如果字符串中有重复字符,返回true;否则,返回false */ public static boolean containRepeatChar(String str) { // 首先检查字符串是否为...