介绍
package com.heima.string;
public class Demo1_String {
/**
* a:字符串字面值"abc"也可以看成是一个字符串对象。
* b:字符串是常量,一旦被赋值,就不能被改变。
*/
public static void main(String[] args) {
String str = "abc";
str = "def";
System.out.println(str);
}
}
构造
package com.heima.string;
public class Demo2_StringCon {
/**
* public String():空构造
* public String(byte[] bytes):把字节数组转成字符串
* public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串
* public String(char[] value):把字符数组转成字符串
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
* public String(String original):把字符串常量值转成字符串
*/
public static void main(String[] args) {
String s1 = new String();
System.out.println(s1);
byte[] arr1 = {97,98,99};
String s2 = new String(arr1);
System.out.println(s2);
byte[] arr2 = {97,98,99,100,101,102};
String s3 = new String(arr2,2,3);
System.out.println(s3);
char[] arr3 = {'a','b','c','d','e'};
String s4 = new String(arr3);
System.out.println(s4);
String s5 = new String(arr3,1,3);
System.out.println(s5);
String s6 = new String("heima");
System.out.println(s6);
}
}
面试题
- 常量池中没有这个字符串对象,则创建一个,如果有可以直接使用。
- 2.
package com.heima.string;
public class Demo3_String {
/**
* * 1.判断定义为String类型的s1和s2是否相等
* String s1 = "abc";
* String s2 = "abc";
* System.out.println(s1 == s2);
* System.out.println(s1.equals(s2));
* 2.下面这句话在内存中创建了几个对象?
* String s1 = new String("abc");
* 3.判断定义为String类型的s1和s2是否相等
* String s1 = new String("abc");
* String s2 = "abc";
* System.out.println(s1 == s2);
* System.out.println(s1.equals(s2));
* 4.判断定义为String类型的s1和s2是否相等
* String s1 = "a" + "b" + "c";
* String s2 = "abc";
* System.out.println(s1 == s2); ?
* System.out.println(s1.equals(s2)); ?
* 5.判断定义为String类型的s1和s2是否相等
* String s1 = "ab";
* String s2 = "abc";
* String s3 = s1 + "c";
* System.out.println(s3 == s2);
* System.out.println(s3.equals(s2)); ?
*/
public static void main(String[] args) {
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2);
System.out.println(s3.equals(s2));
}
private static void demo4() {
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
private static void demo3() {
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
private static void demo2() {
String s1 = new String("abc");
System.out.println(s1);
}
private static void demo1() {
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
}
}
判断功能
package com.heima.string
public class Demo4_StringMethod {
public static void main(String[] args) {
//demo1()
//demo2()
String s1 = "heima"
String s2 = ""
String s3 = null
System.out.println(s1.isEmpty())
System.out.println(s2.isEmpty())
System.out.println(s3.isEmpty())
}
private static void demo2() {
String s1 = "我爱heima,哈哈"
String s2 = "heima"
String s3 = "baima"
String s4 = "我爱"
String s5 = "哈哈"
System.out.println(s1.contains(s2))
System.out.println(s1.contains(s3))
System.out.println("------------------")
System.out.println(s1.startsWith(s4))
System.out.println(s1.startsWith(s5))
System.out.println("------------------")
System.out.println(s1.endsWith(s4))
System.out.println(s1.endsWith(s5))
}
private static void demo1() {
String s1 = "heima"
String s2 = "heima"
String s3 = "HeiMa"
System.out.println(s1.equals(s2))
System.out.println(s2.equals(s3))
System.out.println("---------------")
System.out.println(s1.equalsIgnoreCase(s2))
System.out.println(s1.equalsIgnoreCase(s3))
}
}
常见方法
/**
* * int length():获取字符串的长度。
* char charAt(int index):获取指定索引位置的字符
* int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引。
* int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
* int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
* int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
* lastIndexOf
* String substring(int start):从指定位置开始截取字符串,默认到末尾。
* String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
*/
String类转换功能
/**
* * byte[] getBytes():把字符串转换为字节数组。(通过gbk码表,讲字符串转换成字节数组)
* char[] toCharArray():把字符串转换为字符数组。
* static String valueOf(char[] chs):把字符数组转成字符串。
* static String valueOf(int i):把int类型的数据转成字符串。
* 注意:String类的valueOf方法可以把任意类型的数据转成字符串。
* String toLowerCase():把字符串转成小写。(了解)
* String toUpperCase():把字符串转成大写。
* String concat(String str):把字符串拼接。
*/
/**
* * A:String的替换功能及案例演示
* String replace(char old,char new)
* String replace(String old,String new)
* B:String的去除字符串两空格及案例演示
* String trim()
* C:String的按字典顺序比较两个字符串及案例演示
* int compareTo(String str)(暂时不用掌握)
* int compareToIgnoreCase(String str)(了解)
便利字符串
/**
* * A:案例演示
* 需求:遍历字符串
*/
public static void main(String[] args) {
String s = "heima";
for(int i = 0; i < s.length(); i++) {
System.out.println(s.charAt(i));
}
}
把一个字符串的首字母转成大写,其余为小写。
package com.heima.test;
public class Test4 {
/**
* * A:案例演示
* 需求:把一个字符串的首字母转成大写,其余为小写。(只考虑英文大小写字母字符)
* 链式编程:只要保证每次调用完方法返回的是对象,就可以继续调用
*/
public static void main(String[] args) {
String s = "woaiHEImaniaima";
String s2 = s.substring(0, 1).toUpperCase().concat(s.substring(1).toLowerCase());
System.out.println(s2);
}
}
package com.heima.test;
public class Test5 {
/**
* * A:案例演示
* 需求:把数组中的数据按照指定个格式拼接成一个字符串
* 举例:
* int[] arr = {1,2,3};
* 输出结果:
* "[1, 2, 3]"
*
分析:
1,需要定义一个字符串"["
2,遍历数组获取每一个元素
3,用字符串与数组中的元素进行拼接
*/
public static void main(String[] args) {
int[] arr = {1,2,3};
String s = "[";
for (int i = 0; i < arr.length; i++) {
if(i == arr.length - 1) {
s = s + arr[i] + "]";
}else {
s = s + arr[i] + ", ";
}
}
System.out.println(s);
}
}
package com.heima.test;
import java.util.Scanner;
public class Test6 {
/**
* * A:案例演示
* 需求:把字符串反转
* 举例:键盘录入"abc"
* 输出结果:"cba"
*分析:
*1,通过键盘录入获取字符串Scanner
*2,将字符串转换成字符数组
*3,倒着遍历字符数组,并再次拼接成字符串
*4,打印
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String line = sc.nextLine();
char[] arr = line.toCharArray();
String s = "";
for(int i = arr.length-1; i >= 0; i--) {
s = s + arr[i];
}
System.out.println(s);
}
}