package com.bjpowernode.demo01;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
//String构造方法,
public class Test01 {
public static void main(String[] args) throws UnsupportedEncodingException {
//1) 直接赋值字符串字面量
String s1 = "hello";
//2) 无参构造
String s2 = new String(); //长度为0的字符串, 相当于"",空串
//new运算符在堆区中创建一个对象 , 把该对象的引用(地址)赋值给s2变量
System.out.println( s2.length() );
//3)根据字节数组创建String对象
byte[]bytes = {65,66,67,97,98,99,100};
//把bytes字节数组中的内容, 根据当前默认的编码格式(utf-8)转换为字符串
String s3 = new String(bytes);
System.out.println( s3 ); //ABCabcd
//根据字节数组中的部分字节创建String对象, 把byte字节数组中从2开始的4个字节转换为字符串
s3 = new String(bytes , 2 , 4);
System.out.println( s3 ); //Cabc
//把字节 数组中的字节转换为字符串时,可以指定编码格式
bytes = "hello,动力节点".getBytes(); //字符串的getBytes()可以把字符串转换为默认编码下的字节数组
System.out.println( Arrays.toString(bytes));
//[104, 101, 108, 108, 111, 44, -27, -118, -88, -27, -118, -101, -24, -118, -126, -25, -126, -71]
//utf-8编码中, 一个英文字符占1字节, 一个汉字占3个字节
s3 = new String(bytes, "UTF-8"); //使用utf-8编码把字节数组转换为字符串
System.out.println( s3 );
s3 = new String(bytes, "GBK"); //使用GBK编码把字节数组转换为字符串
System.out.println( s3 ); //hello,鍔ㄥ姏鑺傜偣
//在GBK编码中,一个英文字符占1字节,一个汉字占2个字节
//4) 根据字符数组创建字符串对象
char[]contents = {'A','*','汉','妃','菲','6'};
String s4 = new String(contents);
System.out.println( s4 ); //A*汉妃菲6
s4 = new String(contents, 2, 3); //从2开始的3个字符转换为字符串
System.out.println( s4 );
//5) 根据已有的String对象创建新的字符串
String s5 = new String(s4);
System.out.println( s5 );
System.out.println( s4 == s5 ); //false
}
}
1.1String
1.1.1 构造方法
new String() //创建一个长度为0的字符串
new String(bytes) ; //把字节数组bytes中的所有字节转换为String对象
new String( bytes , from , len) ; 把字节数组bytes中,从from开始的len个字节转换为字符串
new String( char[] ) ; //把字符数组中的字符转换为字符串
new String( str1 ) ; //根据str1创建一个新的字符串对象
有时, 把字节数组转换为字符串对象时,可以指定编码格式
new String( bytes , “utf-8” )
1.1.2 常用操作
char charAt(int index)返回指定index位置的字符.
int compareTo(String anotherString) 比较两个字符串的大小 , 如果当前字符串大返回正数, anotherString参数字符串大返回负数, 两个字符串相等返回0. 逐个字符比较,遇到第一个不相等的字符进行相减.实际上是字符的码值相减
“abc”.compareTo(“abdef”); 返回负数
boolean contains(CharSequence s) 判断当前字符串中是否包含s字符串,如: “hello”.contains(“he”); 返回true
boolean endsWith(String suffix) 判断当前字符串是否以指定的suffix子串结尾. 如: “d:/java/hello.java”.endsWith(“.java”) 返回true
boolean equals(Object anObject)判断两个字符串中的字符是否都一样.重写了Object类的equals()方法.
byte[] getBytes() 返回当前字符串在默认的编码格式下对应的字节数组.
byte[] getBytes(String charsetName) 返回当前字符串在指定的编码格式下对应的字节数组. 如: “hello,动力节点”.getBytes(“GBK”); 返回“hello,动力节点”字符串在GBK编码下对应的字节数组
void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 把当前字符串[srcBegin, srcEnd )范围内的字符复制到dst数组中destBegin开始的位置
int hashCode()
int indexOf(int ch) 返回字符ch在当前字符串中第一次出现的索引值,如: “hello”.indexOf(‘l’) 返回2
int indexOf(String str) 返回字符串str在当前字符串中第一次出现的索引值,如: “abcdefabcdefabcdef”.indexOf(“def”) 返回3
boolean isEmpty() 判断当前字符串是否为空串,长度为0的字符串
int lastIndexOf(int ch) 返回字符ch在当前字符串中最后一次出现的索引值,
int lastIndexOf(String str) 返回字符串str在当前字符串中最后一次出现的索引值,
int length() 返回字符的长度.
boolean startsWith(String prefix) 判断当前字符串是否以前缀prefix字符串开始.
String substring(int beginIndex) 返回从beginIndex到最后的子串,如: “hello,world”.subString(5)返回” ,world”
String substring(int beginIndex, int endIndex) 返回指定范围[beginIndex, endIndex)内的字符串,如: “hello,world”.subString(2,6)返回” llo,”.
char[] toCharArray() 把字符串转换为字符数组. char [] contents = “hello”.toCharArray(), contents字符数组中存储 ’h’,’e’,’l’,’l’,’o’
String toLowerCase() 把字符串中的大写字母转换为小写字母
String toString() 重写Object类的toString().
String toUpperCase() 把字符串中的小写字母转换为大写字母
String trim() 去掉字符串前后的空白字符, 包括空格,Tab等, “ he he “.trim(), 返回”he he”
static String valueOf(int i) 把基本类型的数据转换为字符串. 如String s1 = String.valueOf(456);
static String valueOf(Object obj) 把对象obj转换为字符串
1.1.3 String字符串对象是不可变的
String s1 = “hello”;
s1 = “world”; 创建了一个新的字符串对象,把新的字符串对象的引用赋值给s1, 并没有把hello变为world
举个小例子:
package com.bjpowernode.test;
/**
* ClassName:TestSubString
* PackageName:com.bjpowernode.test
* Description:
* java中所有的索引值遵循左闭右开原则
*
* @date:2020/6/21 12:59
* @author:robin
*/
public class TestSubString {
public static void main(String[] args) {
//需求:把文件绝对路径中的文件夹、文件名、扩展名分离出来
//文件夹的获取
String fileName="D:\\apiPost/ApiPost.exe";
int fileFolderIndex=fileName.lastIndexOf("/");
String fileFolder = fileName.substring(0, fileFolderIndex);
System.out.println(fileFolder);
//文件名的获取
int fileIndex = fileName.lastIndexOf(".");
String fileNam = fileName.substring(fileFolderIndex + 1, fileIndex);
System.out.println(fileNam);
//获取扩展名
String extendName = fileName.substring(fileIndex + 1);
System.out.println(extendName);
}
}