String类的常用方法介绍
2018-04-10 18:46
489 查看
/**
* @author hws
* @date 2018-04-10
* ?String 类的常用方法介绍
*/
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;
public class StringTest {
public static void main(String[] args) {
// 如何将一个byte数组转换成字符串
byte[] bs = { 97, 122, 45, 32, 0, 100, 98 };
String s = new String(bs);
System.out.println(s);
// 怎么将String字符串转换成byte数组
String bb = "张三";
try {
// 编码转换问题 GBK->UTF-8
byte[] bbs = bb.getBytes("UTF-8");
String r = new String(bbs, "UTF-8");
System.out.println(r);
// 将UTF-8 ->GBK
byte[] bbs2 = r.getBytes("GBK");
String rr = new String(bbs2, "GBK");
System.out.println(rr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("==================");
// 返回指定下标的字符
char c = bb.charAt(1);
System.out.println(c);
System.out.println(bb.codePointAt(1));
String ls="李四";
//比较两个字符串(按字典顺序)
int i=ls.compareTo(bb);
System.out.println(i);
//System.out.println(ls.compareTo(bb));
//将李四连接到张三后面
String n = bb.concat(ls);
System.out.println(n);
//System.out.println(bb.concat(ls));
//判断是否拥有此字符串
boolean contains = bb.contains("张");
System.out.println(contains);
System.out.println(bb.contains("12"));
//将char数组转换为String字符串
char cc[]= {'我','和','你','都','我','是'};
String ncc = String.copyValueOf(cc);
System.out.println(ncc);
//判断字符最后一位(测试此字符串是否以指定的后缀结束。 )
boolean endsWith = ncc.endsWith("理");
System.out.println(endsWith);
System.out.println("============");
//忽略大小写的内容比较。将此 String 与另一个 String 比较,不考虑大小写。
System.out.println(bb.equalsIgnoreCase(ncc));
//使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
String format = String.format(new Locale("zh-ch"), bb);
System.out.println(format);
//返回指定字符串的下标。public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引。
int indexOf = bb.indexOf("张");
System.out.println(indexOf);
//从下标3包含3的位置往后找第一个出现的下标。public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
//返回:在此对象表示的字符序列中第一次出现的大于或等于 fromIndex 的字符的索引;如果未出现该字符,则返回 -1。
System.out.println(ncc.indexOf("2", 3));
//倒着找第一个出现的下标。lastIndexOf(int ch)返回指定字符在此字符串中最后一次出现处的索引。
int lastIndexOf = ncc.lastIndexOf("我");
System.out.println(lastIndexOf);
//判断字符串是否是空房子public boolean isEmpty()当且仅当 length() 为 0 时返回 true。如果 length() 为 0,则返回 true;否则返回 false。
String nn=" ";
System.out.println(nn.isEmpty());
//join 特殊拼接
String join = String.join("#", "张三","李四");
System.out.println(join);
//返回字符串长度
System.out.println(join.length());
//正则表达式
//电话
String tel="^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\\\d{8}$";
//邮箱
String email = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
//身份证号
String idCard = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)";
boolean b = Pattern.compile(tel).matcher("13838389438").matches();
boolean b1 = Pattern.compile(email).matcher("373828882@qq.com").matches();
String sfz = "41270119900916353X";
boolean b2 = sfz.matches(idCard);
System.out.println(b2);
//字符串的替换
String replace = join.replace("#", "@");
System.out.println(replace);
System.out.println("=============");
//正则表达式进行正则表达式替换\W表示非单词字符
String replaceAll = join.replaceAll("\\W", "8");
System.out.println(replaceAll);
//只替换第一个
String replaceFirst = join.replaceFirst("#", "8");
System.out.println(replaceFirst);
//按照指定正则表达式拆分字符
String[] split = join.split("#");
System.out.println(Arrays.toString(split));
String eg="张三#20#男@李四#男#21@王五#女#22";
String[] split2 = eg.split("@");
System.out.println(Arrays.toString(split2));
//循环遍历数组,并且转换成字符串形式表示foreach语句
for(String egs:split2) {
String[] split3 = egs.split("#");
System.out.println(Arrays.toString(split3));
}
//判断字符串以什么开头。测试字符串是否以指定的字符开头
boolean startsWith = join.startsWith("张");
System.out.println(startsWith);
//截取字符串substring返回一个新的字符串,它是此字符串的一个子字符串。
//该子字符串从指定索引处的字符开始,直到此字符串末尾。
//1包含,4不包含,即开头包含,结尾不包含
String substring = join.substring(1, 4);
System.out.println(substring);
//表示从下标开始截取
System.out.println(join.substring(1));
//截取李四
String substring2 = join.substring(join.indexOf("李四"), join.indexOf("李四")+2);
System.out.println(substring2);
//截取三到四
String substring3
9fb8
= join.substring(join.indexOf("三"), join.indexOf("四")+1);
System.out.println(substring3);
System.out.println("================");
//字符串转化为char数组
char[] charArray = join.toCharArray();
System.out.println(Arrays.toString(charArray));
//转大写
String xx="abc";
String dxx = xx.toUpperCase();
System.out.println(dxx);
//转小写
xx = dxx.toLowerCase();
System.out.println(xx);
System.out.println("===============");
//去空格
String be=" 东方不败 ";
String trim = be.trim();
System.out.println(trim);
//String->boolean
String be1="true";
Boolean valueOf = Boolean.valueOf(be1);
System.out.println(valueOf);
be1 = String.valueOf(valueOf);
System.out.println(be1);
}
}
* @author hws
* @date 2018-04-10
* ?String 类的常用方法介绍
*/
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Pattern;
public class StringTest {
public static void main(String[] args) {
// 如何将一个byte数组转换成字符串
byte[] bs = { 97, 122, 45, 32, 0, 100, 98 };
String s = new String(bs);
System.out.println(s);
// 怎么将String字符串转换成byte数组
String bb = "张三";
try {
// 编码转换问题 GBK->UTF-8
byte[] bbs = bb.getBytes("UTF-8");
String r = new String(bbs, "UTF-8");
System.out.println(r);
// 将UTF-8 ->GBK
byte[] bbs2 = r.getBytes("GBK");
String rr = new String(bbs2, "GBK");
System.out.println(rr);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
System.out.println("==================");
// 返回指定下标的字符
char c = bb.charAt(1);
System.out.println(c);
System.out.println(bb.codePointAt(1));
String ls="李四";
//比较两个字符串(按字典顺序)
int i=ls.compareTo(bb);
System.out.println(i);
//System.out.println(ls.compareTo(bb));
//将李四连接到张三后面
String n = bb.concat(ls);
System.out.println(n);
//System.out.println(bb.concat(ls));
//判断是否拥有此字符串
boolean contains = bb.contains("张");
System.out.println(contains);
System.out.println(bb.contains("12"));
//将char数组转换为String字符串
char cc[]= {'我','和','你','都','我','是'};
String ncc = String.copyValueOf(cc);
System.out.println(ncc);
//判断字符最后一位(测试此字符串是否以指定的后缀结束。 )
boolean endsWith = ncc.endsWith("理");
System.out.println(endsWith);
System.out.println("============");
//忽略大小写的内容比较。将此 String 与另一个 String 比较,不考虑大小写。
System.out.println(bb.equalsIgnoreCase(ncc));
//使用指定的语言环境、格式字符串和参数返回一个格式化字符串。
String format = String.format(new Locale("zh-ch"), bb);
System.out.println(format);
//返回指定字符串的下标。public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引。
int indexOf = bb.indexOf("张");
System.out.println(indexOf);
//从下标3包含3的位置往后找第一个出现的下标。public int indexOf(int ch,int fromIndex)返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索
//返回:在此对象表示的字符序列中第一次出现的大于或等于 fromIndex 的字符的索引;如果未出现该字符,则返回 -1。
System.out.println(ncc.indexOf("2", 3));
//倒着找第一个出现的下标。lastIndexOf(int ch)返回指定字符在此字符串中最后一次出现处的索引。
int lastIndexOf = ncc.lastIndexOf("我");
System.out.println(lastIndexOf);
//判断字符串是否是空房子public boolean isEmpty()当且仅当 length() 为 0 时返回 true。如果 length() 为 0,则返回 true;否则返回 false。
String nn=" ";
System.out.println(nn.isEmpty());
//join 特殊拼接
String join = String.join("#", "张三","李四");
System.out.println(join);
//返回字符串长度
System.out.println(join.length());
//正则表达式
//电话
String tel="^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\\\\d{8}$";
//邮箱
String email = "^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";
//身份证号
String idCard = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)";
boolean b = Pattern.compile(tel).matcher("13838389438").matches();
boolean b1 = Pattern.compile(email).matcher("373828882@qq.com").matches();
String sfz = "41270119900916353X";
boolean b2 = sfz.matches(idCard);
System.out.println(b2);
//字符串的替换
String replace = join.replace("#", "@");
System.out.println(replace);
System.out.println("=============");
//正则表达式进行正则表达式替换\W表示非单词字符
String replaceAll = join.replaceAll("\\W", "8");
System.out.println(replaceAll);
//只替换第一个
String replaceFirst = join.replaceFirst("#", "8");
System.out.println(replaceFirst);
//按照指定正则表达式拆分字符
String[] split = join.split("#");
System.out.println(Arrays.toString(split));
String eg="张三#20#男@李四#男#21@王五#女#22";
String[] split2 = eg.split("@");
System.out.println(Arrays.toString(split2));
//循环遍历数组,并且转换成字符串形式表示foreach语句
for(String egs:split2) {
String[] split3 = egs.split("#");
System.out.println(Arrays.toString(split3));
}
//判断字符串以什么开头。测试字符串是否以指定的字符开头
boolean startsWith = join.startsWith("张");
System.out.println(startsWith);
//截取字符串substring返回一个新的字符串,它是此字符串的一个子字符串。
//该子字符串从指定索引处的字符开始,直到此字符串末尾。
//1包含,4不包含,即开头包含,结尾不包含
String substring = join.substring(1, 4);
System.out.println(substring);
//表示从下标开始截取
System.out.println(join.substring(1));
//截取李四
String substring2 = join.substring(join.indexOf("李四"), join.indexOf("李四")+2);
System.out.println(substring2);
//截取三到四
String substring3
9fb8
= join.substring(join.indexOf("三"), join.indexOf("四")+1);
System.out.println(substring3);
System.out.println("================");
//字符串转化为char数组
char[] charArray = join.toCharArray();
System.out.println(Arrays.toString(charArray));
//转大写
String xx="abc";
String dxx = xx.toUpperCase();
System.out.println(dxx);
//转小写
xx = dxx.toLowerCase();
System.out.println(xx);
System.out.println("===============");
//去空格
String be=" 东方不败 ";
String trim = be.trim();
System.out.println(trim);
//String->boolean
String be1="true";
Boolean valueOf = Boolean.valueOf(be1);
System.out.println(valueOf);
be1 = String.valueOf(valueOf);
System.out.println(be1);
}
}
相关文章推荐
- Java String类常用方法介绍(5)
- String类的一些常用的方法介绍
- Java中String类的常用方法介绍
- java string类的常用方法详细介绍
- 结合Scikit-learn介绍几种常用的特征选择方法
- iPhone之NSString常用方法介绍
- 结合Scikit-learn介绍几种常用的特征选择方法
- Java-String类的常用方法总结
- JAVA基础 day13 String类和StringBUffer类的常用方法 基本数据类型包装类的学习
- 多线程学习笔记六-------------线程的消亡以及一些常用方法的介绍
- 投资技巧:抛股票有技巧 常用方法介绍
- String类常用方法
- JavaSE001_String类总结之构造器和常用方法
- Android MediaPlayer 常用方法介绍 .
- String类常用的方法
- java中String类方法介绍及使用
- Socket①:常用方法介绍
- java中String类的几种常用方法
- C#--string类常用方法
- 19-集合框架工具类-17-常用对象API(集合框架-工具类-Arrays-方法介绍)