String一些方法的使用

String.format()

String.format()就是将前面的表达式用后面的字符串替代常用方法如下

//单个占位符

String s1 = String.format("hellow %s ,欢迎您!", "Tom");

System.out.print(s1);//hellow Tom ,欢迎您!

// 多个占位符

// 1.按照顺序匹配

String s2 = String.format("hellow %s ,%s是占位符", "Tom", "%s");

System.err.println(s2);//hellow Tom ,%s是占位符

// 2.按照索引匹配

String s3 = String.format("%1$s,%3$s,%2$s", "1个字符串", "2个字符串", "3个字符串");

扫描二维码关注公众号,回复: 11322594 查看本文章

System.out.println(s3);//1个字符串,3个字符串,2个字符串

但是他的作用远远不止这些,很强大,很复杂,有时间可以看看;

String.split()

String.split()主要用来分割字符串参数是你要以什么字符来分割最后得到字符串被分割后组成的字符串数组

String b ="100-1-0-5";

String arg1 [] =b.split("-");

特殊字符:.     |    *  

这些字符split是无法识别的需要在前面加”\\”:否则分割的字符串数组长度为0

String a ="100.1";

String arg2 [] =a. split("\\.")[0]

猜你喜欢

转载自www.cnblogs.com/luzhanshi/p/13167696.html