4.Java String

1.初始化

可以使用数组来初始化,分配一个新的字符串,将参数中的字符数组元素全部变为字符串。该字符数组的内容已被复制,后续对字符数组的修改不会影响新创建的字符串。

char a[] = {'H','e','l','l','0'};
String sChar = new String(a);
a[1] = 's';

 String(char[] value,int offset,int count)

sChar 变量的值是字符串“ello”。该构造方法使用字符数组中的部分连续元素来创建字符串对象。offset 参数指定起始索引值,count 指定截取元素的个数。

char a[]={'H','e','l','l','o'};
String sChar=new String(a,1,4);
a[1]='s';

2.String转int

  • Integer.parseInt(str)
  • Integer.valueOf(str).intValue()
public static void main(String[] args) {
    String str = "123";
    int n = 0;

    // 第一种转换方法:Integer.parseInt(str)
    n = Integer.parseInt(str);
    System.out.println("Integer.parseInt(str) : " + n);

    // 第二种转换方法:Integer.valueOf(str).intValue()
    n = 0;
    n = Integer.valueOf(str).intValue();
    System.out.println("Integer.parseInt(str) : " + n);
}

3.int转String

使用第三种方法相对第一第二种耗时比较大。在使用第一种 valueOf() 方法时,注意 valueOf 括号中的值不能为空,否则会报空指针异常

  • String s = String.valueOf(i);
  • String s = Integer.toString(i);
  • String s = "" + i;
public static void main(String[] args) {
    int num = 10;

    // 第一种方法:String.valueOf(i);
    num = 10;
    String str = String.valueOf(num);
    System.out.println("str:" + str);

    // 第二种方法:Integer.toString(i);
    num = 10;
    String str2 = Integer.toString(num);
    System.out.println("str2:" + str2);

    // 第三种方法:"" + i;
    String str3 = num + "";
    System.out.println("str3:" + str3);
}

4.字符串拼接

String 字符串拼接可以使用“+”运算符或 String 的 concat(String str) 方法。“+”运算符优势是可以连接任何类型数据拼接成为字符串,而 concat 方法只能拼接 String 类型字符串。

在使用“+”运算符连接字符串和 int 型(或 double 型)数据时,“+”将 int(或 double)型数据自动转换成 String 类型。

5.字符串长度

字符串名.length();

6.大小写转换、

字符串名.toLowerCase()    // 将字符串中的字母全部转换为小写,非字母不受影响
字符串名.toUpperCase()    // 将字符串中的字母全部转换为大写,非字母不受影响

7.去除字符串前后空格

字符串名.trim()

注意:trim() 只能去掉字符串中前后的半角空格(英文空格),而无法去掉全角空格(中文空格)。可用以下代码将全角空格替换为半角空格再进行操作,其中替换是 String 类的 replace() 方法

str = str.replace((char) 12288, ' ');    // 将中文空格替换为英文空格
str = str.trim();

其中,12288 是中文全角空格的 unicode 编码。

8.提取子串

substring(int beginIndex) 形式

此方式用于提取从索引位置开始至结尾处的字符串部分。调用时,括号中是需要提取字符串的开始位置,方法的返回值是提取的字符串。

String str = "我爱 Java 编程";
String result = str.substring(3);
System.out.println(result);    // 输出:Java 编程

 substring(int beginIndex,int endIndex) 形式

public static void main(String[] args) {
    String day = "Today is Monday";    //原始字符串
    System.out.println("substring(0)结果:"+day.substring(0));
    System.out.println("substring(2)结果:"+day.substring(2));
    System.out.println("substring(10)结果:"+day.substring(10));
    System.out.println("substring(2,10)结果:"+day.substring(2,10));
    System.out.println("substring(0,5)结果:"+day.substring(0,5));
}

9.字符串的分割

String 类的 split() 方法可以按指定的分割符对目标字符串进行分割,分割后的内容存放在字符串数组中。该方法主要有如下两种重载形式:

str.split(String sign)
str.split(String sign,int limit)
  • str 为需要分割的目标字符串。
  • sign 为指定的分割符,可以是任意字符串。
  • limit 表示分割后生成的字符串的限制个数,如果不指定,则表示不限制,直到将整个目标字符串完全分割为止。
public static void main(String[] args) {
    String Colors = "Red,Black,White,Yellow,Blue";
    String[] arr1 = Colors.split(","); // 不限制元素个数
    String[] arr2 = Colors.split(",", 3); // 限制元素个数为3
    System.out.println("所有颜色为:");
    for (int i = 0; i < arr1.length; i++) {
        System.out.println(arr1[i]);
    }
    System.out.println("前三个颜色为:");
    for (int j = 0; j < arr2.length; j++) {
        System.out.println(arr2[j]);
    }
}

