2020.7.28-String类的方法

01_String类的概述

01.1_定义

  • 就是由一个或多个字符组成的序列。字符串可以看成是字符数组,每一个字符从左往后编有索引,从0开始。字符串是由多个字符组成的一串数据(字符序列)
    字符串可以看成是字符数组

01.2_String类的构造方法

常见构造方法
	public String():空构造
	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):把字符数组的一部分转成字符串
	public String(String original):把字符串常量值转成字符串
B:案例演示	
	演示String类的常见构造方法
	演示前先说一个字符串的方法:
	public int length():返回此字符串的长度。

01.2举例解释字符串/字符数组

package com.west.Demo3;
public class StringTest {
    public static void main(String[] args) {
        //String 字符串:就是有一个或多个字符组成的序列。
        //字符串可以看成是字符数组,每一个字符从左往后编有索引,从0开始

        //String 类代表字符串。
        //1. Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
        //2. 字符串是常量;它们的值在创建之后不能更改。

        // String() 初始化一个新创建的 String 对象,使其表示一个空字符序列。

       /* String s = new String();
        //String 类他重写了父类的toString方法,打印的是字符串字面值
        System.out.println(s);
        //"" 空串
        System.out.println("");

        System.out.println("=====================================");
        java.lang.String s1 = new java.lang.String("abc");
        System.out.println(s1);
*/
       //把字符串看成索引
        //public String(byte[] bytes);
        //定义一个字符串数组
        byte[] by={99,96,95,47,-23,24,23};
        String str=new String(by);
        System.out.println(str);//c`_/�会出现乱码情况
       //public String(byte[] bytes,int index,int length):
        // 把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
        String str1=new String(by,0,4);
        System.out.println(str1);//c`_/
        //以下这种情况会出现索引越界
        String s = new String(by,4,4);
        System.out.println(s);//StringIndexOutOfBoundsException
    }
}

01.3字符串与字符数组转换

package com.west.Demo3;

public class StringTest01 {
    public static void main(String[] args) {
        //public String(char[] value):把字符数组转成字符串
        /*char[] value={'a','b','c','d','詹','姆','斯'};
        String s = new String(value);
        System.out.println(s);//abcd詹姆斯
        for (int i = 0; i < value.length; i++) {
            s+=value;
        }
        System.out.println(s);//abcd詹姆斯[C@4554617c[C@4554617c[C@4554617c[C@4554617c[C@4554617c[C@4554617c[C@4554617c
        System.out.println("-----------------------------------");
        //public String(char[] value,int index,int count):把字符数组的一部分转成字符串
        String s1 = new String(value,4,3);
        System.out.println(s1);//詹姆斯*/
        System.out.println("-----------------------------------");
        //public String(String original):把字符串常量值转成字符串
        int length="abc".length();
        System.out.println(length);//3
        String s2 = new String("abc");
        int len=s2.length();
        System.out.println(len);//3
        System.out.println("---------------------");
    }
}

01.4_String的特点: 一旦被创建就不能改变 因为字符串的值是在堆内存的

String s3 = new String("hello");
         s3="world"+"   dudu";
         //在堆内存常量池开辟了两个空间,分别有两个地址,第二个地址将第一个地址覆盖。(内容不能变,引用可以变)
        System.out.println(s3);//world   dudu
        //两个对象指的是一个常量池里的同一空间
        String s4 = new String("hello");
        String s5="hello";
        System.out.println(s4==s5);//false

01.5_String类的判断功能

