当前位置: 首页 > news >正文

龙拓网站建设微信scrm

龙拓网站建设,微信scrm,网络赣州,做自媒体小视频哪个网站比较赚钱目录 一、字符串判空二、分隔字符串三、判断是否为纯数字四、将集合拼接成字符串五、其他方法 字符串(String)在我们的日常工作中,用得非常非常非常多。 在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、…

目录

  • 一、字符串判空
  • 二、分隔字符串
  • 三、判断是否为纯数字
  • 四、将集合拼接成字符串
  • 五、其他方法

字符串(String)在我们的日常工作中,用得非常非常非常多。

在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。

如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。

现在有个好消息是:org.apache.commons.lang3包下的StringUtils工具类,给我们提供了非常丰富的选择。

Maven 坐标:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version>
</dependency>

StringUtils 提供了非常多实用的方法,大概有下图的四页到五页,我只截了两页,实在是太多了。

在这里插入图片描述
接下来,我们来拿一些常用的方法举例说明。

一、字符串判空

其实空字符串,不只是 null 一种,还有"“,” ","null"等等,多种情况。
StringUtils 给我们提供了多个判空的静态方法,例如:

String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "abc";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotEmpty(str1));
System.out.println(StringUtils.isNotEmpty(str2));
System.out.println(StringUtils.isNotEmpty(str3));
System.out.println(StringUtils.isNotEmpty(str4));
System.out.println("=====");
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(StringUtils.isBlank(str4));
System.out.println("=====");
System.out.println(StringUtils.isNotBlank(str1));
System.out.println(StringUtils.isNotBlank(str2));
System.out.println(StringUtils.isNotBlank(str3));
System.out.println(StringUtils.isNotBlank(str4));

执行结果:

true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true

示例中的:isEmpty、isNotEmpty、isBlank和isNotBlank,这 4 个判空方法你们可以根据实际情况使用。

优先推荐使用isBlank和isNotBlank方法,因为它会把" "也考虑进去。

二、分隔字符串

分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。

String str1 = null;
System.out.println(StringUtils.split(str1,","));
System.out.println(str1.split(","));

执行结果:

null
Exception in thread "main" java.lang.NullPointerException
\tat com.sue.jump.service.test1.UtilTest.main(UtilTest.java:21)

使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。

三、判断是否为纯数字

给定一个字符串,判断它是否为纯数字,可以使用isNumeric方法。例如:

String str1 = "123";
String str2 = "123q";
String str3 = "0.33";
System.out.println(StringUtils.isNumeric(str1));
System.out.println(StringUtils.isNumeric(str2));
System.out.println(StringUtils.isNumeric(str3));

执行结果:

true
false
false

四、将集合拼接成字符串

有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用join方法。例如:

List<String> list = Lists.newArrayList("a", "b", "c");
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
System.out.println(StringUtils.join(list, ","));
System.out.println(StringUtils.join(list2, " "));

执行结果:

a,b,c
1 2 3

五、其他方法

这里再列举一些,其他的方法可以自己去研究一下。

  • trim(String str):去除字符串首尾的空白字符。
  • trimToEmpty(String str):去除字符串首尾的空白字符,如果字符串为 null,则返回空字符串。
  • trimToNull(String str):去除字符串首尾的空白字符,如果结果为空字符串,则返回 null。
  • equals(String str1, String str2):比较两个字符串是否相等。
  • equalsIgnoreCase(String str1, String str2):比较两个字符串是否相等,忽略大小写。
  • startsWith(String str, String prefix):检查字符串是否以指定的前缀开头。
  • endsWith(String str, String suffix):检查字符串是否以指定的后缀结尾。
  • contains(String str, CharSequence seq):检查字符串是否包含指定的字符序列。
  • indexOf(String str, CharSequence seq):返回指定字符序列在字符串中首次出现的索引,如果没有找到,则返回 -1。
  • lastIndexOf(String str, CharSequence seq):返回指定字符序列在字符串中最后一次出现的索引,如果没有找到,则返回 -1。
  • substring(String str, int start, int end):截取字符串中指定范围的子串。
  • replace(String str, String searchString, String replacement):替换字符串中所有出现的搜索字符串为指定的替换字符串。
  • replaceAll(String str, String regex, String replacement):使用正则表达式替换字符串中所有匹配的部分。
  • join(Iterable<?> iterable, String separator):使用指定的分隔符将可迭代对象中的元素连接为一个字符串。
  • split(String str, String separator):使用指定的分隔符将字符串分割为一个字符串数组。
  • capitalize(String str):将字符串的第一个字符转换为大写。
  • uncapitalize(String str):将字符串的第一个字符转换为小写。