10.字符串的替换

replace() 方法用于将目标字符串中的指定字符(串)替换成新的字符(串),其语法格式如下:

字符串.replace(String oldChar, String newChar)
public static void main(String[] args) {
    String words = "hello java,hello php";
    System.out.println("原始字符串是'"+words+"'");
    System.out.println("replace(\"l\",\"D\")结果:"+words.replace("l","D"));
    System.out.println("replace(\"hello\",\"你好\")结果:"+words.replace("hello","你好 "));
    words = "hr's dog";
    System.out.println("原始字符串是'"+words+"'");
    System.out.println("replace(\"r's\",\"is\")结果:"+words.replace("r's","is"));
}

11.字符串的比较

equals() 方法将逐个地比较两个字符串的每个字符是否相同。如果两个字符串具有相同的字符和长度,它返回 true,否则返回 false。

String str1 = "abc";
String str2 = new String("abc");
String str3 = "ABC";
System.out.println(str1.equals(str2)); // 输出 true
System.out.println(str1.equals(str3)); // 输出 false

equalsIgnoreCase() 方法的作用和语法与 equals() 方法完全相同,唯一不同的是 equalsIgnoreCase() 比较时不区分大小写。当比较两个字符串时,它会认为 A-Z 和 a-z 是一样的。

 

理解 equals() 方法和==运算符执行的是两个不同的操作是重要的。如同刚才解释的那样,equals() 方法比较字符串对象中的字符。而==运算符比较两个对象引用看它们是否引用相同的实例。

String s1 = "Hello";
String s2 = new String(s1);
System.out.println(s1.equals(s2)); // 输出true
System.out.println(s1 == s2); // 输出false

变量 s1 指向由“Hello”创建的字符串实例。s2 所指的的对象是以 s1 作为初始化而创建的。因此这两个字符串对象的内容是一样的。但它们是不同的对象,这就意味着 s1 和 s2 没有指向同一的对象,因此它们是不==的。

compareTo() 方法用于按字典顺序比较两个字符串的大小,该比较是基于字符串各个字符的 Unicode 值。

它会按字典顺序将 str 表示的字符序列与 otherstr 参数表示的字符序列进行比较。如果按字典顺序 str 位于 otherster 参数之前,比较结果为一个负整数;如果 str 位于 otherstr 之后,比较结果为一个正整数;如果两个字符串相等,则结果为 0。

12空字符串和null的区别

“”是一个长度为 0 且占内存的空字符串,在内存中分配一个空间,可以使用 Object 对象中的方法。

null 是空引用,表示一个对象的值,没有分配内存,调用 null 的字符串的方法会抛出空指针异常。

有时要检查一个字符串既不是 null 也不为空串,这种情况下就需要使用以下条件:

if (str != null && str.length() != 0)

public static void main(String[] args) {
    String str1 = new String();
    String str2 = null;
    String str3 = "";

    System.out.println(str3.length()); // 空字符串""的长度为0
    System.out.println(str2.length()); // 抛出空指针异常

    System.out.println(str1); // 输出""
    System.out.println(str1 == str2); // 内存地址的比较,返回false
    System.out.println(str1.equals(str2)); // 值的比较,返回false
    System.out.println(str2 == str3); // 内存地址的比较,返回false
    System.out.println(str3.equals(str2)); // 值的比较,返回false
    System.out.println(str1 == str3); // 内存地址的比较,返回false
    System.out.println(str1.equals(str3)); // 值的比较,返回true
}

13.字符串的查找

indexOf() 方法用于返回字符(串)在指定字符串中首次出现的索引位置,如果能找到,则返回索引值,否则返回 -1。

str.indexOf(value)
str.indexOf(value,int fromIndex)

其中,str 表示指定字符串;value 表示待查找的字符(串);fromIndex 表示查找时的起始索引,如果不指定 fromIndex,则默认从指定字符串中的开始位置(即 fromIndex 默认为 0)开始查找。

