String类、常用方法(length, charAt, contains, toCharArray, indexOf, lastIndexOf, trim, toUpperCase...)

一、String

  • 字符串是常量,创建之后不可改变。
  • 字符串字面值存储在字符串池中,可以共享。
  • String s = “Hello”; 产生一个对象,字符串池中存储。
package com.jacyzhu.string;

public class Demo01 {
    public static void main(String[] args) {
        String name = "hello"; // "hello"常量存储在字符串池中
        name = "zhangsan"; // "zhangsan"赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开辟一个空间
        String name2 = "zhangsan";
    }
}

在这里插入图片描述

  • Strings = new String(“Hello”); // 产生两个对象,堆、池各存储一个。
package com.jacyzhu.string;

public class Demo01 {
    public static void main(String[] args) {
        // 演示字符串的另一种创建方式
        String str = new String("java");
        String str2 = new String("java");
        System.out.println(str == str2); // false
        System.out.println(str.equals(str2)); // true
    }
}

在这里插入图片描述

二、常用方法

  • public int length():返回字符串的长度
  • public char charAt(int index):根据下标获取字符
  • public boolean contains(String str):判断当前字符串中是否包含str
  • public char[] toCharArray():将字符串转换成数组
  • public int indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
  • public int lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
  • public String trim():去掉字符串前后的空格
  • public String toUpperCase():将小写转成大写;public String toLowerCase():将大写转成小写;
  • public boolean endWith(String str):判断字符串是否以str结尾;public boolean startWith(String str):判断字符串是否以str开头
  • public String replace(char oldChar, char newChar):将旧字符串替换成新字符串
  • public String[] split(String str):根据str做拆分
package com.jacyzhu.string;

import com.sun.xml.internal.ws.api.ha.StickyFeature;

import java.util.Arrays;

public class Demo02 {
    public static void main(String[] args) {

        System.out.println("-----------字符串方法的使用1----------");
        // 字符串方法的使用
        // 1.length():返回字符串的长度
        // 2.charAt(int index):根据下标获取字符
        // 3.contains(String str):判断当前字符串中是否包含str
        String content = "java是世界上最好的java编程语言,java真香";
        System.out.println(content.length()); // 26

        System.out.println(content.charAt(0)); // j
        System.out.println(content.charAt(content.length()-1)); // 香

        System.out.println(content.contains("java")); // true
        System.out.println(content.contains("php")); // false

        System.out.println("-----------字符串方法的使用2----------");
        // 4.toCharArray():将字符串转换成数组
        // 5.indexOf(String str):查找str首次出现的下标,存在,则返回该下标;不存在,则返回-1
        // 6.lastIndexOf(String str):查找字符串在当前字符串中最后一次出现的下标索引
        System.out.println(content.toCharArray()); // java是世界上最好的编程语言
        System.out.println(Arrays.toString(content.toCharArray())); // [j, a, v, a, 是, 世, 界, 上, 最, 好, 的, j, a, v, a, 编, 程, 语, 言, ,, j, a, v, a, 真, 香]

        System.out.println(content.indexOf("java")); // 0
        System.out.println(content.indexOf("java", 4)); // 11

        System.out.println(content.lastIndexOf("java")); // 20

        System.out.println("-----------字符串方法的使用3----------");
        // 7.trim():去掉字符串前后的空格
        // 8.toUpperCase():将小写转成大写;toLowerCase():将大写转成小写;
        // 9.endWith(String str):判断字符串是否以str结尾;startWith(String str):判断字符串是否以str开头
        String content2 = "  hello World  ";

        System.out.println(content2.trim()); // hello World

        System.out.println(content2.toUpperCase()); //   HELLO WORLD
        System.out.println(content2.toLowerCase()); //   hello world

        String filename = "hello.java";

        System.out.println(filename.endsWith(".java")); // true
        System.out.println(filename.startsWith("hello")); // true

        System.out.println("-----------字符串方法的使用4----------");
        // 10.replace(char oldChar, char newChar):将旧字符串替换成新字符串
        // 11.split(String str):根据str做拆分
        System.out.println(content.replace("java", "php")); // php是世界上最好的php编程语言,php真香

        String say = "java is the best programing language";
        String[] arr = say.split(" ");
        System.out.println(arr.length);
        for (String s : arr) {
            System.out.println(s);
        }
        System.out.println("======================");
        String say2 = "java is the best programing language,java is yyds";
        String[] arr2 = say2.split("[ ,]");
        for (String s : arr2) {
            System.out.println(s);
        }
        System.out.println("======================");
        String say3 = "java is the best   programing language,java is yyds";
        String[] arr3 = say3.split("[ ,]+");
        for (String s : arr3) {
            System.out.println(s);
        }

        System.out.println("-------------补充-------------");
        // 补充两个方法equals()、compareTo():比较大小
        String s1 = "hello";
        String s2 = "HELLO";
        System.out.println(s1.equals(s2)); // false
        System.out.println(s1.equalsIgnoreCase(s2)); // true 忽略大小写

        String s3 = "abc"; // 97  98  99
        String s4 = "xyzw"; // 120 121 122
        System.out.println(s3.compareTo(s4)); // -23  97-120

        String s5 = "abc"; // 长度 3
        String s6 = "abcxyz"; // 长度 6
        System.out.println(s5.compareTo(s6)); // -3  3-6

        String s7 = "abc";
        String s8 = "abc";
        System.out.println(s7.compareTo(s8)); // 0
    }
}
运行结果:
-----------字符串方法的使用1----------
26
j
香
true
false
-----------字符串方法的使用2----------
java是世界上最好的java编程语言,java真香
[j, a, v, a,,,,,,,, j, a, v, a,,,,,, j, a, v, a,,]
0
11
20
-----------字符串方法的使用3----------
hello World
  HELLO WORLD  
  hello world  
true
true
-----------字符串方法的使用4----------
php是世界上最好的php编程语言,php真香
6
java
is
the
best
programing
language
======================
java
is
the
best
programing
language
java
is
yyds
======================
java
is
the
best
programing
language
java
is
yyds
-------------补充-------------
false
true
-23
-3
0

练习

需求:
    已知String str = "this is a text";
    1.将str中的单词单独获取出来
    2.将str中的text替换为practice
    3.在text前面插入一个easy
    4.将每个单词的首字母改为大写
package com.jacyzhu.string;

public class Demo03 {
    /*
    需求:
    已知String str = "this is a text";
    1.将str中的单词单独获取出来
    2.将str中的text替换为practice
    3.在text前面插入一个easy
    4.将每个单词的首字母改为大写
     */
    public static void main(String[] args) {
        String str = "this is a text";

        System.out.println("---------1.将str中的单词单独获取出来---------");
        String[] arr = str.split(" ");
        for (String s : arr) {
            System.out.println(s);
        }

        System.out.println("---------2.将str中的text替换为practice---------");
        System.out.println(str.replace("text", "practice"));

        System.out.println("---------3.在text前面插入一个easy---------");
        System.out.println(str.replace("text", "easy text"));

        System.out.println("---------4.将每个单词的首字母改为大写---------");
        for (int i = 0; i < arr.length; i++) {
            char first = arr[i].charAt(0);
            // 把第一个字符转换成大写
            char upperfirst = Character.toUpperCase(first);
            String newstring = upperfirst + arr[i].substring(1);
            System.out.print(newstring + " ");
        }
    }
}
运行结果:
---------1.将str中的单词单独获取出来---------
this
is
a
text
---------2.将str中的text替换为practice---------
this is a practice
---------3.在text前面插入一个easy---------
this is a easy text
---------4.将每个单词的首字母改为大写---------
This Is A Text 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值