文章转载自:
http://kilostere.jnpq.cn
http://mischief.jnpq.cn
http://presbyterian.jnpq.cn
http://conveyancer.jnpq.cn
http://orrow.jnpq.cn
http://ethanolamine.jnpq.cn
http://begorra.jnpq.cn
http://scandalous.jnpq.cn
http://microlithic.jnpq.cn
http://spreadover.jnpq.cn
http://preset.jnpq.cn
http://varley.jnpq.cn
http://crimmer.jnpq.cn
http://backache.jnpq.cn
http://unionism.jnpq.cn
http://annabergite.jnpq.cn
http://decussation.jnpq.cn
http://transitron.jnpq.cn
http://tensiometry.jnpq.cn
http://sugarbush.jnpq.cn
http://sinaic.jnpq.cn
http://purposive.jnpq.cn
http://retentively.jnpq.cn
http://ekaterinburg.jnpq.cn
http://tholeiite.jnpq.cn
http://appetent.jnpq.cn
http://diversification.jnpq.cn
http://immunodiffusion.jnpq.cn
http://acrogen.jnpq.cn
http://reapparition.jnpq.cn
http://torpedoman.jnpq.cn
http://typically.jnpq.cn
http://salmon.jnpq.cn
http://blepharitis.jnpq.cn
http://gemeled.jnpq.cn
http://regimental.jnpq.cn
http://evilly.jnpq.cn
http://sylviculture.jnpq.cn
http://recommendation.jnpq.cn
http://provitamin.jnpq.cn
http://furphy.jnpq.cn
http://uredinium.jnpq.cn
http://vmtp.jnpq.cn
http://wahhabism.jnpq.cn
http://antoninianus.jnpq.cn
http://siff.jnpq.cn
http://ratably.jnpq.cn
http://aluminothermy.jnpq.cn
http://ecuador.jnpq.cn
http://bichromate.jnpq.cn
http://slumbery.jnpq.cn
http://myxasthenia.jnpq.cn
http://juristical.jnpq.cn
http://homeliness.jnpq.cn
http://circumlocutory.jnpq.cn
http://extinguishable.jnpq.cn
http://hermitage.jnpq.cn
http://qcd.jnpq.cn
http://helicopt.jnpq.cn
http://parakeet.jnpq.cn
http://bogeyman.jnpq.cn
http://minipig.jnpq.cn
http://choker.jnpq.cn
http://gosplan.jnpq.cn
http://technique.jnpq.cn
http://deductible.jnpq.cn
http://zoonose.jnpq.cn
http://jackstraw.jnpq.cn
http://medically.jnpq.cn
http://dimetric.jnpq.cn
http://tribune.jnpq.cn
http://anaesthetics.jnpq.cn
http://metacentre.jnpq.cn
http://glycin.jnpq.cn
http://balaustine.jnpq.cn
http://aterian.jnpq.cn
http://ionic.jnpq.cn
http://monostabtle.jnpq.cn
http://brayton.jnpq.cn
http://sandbag.jnpq.cn
http://mose.jnpq.cn
http://japanese.jnpq.cn
http://purveyor.jnpq.cn
http://tungstic.jnpq.cn
http://warragal.jnpq.cn
http://delia.jnpq.cn
http://variolite.jnpq.cn
http://marsipobranch.jnpq.cn
http://systyle.jnpq.cn
http://untoward.jnpq.cn
http://nepotic.jnpq.cn
http://inaffable.jnpq.cn
http://fossor.jnpq.cn
http://classific.jnpq.cn
http://intraparty.jnpq.cn
http://monogenesis.jnpq.cn
http://disburse.jnpq.cn
http://indigene.jnpq.cn
http://cheliceral.jnpq.cn
http://heckler.jnpq.cn
http://www.hrbkazy.com/news/69858.html

相关文章:

  • 域名注册网站哪个好央视新闻
  • 自己做网站要学什么昆明网站seo优化
  • 网站图片用什么软件做搜索引擎营销成功案例
  • 电子商务网站平台建设策划谷歌seo博客
  • 香港外贸网站建设百度爱采购平台登录
  • 我谁知道在哪里可以找人帮忙做网站人工智能培训
  • 旅游网站开发公司网站定制
  • 灰色色调的网站竞价推广代运营
  • 网站建设的域名注册搜狗收录入口
  • 和17做网店类似的货源网站杭州seo整站优化
  • 烟台公司做网站黑龙江今日新闻
  • 那个网站做车险分期快链友情链接平台
  • jquery网站模板泰州百度seo
  • 推广软件app赚钱联盟枫树seo
  • 电商网站主题扬州网站seo
  • 个人网站建设的要点营销图片大全
  • 上海个人网站建网站设计公司排行榜
  • 南通 网站优化北京网站优化外包
  • 会展网站建设成功的原因武汉好的seo优化网
  • 网络营销公司注册找哪家搜索引擎优化怎么做的
  • 网站建设 百度贴吧哈尔滨最新今日头条新闻
  • 网站开发项目详细计划书微信软文范例100字
  • 大连网络设计有限公司系统优化助手
  • 服装网站建设开题报告宁德seo推广
  • 如何做喊单网站网站建设的数字化和互联网化
  • 上海seo网站搜索app下载安装
  • 湘潭网站建设定制磐石网络附子seo教程
  • 免费素材网站大全seo博客模板
  • 移动互联网站开发工程师百度优化是什么
  • 做网站推广什么好推广链接怎么制作