String s = "Hello Java";
int size = s.indexOf('v');    // size的结果为8
public static void main(String[] args) {
    String words = "today,monday,sunday";
    System.out.println("原始字符串是'"+words+"'");
    System.out.println("indexOf(\"day\")结果:"+words.indexOf("day"));
    System.out.println("indexOf(\"day\",5)结果:"+words.indexOf("day",5));
    System.out.println("indexOf(\"o\")结果:"+words.indexOf("o"));
    System.out.println("indexOf(\"o\",6)结果:"+words.indexOf("o",6));
}
原始字符串是'today,monday,sunday'
indexOf("day")结果:2
indexOf("day",5)结果:9
indexOf("o")结果:1
indexOf("o",6)结果:7

lastIndexOf() 方法用于返回字符(串)在指定字符串中最后一次出现的索引位置,如果能找到则返回索引值,否则返回 -1。

lastIndexOf() 方法的查找策略是从右往左查找,如果不指定起始索引,则默认从字符串的末尾开始查找。

public static void main(String[] args) {
    String words="today,monday,Sunday";
    System.out.println("原始字符串是'"+words+"'");
    System.out.println("lastIndexOf(\"day\")结果:"+words.lastIndexOf("day"));
    System.out.println("lastIndexOf(\"day\",5)结果:"+words.lastIndexOf("day",5));
    System.out.println("lastIndexOf(\"o\")结果:"+words.lastIndexOf("o"));
    System.out.println("lastlndexOf(\"o\",6)结果:"+words.lastIndexOf("o",6));
}
原始字符串是'today,monday,Sunday'
lastIndexOf("day")结果:16
lastIndexOf("day",5)结果:2
lastIndexOf("o")结果:7
lastlndexOf("o",6)结果:1

String 类的 charAt() 方法可以在字符串内根据指定的索引查找字符

String words = "today,monday,sunday";
System.out.println(words.charAt(0));    // 结果:t
System.out.println(words.charAt(1));    // 结果:o
System.out.println(words.charAt(8));    // 结果:n

14.StringBuffer

StringBuffer 类是可变字符串类,创建 StringBuffer 类的对象后可以随意修改字符串的内容。每个 StringBuffer 类的对象都能够存储指定容量的字符串,如果字符串的长度超过了 StringBuffer 类对象的容量,则该对象的容量会自动扩大。

StringBuffer 类的 append() 方法用于向原有 StringBuffer 对象中追加字符串。

StringBuffer buffer = new StringBuffer("hello,");    // 创建一个 StringBuffer 对象
String str = "World!";
buffer.append(str);    // 向 StringBuffer 对象追加 str 字符串
System.out.println(buffer.substring(0));    // 输出:Hello,World!

StringBuffer 类的 setCharAt() 方法用于在字符串的指定索引位置替换一个字符。

StringBuffer 对象.setCharAt(int index, char ch);
StringBuffer sb = new StringBuffer("hello");
sb.setCharAt(1,'E');
System.out.println(sb);    // 输出:hEllo
sb.setCharAt(0,'H');
System.out.println(sb);    // 输出:HEllo
sb.setCharAt(2,'p');
System.out.println(sb);    // 输出:HEplo

StringBuffer 类中的 reverse() 方法用于将字符串序列用其反转的形式取代。

deleteCharAt() 方法用于移除序列中指定位置的字符,deleteCharAt() 方法的作用是删除指定位置的字符,然后将剩余的内容形成一个新的字符串。

StringBuffer sb = new StringBuffer("She");
sb.deleteCharAt(2);
System.out.println(sb);    // 输出:Se

delete() 方法用于移除序列中子字符串的字符,start 表示要删除字符的起始索引值(包括索引值所对应的字符),end 表示要删除字符串的结束索引值(不包括索引值所对应的字符)。

StringBuffer 对象.delete(int start,int end);
StringBuffer sb = new StringBuffer("hello jack");
sb.delete(2,5);
System.out.println(sb);    // 输出:he jack
sb.delete(2,5);
System.out.println(sb);    // 输出:heck

15.String、StringBuffer和StringBuilder的区别

在 Java 中字符串属于对象,Java 提供了 String 类来创建和操作字符串。String 类是不可变类,即一旦一个 String 对象被创建以后,包含在这个对象中的字符序列是不可改变的,直至这个对象被销毁。
StringBuilder 类是 JDK 1.5 新增的类,它也代表可变字符串对象。实际上,StringBuilder 和 StringBuffer 功能基本相似,方法也差不多。不同的是,StringBuffer 是线程安全的,而 StringBuilder 则没有实现线程安全功能,所以性能略高。因此在通常情况下,如果需要创建一个内容可变的字符串对象,则应该优先考虑使用 StringBuilder 类。

操作少量的数据使用 String。
单线程操作大量数据使用 StringBuilder。
多线程操作大量数据使用 StringBuffer。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值