导包:

import java.lang.String(可以不用写)

字符串是Unicode字符序列,程序中所有双引号的字符串,都是String类的对象实例

字符串的特点:

1、字符串的内容永不改变【重点】

java文档中将String 类对象称为是不可变的(immutable)

如果想要修改字符串,可以提取想要保留的子串,再与希望替换的 字符拼接

String s="hello";
s=s.substring(0,3)+"p!";
s由hello改为help

Android定义字符串 定义字符串java_字符串

2、因为字符串不可改变,所以字符串是可以共享使用的。可以修改字符串变量,让它引用另一个字符串。(相当于原本存放3的变量改成存放4一样)

3、但是底层原理是byte[]字节数组。

创建字符串:

构造方法:

 Stirng():创建一个空白字符串,不含任何内容。

String(char[]array)根据字符数组的内容,来创建对应的字符串

String(byte[]array)根据字节数组的内容,来创建对应的字符串

Android定义字符串 定义字符串java_后端_02

直接创建:

String str="hello"

空  串:长度为0的串,是一个Java对象有自己的长度和内容。

NULL:表示目前没有任何对象与该变量关联。

字符串常量池:

程序当中直接写上的双引号字符串,就在字符串常量池中

对于基本类型来说,==是进行数值的比较

对于引用类型来说,==是进行地址值的比较,只能判断两个字符串是否放在同一个位置上。

所以,在Java中,对于字符串的比较,不能使用==。因为,有可能内容相同的多个字符串副本放在不同的位置上。

Android定义字符串 定义字符串java_Android定义字符串_03


字符串常用方法:

==是进行对象地址值的比较

equals(Object obj):

参数可以是任何对象,只有参数是一个字符串并且内容相同的才会给true,否则返回false

注意:

1、任何对象都能用object接收

2、equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样

3、如果比较双方一个常量一个变量,推荐写法:常量.equals(变量);防止NullPointer 

package String;

public class equal {
    public static void main(String[] args) {
        String s1="hello";
        String s2="hello";
        char[] charArray={'h','e','l','l','o'};
        String s3=new String(charArray);
        System.out.println(s1.equals(s2));//true
        System.out.println(s2.equals(s3));//true
    }
}

equalsIgnoreCase(String str):忽略大小写,进行内容比较

package String;

public class equal {
    public static void main(String[] args) {
        String s1="hello";
        String s2="Hello";
        
        System.out.println(s1.equalsIgnoreCase(s2));//true
      
    }
}

int compareTo(String other)按照字典顺序,如果字符串位于other之前,返回一个负数,如果字符串位于other之后,返回一个整数, 如果两个字符串相等,返回0


length():获取字符串长度

String s1="111";
int length=s1.length();//3

concat(String str):将当前字符串和参数字符串拼接成为 新的字符串

String s1="hello";
String s2="world";
String s3=s1.concat(s2);//s3=helloworld,s1s2不变

char charAt(int index):获取指定索引位置的单个字符(索引从0开始)

String s="hello";
char c=s.charAt(1);//e

int indexOf(String str)/indexof(String str,int fromindex):查找参数字符串在本字符串中首次出现的索引位置,如果没有,返回-1值

String s1="helloworld";
int index=s1.indexOf("llo");//2
int index1=s1.index0f("Ll0");//-1

int lastindexof(String str)/lastindexof(String str,int fromindex):返回与字符串str匹配的最后一个子串的开始位置

 substring(int index):截取从参数位置一直到字符串末尾,返回新字符串

String s1="helloworld";
String s2=s1.substring(5);//world

substring(int begin,int end):截取从begin开始,一直到end-1结束,中间的字符串

String s1="helloworld";
String s2=s1.substring(4,7);//owo

 char[] toCharArray():将当前字符串拆分为字符数组作为返回值

 byte[] getBytes():获得当前字符串底层的字节数组

 replace(charSequence old,CharSequence new):将所有出现的老字符串替换成为新的字符串,返回替换之后的结果字符串

备注:CharSequence为接口,可以接受String类型

char []chars="hello".toCharArray();//转换成为字符数组
chars.length;//5
byte[] bytes="abc".getBytes();//转换成为字节数组
//97 98 99
String s1="how do you do";
Strings2=s1.replace("0","*");//h*w d* y*u d*

 String[] split(String regex):按照参数的规则,将字符串切分成为若干部分

String s1="aaa,bbb,ccc";
String[]array1=s1.split(",");
//aaa  bbb ccc
String s2="aaa bbb ccc";
String[] array2=s2.split(" ");
//aaa bbb ccc

注意:

参数为“正则表达式”:(不能以”.“分割)

startsWith(String prefix)、endsWith(String suffix):判断字符串是否以..开头/结尾

empty():判断字符串是否为空

blank():判断字符串是否以空格组成

toLowerCase():转小写

toUpperCase():转大写

strip():返回一个新字符串,删除原始字符头部和尾部的字符或空格

join(a,charsequence b):用a连接字符串序列b

repeat(int count):当前字符串重复count次

补充:

Android定义字符串 定义字符串java_后端_04