编码:String—》byte[] 调用String的getBytes 字符串—》字节(看得懂----》看不懂的二进制数据)
解码:byte[]---->String:调用String的构造器 编码的逆过程,字节-----》字符串(看不懂的二进制数据----》看得懂)
说明:解码时,要求解码使用的字符集必须与编码时使用的字符集一致,否则会出现乱码
package shangGuiGu.day23;
import org.junit.Test;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.Arrays;
public class demo01 {
@Test
public void test() throws UnsupportedEncodingException {
String str1="abc12啊";
byte[] bytes=str1.getBytes();
System.out.println(Arrays.toString(bytes));//[97, 98, 99, 49, 50, 51]
System.out.println(bytes);//[B@5d6f64b1
byte[] gbks=str1.getBytes("gbk");//使用gbk字符集进行编码
String str2=new String(bytes);
System.out.println(str2);
String str3=new String(gbks);//乱码
String str4=new String(gbks,"gbk");
System.out.println(str3);
System.out.println(str4);
}
}
StringBuffer和StringBuilder
String:不可变的字符序列;底层使用char[]储存
StringBuffer:可变的字符序列;线程安全的,效率低;底层使用char[]储存
StringBuilder:可变的字符序列;线程不安全的,效率高;底层使用char[]储存
源码分析 :
String str=new String();//char[] value=new char[0];
String str1=new String(“abc”)//char[] value=new char[]{‘a’,‘b’,‘c’};
StringBuffer sb1=new StringBuffer()//char[] value=new char[16] 底层创建了一个长度是16的数组
sb1.append(‘a’)//value[0]=‘a’
sb1.append(‘b’)//valu[1]=‘b’
StringBuffer sb2=new StringBuffer(“abc”)//char[] value=new char[“abc”.length+16]
package shangGuiGu.day23;
import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.junit.Test;
import org.w3c.dom.ls.LSOutput;
public class demo02 {
@Test
public void test1() {
StringBuffer sb1 = new StringBuffer("abc");
sb1.setCharAt(0, 'm');
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer();
System.out.println(sb2.length());//0
}
@Test
public void test2(){
StringBuffer s1=new StringBuffer("abc");
s1.append(1);
s1.append('2');
System.out.println(s1);
//s1.delete(2,4);//[2,4)
System.out.println(s1);//ab2
//s1.replace(2,4,"hello");
System.out.println(s1);//abhello2
//s1.insert(2,false);
System.out.println(s1);//abfalsec12
//s1.reverse();
System.out.println(s1);//21cba
String s2=s1.substring(1,3);
System.out.println(s2);//bc
}
}
package shangGuiGu.day23;
import org.junit.Test;
import java.util.Date;
public class demo03 {
@Test
public void test(){
long time=System.currentTimeMillis();
System.out.println(time);//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差,称为时间戳
Date date1 = new Date();//显式当前的签约日时分秒
System.out.println(date1.toString());
System.out.println(date1.getTime());//返回当前时间与1970年1月1日0时0分0秒之间以毫秒为单位的时间差
Date date2 = new Date(1637546386739L);
System.out.println(date2);
}
}
package shangGuiGu.day23;
import org.junit.Test;
public class demo04 {
//将一个字符串进行反转
public String reverse(String str, int startIndex, int endIndex) {
if (str != null && str.length() != 0) {
char[] arr = str.toCharArray();
for (int x = startIndex, y = endIndex; x < y; x++, y--) {
char temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
return new String(arr);
}
return null;
}
public String reverse2(String str, int startIndex, int endIndex) {
if(str!=null){
String reversestr=str.substring(0,startIndex);
for(int i=endIndex;i>=startIndex;i--){
reversestr+=str.charAt(i);
}
reversestr+=str.substring(endIndex+1);
return reversestr;
}
return null;}
//优化:使用StringBuffer/StringBuilder
public String reverse3(String str, int startIndex, int endIndex) {
if(str!=null){
StringBuffer builder=new StringBuffer(str.length());
builder.append(str.substring(0,startIndex));
for(int i=endIndex;i>=startIndex;i--){
builder.append(str.charAt(i));
}
builder.append(str.substring(endIndex+1));
return builder.toString();
}return null;}
@Test
public void test(){
String str="abcdrfg";
String reverse2 = reverse2(str, 2, 5);
String reverse = reverse(str, 2, 5);
String reverse3 = reverse3(str, 2, 5);
System.out.println(reverse);
}
}
package shangGuiGu.day23;
import org.junit.Test;
public class demo05 {
public int getcount(String mainstr,String substr){
int mainLength = mainstr.length();
int subLenght=substr.length();
int count=0;
int index = 0;
if(mainLength>=subLenght){
//方式一
// while((index=mainstr.indexOf(substr))!=-1){
// count++;
// mainstr=mainstr.substring(index+substr.length());
// }
//方式二
while((index=mainstr.indexOf(substr,index))!=-1){
count++;
index+=subLenght;
}
return count;
}else {
return 0;}
}
@Test
public void test(){
String mainstr="abcccccccccabcda";
String substr="ab";
int count=getcount(mainstr,substr);
System.out.println(count);
}
}
package shangGuiGu.day23;
import org.junit.Test;
public class demo06 {
public String getmaxsamestring(String str1,String str2){
String maxstr = (str1.length() >= str2.length()) ? str1 : str2;
String minstr=(str1.length() < str2.length()) ? str1 : str2;
int length=minstr.length();
for(int i=0;i