package com.west.Demo3;
//String类的判断功能
public class StringTest02 {
    public static void main(String[] args) {
        //创建一个字符串
        String s = new String("abc");
        String s1 = new String("Abc");
        String s2="abc";
        String s3="dududu";
       String s4="d";
        //1、public boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
        System.out.println(s.equals(s1));//false
        //2、public boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
        System.out.println(s.equalsIgnoreCase(s1));//true
        //3、public boolean contains(String str):判断字符串中是否包含传递进来的字符串
        System.out.println(s3.contains(s4));//true
        //4、public boolean startsWith(String str):判断字符串是否以传递进来的字符串开头
        System.out.println(s3.startsWith(s4));//true
        //5、public boolean endsWith(String str):判断字符串是否以传递进来的字符串结尾
        System.out.println(s3.endsWith(s4));//false
        //6、public boolean isEmpty():判断字符串的内容是否为空串""。
        System.out.println(s4.isEmpty());//false

01.6_模拟用户登录

package com.west.Demo3;

import java.util.Scanner;

/* A:
        案例演示:
        需求:模拟登录, 给三次机会, 并提示还有几次。*/
public class StringTest03 {
    public static void main(String[] args) {
        String name = "long";
        String password = "123456";
        Scanner sc = new Scanner(System.in);
        for (int i = 1; i <=3; i++) {

            System.out.println("第" + i + "次输入,共三次机会");
            System.out.println("请输入用户名");
            String nam = sc.nextLine();
            System.out.println("请输入密码");
            String n = sc.nextLine();
            while(i<=3)
            if (nam.equals(name) && n.equals(password)) {
                System.out.println("输入正确");
                break;
            } else {
                /*if (3 - i > 0)
                    System.out.println("输入错误请重新输入");
                else {*/
                    System.out.println("请重新输入");
                    break;
                }

            }
        }
    }

01.7_String类的获取功能及String类的转换功能)

package com.west.Demo3;

//String类的转换功能
public class StringTest04 {
    public static void main(String[] args) {
        String[] str={"dudu","dada","1","你好"};
        //int a=12;
        //String s1=new String(String.valueOf(str));
        String s = new String("嘟嘟嘟");
        //1,public byte[] getBytes():把字符串转换为字节数组。
        /*byte[] by=s.getBytes();
        for (int i = 0; i < s.length(); i++) {
            System.out.println(by[i]);//-27、-104、-97
        }*/
        //2,public char[] toCharArray():把字符串转换为字符数组。
        char[] c=s.toCharArray();//调用方法
        /*char[] chars=new char[s.length()];//创建接收数组的对象
        for (int j = 0; j < s.length(); j++) {
            chars[j]=s.charAt(j);
           // System.out.println(chars[j]);
        }*/
        String s1 = new String(c);
        System.out.println(s1);
        System.out.println("------------------------------------------");
        //拼接空串
        int num=66;
        String s2=num+"";
        System.out.println(s2);//66
        System.out.println(s.toCharArray());//abc
        //3,public static String valueOf(char[] chs):把字符数组转成字符串。
        String s3=String.valueOf(100);
        System.out.println(s3);//100
        String s4 = String.valueOf(false);
        System.out.println(s4);//false
        //System.out.println(String.valueOf(str));
        //4,public static String valueOf(int i):把int类型的数据转成字符串。
        System.out.println();
        //拼接字符串
        String s5="asd".concat("123").concat("didud");
        System.out.println(s5);

    }
}

01.8_拼接字符串

package com.west.Demo3;

public class StringTest07 {
    public static void main(String[] args) {
        /*String str = "我爱你";
        byte[] bytes = str.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            System.out.println(bytes[i]);
        }
        System.out.println("==========================");
        String s = new String(bytes);
        System.out.println(s);

        System.out.println("================================");*/
        String str2 = "我爱你";
        //把一个字符串转换成字符数组
       // char[] chars = str2.toCharArray();
        char[] chars = new char[str2.length()];
        for (int i = 0; i < str2.length(); i++) {
            chars[i] = str2.charAt(i);
        }

        String s1 = new String(chars);
        System.out.println(s1);

        String s2 = "abc".toUpperCase();
        String s3 = "BcD".toLowerCase();

        System.out.println("=======================================");
      /*  int num=100;  //"100"
        String str3=num+""; //拼接空串
        System.out.println(str3);
        boolean b=true; //"true"*/
        //valueOf(100); 把很多种类型转换成字符串
        String s4 = String.valueOf(100);
        String s5 = String.valueOf(false);

        char[] chars1={'a','b','c'};
        String s6 = new String(chars1);
        String s7 = String.valueOf(chars);
        //拼接字符串。
        String abc = "abc".concat("ABC").concat("dddd").concat("555555");
        System.out.println(abc);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有点。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值