javastring构造 java中string的构造方法
转载
一、构造方法
//1.初始化新创建的String对象,以使其表示空字符序列
new String();
//2.通过使用平台的默认字符集解码指定的字节数组来构造新的String
//bytes - 要解码为字符的字节
byte[] bytes=new byte[1024];
new String(bytes);
//3.构造一个新的String由指定用指定的字节的数组解码charset
//bytes - 要解码为字符的字节
//charset -该charset要使用的解码bytes
Charset charset = Charset.defaultCharset();
new String(bytes, charset);
//4.通过使用平台的默认字符集解码指定的字节子阵列来构造新的String
//bytes - 要解码为字符的字节
//offset - 要解码的第一个字节的索引
//length - 要解码的字节数
int len=6;
new String(bytes,0,len);
//5.构造一个新的String通过使用指定的指定字节子阵列解码charset
//bytes - 要解码为字符的字节
//offset - 要解码的第一个字节的索引
//length - 要解码的字节数
//charset -该charset要使用的解码bytes
new String(bytes,0,len,charset);
//6.构造一个新的String通过使用指定的字符集解码指定的字节子阵列
//bytes - 要解码为字符的字节
//offset - 要解码的第一个字节的索引
//length - 要解码的字节数
//charsetName - 支持的名称charset
new String(bytes,0,len,"UTF-8");
//7.构造一个新的String由指定用指定的字节的数组解码charset
//bytes - 要解码为字符的字节
//charsetName - 支持的名称charset
new String(bytes,"UTF-8");
//8.分配一个新的String ,以便它表示当前包含在字符数组参数中的字符序列。字符数组的内容被复制; 字符数组的后续修改不会影响新创建的字符串。
//value - 字符串的初始值
char[] value=new char[1024];
new String(value);
//9.分配一个新的String ,其中包含Unicode code point数组参数的子阵列中的字符
//offset参数是子阵列的第一个代码点的索引,count参数指定子阵列的长度,子阵列的内容转换为chars; int数组的int不影响新创建的字符串
//value - 作为字符源的数组
//offset - 初始偏移量
//count - 长度
new String(value,0,len);
//10.分配一个新的String ,其中包含Unicode code point数组参数的子阵列中的字符。offset参数是子阵列的第一个代码点的索引,count参数指定子阵列的长度。子阵列的内容转换为char s;int数组的int不影响新创建的字符串。
//codePoints - 作为Unicode代码点的源的数组
//offset - 初始偏移量
//count - 长度
int[] codePoints={1,2};
new String(codePoints,0,len);
//11.初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。 除非需要original的显式副本, original使用此构造函数是不必要的,因为Strings是不可变的
//original - String
new String("a");
//12.分配一个新的字符串,其中包含当前包含在字符串缓冲区参数中的字符序列。 字符串缓冲区的内容被复制; 字符串缓冲区的后续修改不会影响新创建的字符串。
//buffer - StringBuffer
new String(new StringBuffer("a"));
//13.分配一个新的字符串,其中包含当前包含在字符串构建器参数中的字符序列。 字符串构建器的内容被复制; 字符串构建器的后续修改不会影响新创建的字符串,提供此构造函数以便迁移到StringBuilder,通过toString方法从字符串构建器获取字符串可能运行速度更快,通常是首选。
//builder - StringBuilder
new String(new StringBuilder("a"));
二、String常用方法
String str = "hello word";
//返回指定索引的值
System.out.println(str.charAt(0));// h
//返回指定索引出的字符(Unicode代码点)
System.out.println(str.codePointAt(0));// 104
//返回指定索引之前的字符(Unicode代码点)
System.out.println(str.codePointBefore(1));// 104
//返回此String制定文本范围内的UniCode代码点数
System.out.println(str.codePointCount(0,1));// 1
//按字典顺序比较两个字符串
System.out.println(str.compareTo("hello word"));//0
// 按字典顺序比较两个字符串,忽略病例差异
System.out.println(str.compareToIgnoreCase("hello wor"));//1
//将指定的字符串连接到该字符串的末尾
System.out.println(str.concat("!!!"));// hello word!!!
//当此字符串包含指定的值时才返回true 不包含返回false
System.out.println(str.contains("a"));// false
//字符串是否以指定的后缀结尾
System.out.println(str.endsWith("d"));// true
//将这个字符床与指定字符串进行比较(比较内容)
System.out.println(str.equals("hello word")); //true
//如果位数不够16位,自动以0补上
System.out.println(String.format("%016d",6));//0000000000000006
//获取此字符串的字节数组
System.out.println(str.getBytes());// [B@1055e4af
//返回指定编码的字节数组
System.out.println(str.getBytes("UTF-8"));//[B@3caeaf62
//返回此字符串的哈希码
System.out.println(str.hashCode()); //-1604693608
//返回指定字符第一次出现的索引 如果不存在返回 -1
System.out.println(str.indexOf("e"));// 1
//返回指定字符第一次出现的索引,从制定的索引开始 如果不存在返回 -1
System.out.println(str.indexOf("l",2));// 2
//返回字符串对象的规范表示
System.out.println(str.intern());// hello word
//如果长度为 0 返回true
System.out.println(str.isEmpty());// false
//返回指定字符的最后一次出现的字符串中的索引 如果没有返回 -1
System.out.println(str.lastIndexOf("a"));
//返回指定字符串的最后一次出现的字符串中的索引,从制定索引开始向后搜索
System.out.println(str.lastIndexOf("w",str.length())-1);// 5
//返回字符串长度
System.out.println(str.length());// 10
//返回替换后的字符串 h 要替换的参数 b 要替换的值
System.out.println(str.replace("h","b"));// bello word
//返回替换后的字符串(替换字符串中的所有‘l’) l 要替换的参数 o 要替换的值
System.out.println(str.replaceAll("l","o"));// heooo word
//返回替换后的字符串(替换字符串中的第一个匹配的字符串) o 要替换的参数 a 要替换的值
System.out.println(str.replaceFirst("o","a"));// hella word
//将这个字符串以给定的字符分割(以空格分隔)
String[] split = str.split(" ");
for (String s :
split) {
System.out.println(s);
/**
* 输出:
* hello
* word
* */
}
//将这个字符串以给定的字符分割(以空格分隔)
String[] ss = str.split(" ", 2);
for (String s :
ss) {
System.out.println(s);
/**
* 输出:
* hello
* word
* */
}
//测试此字符串是否以指定的前缀开头
System.out.println(str.startsWith("h")); //true
//测试在指定索引处开始的此字符串的子字符串是否以指定的前缀开头。
System.out.println(str.startsWith("w",6));// true
//返回一个字符序列,该序列是该序列的子序列。(包含开始的索引,但不包括结束的索引)
System.out.println(str.subSequence(0,str.length()-1));// hello wor
//返回一个字符串(从指定索引开始截取,包含开始索引)
System.out.println(str.substring(2));// llo word
//返回一个字符串(从指定索引开始,指定索引结束,包括开始索引,不包括结束索引)
System.out.println(str.substring(2,4));// ll
//将字符串转换为信息字符数组
char[] x = str.toCharArray();
System.out.println(x);// hello word
//将字符串全部转换为小写
System.out.println(str.toLowerCase());// hello word
//将字符串全部转换为小写(Locale.ENGLISH 英文形式)
System.out.println(str.toLowerCase(Locale.ENGLISH));// hello word
//将字符串全部转换为大写
System.out.println(str.toUpperCase());// HELLO WORD
//将字符串全部转换为大写(Locale.ENGLISH英文形式)
System.out.println(str.toUpperCase(Locale.ENGLISH));// HELLO WORD
//去除字符串中前\后的空格
String l = " aaa ";
System.out.println(l.trim());//aaa
//返回 int参数的字符串 int形式。 其他数据类型相同(boolean,char,float,object...)
String s = String.valueOf(1);
System.out.println(s);
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。