package test0811;
import java.util.Arrays;
import java.util.Scanner;
public class Test02 {
public static void main(String[] args) {
/*
* 字符串类:
* String:是用final修饰的,不能被继承
* 动态的创建方式,构造方法:
* String 变量名 = new String(); 空的字符序列
* String 变量名 = new String(String str); 有默认值的字符序列
* 静态的创建方式:String s = "hello"
* 字符串创建的内存模型:
* 如果采用静态的方式创建,则字符串常量值存放在方法区中,不同的字符串对象指向方法区中同样的内容.
* 如果采用动态的方式创建,一定会在堆中重新分配内存.
* String字符串类是不可能变的字符序列,对字符串的任何修改都相当于重新构建了一个字符串对象
*
* String类的常用方法:
* 1.boolean equal(String) 比较两个字符串是否相等
* 2.boolean equalsIgnoreCase(String) 比较两个字符串的内容是否相等,同时忽略大小写
* 3.String toUpperCase() 将字符串中所有字符转为大写
* 4.String toLowerCase() 将字符串中所有字符转为小写
* 5.char CharAt(int) 返回指定索引值处的字符,字符串的索引值从零开始
* 6.int length() 返回字符串的长度
* 7.String substring(int begin) 返回一个新的字符串,内容从begin开始到最后
* 8.String subString(int begin,int end) 返回一个新的字符串,该字符串是从begin开始到end-1结束的字符串的内容
* 9.int indexOf/lastIndexOf(char) 返回字符在此字符串中第一次/最后一次出现的索引
* 如果找不到指定的字符或字符串,返回值-1
* 10.String trim() 去除字符串前后空格,返回一个新的字符串
* 11.String concat(String str) 将指定字符串连接到此字符串的结尾,字符串拼接,返回新字符串
* 12.byte[] getBytes() 使用平台的默认字符集将次String编码为byte序列,并将结果存储到一个新的byte数组中
* 13.String[] split(String str) 按照指定字符串将原字符串分割成一个数组
* 14.String replace(char a,char b) 将字符串中字符a全部替换成字符b,返回新字符串
* 15.boolean startsWith(String str) 判断该字符串是否以什么开头
* 16.boolean endsWith(String str) 判断该字符串是否以什么结尾
*
*/
/*
String str = new String();
System.out.println(str);
String str1 = new String("abc");
System.out.println(str1);//abc
String s = "hello";
System.out.println(s);
*/
/*
String s1 = "hello";//引用数据类型会涉及到内存地址的分配
String s2 = "hello";
String s3 = new String("hello");
String s4 = new String("hello");
s4 = "hello";
System.out.println(s1==s2);//true--地址一样
System.out.println(s2==s3);//false--地址不一样
System.out.println(s1==s4);//true
System.out.println(s3==s4);//false
*/
String s2 = "hello";
String s3 = new String("hello");
System.out.println(s2.equals(s3));//true
String s4 = "Hello";
System.out.println(s2.equalsIgnoreCase(s4));//true
String str = s2.toUpperCase();
System.out.println(str);//HELLO
s2.toUpperCase();
System.out.println(s2);//hello 调用方法后没有值进行接收,对原字符串无任何影响
String s = "aaBBBBdd";
s = s.toLowerCase();
System.out.println(s);//aabbbbdd
String str5 = "ascdefg";
System.out.println(str5.charAt(0));//a
for(int i=0;i<str5.length();i++){
System.out.print(str5.charAt(i)+" ");
}
//从控制台输入一个字符串,统计字符串中a出现的次数,忽略大小写
System.out.println("请输入一个字符串:");
int count = 0;
String strr = new Scanner(System.in).next();
strr = strr.toLowerCase();
for(int i=0;i<strr.length();i++){
if('a'==strr.charAt(i)){
count++;
}
}
System.out.println("字符串中a出现的次数"+count);
String c = "I Love You! you";
String s1 = c.substring(3);
System.out.println(s1);//ove You!
String cc = c.substring(0, 3);
System.out.println(cc);//I L
System.out.println(c.indexOf('o'));//3
System.out.println(c.lastIndexOf('o'));//13
System.out.println(c.indexOf("you"));//12
System.out.println(c.lastIndexOf("you"));//12
System.out.println(c.indexOf('o',4));//8
String ss = " aaaa bb bb ";
String sc = ss.trim();
System.out.println(sc);//aaaa bb bb
String m = "11";
m = m.concat("22");
System.out.println(m);//1122
String sa = "hello";
byte[] b = s.getBytes();
System.out.println(Arrays.toString(b));//[97, 97, 98, 98, 98, 98, 100, 100]
String d = "aa,bb,cc,dd";
String[] arrm = d.split(",");
System.out.println(Arrays.toString(arrm));//[aa, bb, cc, dd]
String u = "hello";
u = u.replace('l', 'L');
System.out.println(u);//heLLo
System.out.println(u.startsWith("dd"));//false
System.out.println(u.endsWith("o"));//true
/*
* StringBuffer是可变的字符序列,只改变一个值,不耗费内存
* append方法:追加字符
* insert方法:插入字符
* toString方法:将StringBuffer类型对象转为String类型(对String类型进行了重写)
*
* StringBuilder的使用方法和StringBuffer完全相同
*
* StringBuffer和StringBuilder的区别
* 1.StringBuffer是线程安全的,StringBuilder是线程不安全的
* 2.StringBuffer是JDK1.0出现的,StringBuilder是JDK1.5出现的
* 3.StringBuilder的效率比StringBuffer快一些
*
*/
StringBuffer sbf = new StringBuffer("hello");
sbf.append("!!!");
sbf.append("!!");
System.out.println(sbf.toString());//hello!!!!!
}
}
package test0810;
public class Test06 {
public static void main(String[] args) throws ClassNotFoundException {
String str1 = new String("aaa");//新创建对象
String str2 = new String("aaa");//新创建对象 str1和str2地址不同
System.out.println(str1.equals(str2));//true
String str3 = "aaa";//字符串内部一定在字符数组中,在常量池里创建aaa
String str4 = "aaa";//在常量池里寻找aaa
System.out.println(str3==str4);//true,两者地址相同
}
}
package test0810;
public class Test08 {
public static void main(String[] args) {
String str = "aaa";
System.out.println(str.equalsIgnoreCase("Aaa"));//true
String str1 = "Aaa";
System.out.println(str1.toLowerCase());//aaa
System.out.println(str1.toUpperCase());//AAA
// 还原方法 public String toLowerCase(){}
String str2 = "abcdefg";
System.out.println(str2.charAt(3));//根据索引找字符 d
// 还原charAt方法 public char charAt(int num){}
for (int i = str2.length() - 1; i >= 0; i--) {// 反转字符串
System.out.print(str2.charAt(i)); //gfedcba
}
String str3 = "hello world!";
System.out.println(str3.indexOf('o'));//找位置 方法重载 4
System.out.println(str3.indexOf("or"));//找字符串 7 方法重载
System.out.println(str3.indexOf("orr"));// -1
//还原方法 public int indexOf(char c){}
System.out.println(str3.lastIndexOf('o')); //7
String str4 = "hello world!";
System.out.println(str4.substring(3));//lo world!
System.out.println(str4.substring(3,7));//lo w
String a = "abc";
System.out.println("abc".length());//3
System.out.println(" hello ".trim());//hello
//***面试题
//在Java中获取数组长度要使用length属性,获取String长度要使用length()方法
int[] an = new int[6];
System.out.println(an.length);//6
String strn = "sfsdfs";
System.out.println(strn.length());//6
}
}