字符串是由多个字符组成的一串数据(字符序列)
字符串可以看成是字符数组
通过JDK提供的API,查看String类的说明
可以看到这样的两句话。 a:字符串字面值"abc"也可以看成是一个字符串对象,即可以通过字符串直接调用方法。 b:字符串是常量,一旦被创建,就不能被改变。 public class Demo { public static void main(String[] args) { // 1.String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。 // 2.字符串是常量;它们的值在创建之后不能被更改 //构造方法 /*构造方法摘要 String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。 */ //构建一个空的字符串对象,字符串内容为空 String s = new String(); //String 类重写类Object类中的方法,打印字符串内容 System.out.println(s.toString());//打印字符串的内容 System.out.println(""); //"" 空串 /*String(String original) 初始化一个新创建的 String 对象,使其表示一个与参数相同的字符序列; 换句话说,新创建的字符串是该参数字符串的副本。 */ String s1 = new String("abc"); System.out.println(s1.toString()); } } public String(): 空构造
public String(String original): 把字符串常量值转成字符串
public String(byte[] bytes): 把字节数组转成字符串
public String(byte[] bytes,int index,int length): 把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
public String(char[] value): 把字符数组转成字符串
public String(char[] value,int index,int count): 把字符数组的一部分转成字符串
演示String类的常见构造方法
演示前先说一个字符串的方法: public int length():返回此字符串的长度。
public class Demo { public static void main(String[] args) { //public String( byte[] bytes):把字节数组转成字符串 //字符串:由单个或多个字符组成的字符序列 byte[] bytes={97,98,99,100,101}; String s = new String(bytes); System.out.println(s); //可以将字节数组的一部分转换成字符串 //从0索引处开始,转换3个字节 /* public String( byte[] bytes, int index, int length):把字节数组的一部分转成字符串 (index:表示的是从第几个索引开始, length表示的是长度) */ String s1 = new String(bytes,0,3); System.out.println(s1); } } public class Demo { public static void main(String[] args) { char[] chars={'a','b','c','d','你','好'}; //把一个字符数组转换成字符串 //遍历字符数组,拼串 /* String s = new String(); for (int i = 0; i < chars.length; i++) { s += chars[i]; } System.out.println(s); */ String s = new String(chars); System.out.println(s); //可以将字符数组的一部分转换成字符串 String s1 = new String(chars, 4, 2); System.out.println(s1); } }一旦被创建就不能改变,因为字符串的值是在方法区的常量池中划分空间分配地址值的
public class Demo { public static void main(String[] args) { // 字符串是常量:它们的值在创建之后不能更改。能变的是这个指向 String s = "hello"; s = "world" + "java"; //问s的结果是多少 ? System.out.println(s); //结果是worldjava //s = "world" + "java",这部操作改变了s的指向; } } String s1 = new String("hello"); String s2 = new String("hello"); System.out.println(s1 == s2);//false //String 重写了Object类的equal() 比较的是,两个字符串字面上的内容是否相同 System.out.println(s1.equals(s2));//true String s3 = new String("hello"); String s4 = "hello"; System.out.println(s3 == s4);//false System.out.println(s3.equals(s4));//true //采用这种直接赋值的方式,它会先去常量中找,有没有该字符串,如果没有就创建,如果有就把之前的地址值复制过来 String s5 = "hello"; String s6 = "hello"; System.out.println(s5 == s6);//true System.out.println(s5.equals(s6));//true public boolean equals(Object obj): 比较字符串的内容是否相同,区分大小写
public boolean equalsIgnoreCase(String str): 比较字符串的内容是否相同,忽略大小写
public boolean contains(String str): 判断字符串中是否包含传递进来的字符串
public boolean startsWith(String str): 判断字符串是否以传递进来的字符串开头
public boolean endsWith(String str): 判断字符串是否以传递进来的字符串结尾
public boolean isEmpty(): 判断字符串的内容是否为空串""。
B:案例演示:
public class Demo { public static void main(String[] args) { //与判断相关的方法 boolean b = "abc".equals("abc"); //区分大小写 System.out.println(b); boolean b1 = "ABC".equalsIgnoreCase("abc");//不区分大小写 System.out.println(b1); //判断一个字符串中是否包含这个子串 boolean b2 = "往后余生,洗衣是你,做饭是你,带娃还是你".contains("是你"); System.out.println(b2); //判断是否以这个字符串开头或结尾 System.out.println("abc".startsWith("a")); System.out.println("abc".endsWith("c")); //判断是否是空串 System.out.println("".isEmpty()); System.out.println("".length() == 0); } } //可以用于验证密码是否输入正确 public class Demo { public static void main(String[] args) { String name = "张三"; String password = "123456"; for (int i = 1; i <= 3; i++) { Scanner scanner = new Scanner(System.in); System.out.println("请输入你的用户名"); String uname = scanner.nextLine(); System.out.println("请输入你的密码"); String pwd = scanner.nextLine(); if (uname.equals(name) && pwd.equals(password)) { System.out.println("登录成功"); break; } else { if ((3 - i) == 0) { System.out.println("3次机会已经用完,你的卡已被回收."); } else { System.out.println("用户名或密码输入错误,请重新输入,你还剩余" + (3 - i) + "次机会"); } } } } } public int length(): 获取字符串的长度。
public char charAt(int index): 获取指定索引位置的字符。
public int indexOf(int ch): 返回指定字符在此字符串中第一次出现处的索引。
public int indexOf(String str): 返回指定字符串在此字符串中第一次出现处的索引。
public int indexOf(int ch,int fromIndex): 返回指定字符在此字符串中从指定位置后第一次出现处的索引。
public int indexOf(String str,int fromIndex): 返回指定字符串在此字符串中从指定位置后第一次出现处的索引。 public String substring(int start): 从指定位置开始截取字符串,默认到末尾。
public String substring(int start,int end): 从指定位置开始到指定位置结束截取字符串。
public class Demo { public static void main(String[] args) { //有关于获取的方法 int length = "这是一个字符串".length(); //获取长度 //字符串有索引 //indexOf() 查找该字符或字符串,在原串中第一次出现时的索引 int index = "这是一个字符串是".indexOf('是'); //如果没有找到 返回 -1 我们经常用这个作为判断依据 index = "这是一个字符串是".indexOf("字符串2"); System.out.println(index); //charAt(6) 根据索引获取单个字符 char ch = "这是一个字符串是".charAt(6); System.out.println(ch); String s = "这是一是个是字符是串是"; ch = s.charAt(s.indexOf("字")); System.out.println(ch); //从后往前检索 int index1 = s.lastIndexOf("是"); System.out.println(index1); //可以指定开始的地方来检索 int index2 = s.indexOf('是', s.indexOf('个')); System.out.println(index2); //从原串中截取一段字串 String str = "假如我年少有为不自卑,懂得什么是珍贵"; //根据起始索引截取到末尾 String s1 = str.substring(str.indexOf("懂")); System.out.println(s1); //根据首尾索引,截取一部分字符串 String s2 = str.substring(3, str.indexOf('卑')+1); //含头不含尾部 System.out.println(s2); } } //字符串的遍历 public class Demo { public static void main(String[] args) { String str = "假如我年少有为不自卑,懂得什么是珍贵"; // char c = str.charAt(0); //遍历字符串 for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); System.out.println(c); } } } //统计不同类型字符个数 public class MyTest { public static void main(String[] args) { String str="asfdassdfASFSsf1222asdfas5555s4555AFFDDdddaAAA"; int num = 0; int da = 0; int xiao = 0; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if(ch >= 'a' && ch <= 'z'){ xiao++; }else if(ch >= 'A' && ch <= 'Z'){ da++; } else{ num++; } } System.out.println("大写字母"+ da +"个"); System.out.println("小写字母" + xiao + "个"); System.out.println("数字字符" + num + "个"); } } public byte[] getBytes(): 把字符串转换为字节数组。
public char[] toCharArray(): 把字符串转换为字符数组。
public static String valueOf(char[] chs): 把字符数组转成字符串。
public static String valueOf(int i): 把int类型的数据转成字符串。
注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
public String toUpperCase(): 把字符串转成大写。
public String concat(String str): 把字符串拼接。
public String replace(char old,char new) 将指定字符进行互换
public String replace(String old,String new) 将指定字符串进行互换
public class Demo { public static void main(String[] args) { String str="我喜欢奥巴马呀,还喜欢普京"; String s2 = str.replace("奥巴马", "*").replace("普京", "*"); System.out.println(s2); } } public String trim() 去除两端空格
public class Demo { public static void main(String[] args) { String username=" zhangsan "; String s3 = username.trim(); System.out.println(s3); } } public int compareTo(String str) 会对照ASCII 码表 从第一个字母进行减法运算,返回的就是这个减法的结果
如果前面几个字母一样会根据两个字符串的长度进行减法运算,返回的就是这个减法的结果
如果连个字符串一摸一样,返回的就是0
public int compareToIgnoreCase(String str) 跟上面一样 只是忽略大小写的比较
public class Demo { public static void main(String[] args) { String str="abc"; String str2="abcdef"; boolean b = str.equals(str2); // str.equalsIgnoreCase(str) 不区分大小写的比较 //比较两个字符串 按照字典顺序去比,返回的是两个字符的差值 //当字典顺序比不出来,用长度去比 int num = str.compareTo(str2);//区分大小写的比较 if(num == 0){ System.out.println("这两个字符串相同"); } // str.compareToIgnoreCase(str2) 不区分大小写的比较 System.out.println(num); } } public class Demo { public static void main(String[] args) { /* 案例演示 需求:把数组中的数据按照指定个格式拼接成一个字符串 举例: int[] arr = {1, 2, 3}; 拼接结果: "[1, 2, 3]"*/ int[] arr = {1, 2, 3, 4, 5}; //对上面的数组,进行遍历,经过你的处理得到一个漂亮的字符串 //"[1,2,3]"; String str="["; for (int i = 0; i < arr.length; i++) { if(i == arr.length-1){ str += arr[i]+"]"; }else{ str += arr[i]+","; } } System.out.println(str); } }