2022-07-20 第二组-于凤琳 String类与包装器类型学习笔记

一,String类

1.String字符串,是一个类同时也是一个引用数据类型。

类中可以存在哪些结构1.属性2.方法3.构造器

所以String这个类中也有属性,方法和构造器。

2.String一旦声明就不可改变!(在内存的角度来看)

 (一),String类的对象

  •  形式1,String 既然是 一个类,s1应该叫做String类的一个对象。

              类如果要创建对象,得用new, 但是s1并没有new,s1也可以叫做对象。

             s1被所赋的值为字面量(常量) 存放在方法区(常量池中)

 String s1="abcdefg";
  • 形式2,s2通过new创建出来的String类的对象。创建对象是要调用构造器
String s2=new String ("abcdefg");
  •  区分:s1指向的abcdefgs2指向的abcdefg不在同一个区域内

 (二),虚地址

  • 双等号比较的是虚地址,虚地址:对象在内存当中的存储位置。

  •         //返回结果为false
            System.out.println(s1 == s2);
            //要永远记住new就代表新建
            //返回结果为false
            System.out.println(s2 == s3);
            //返回结果为true
            System.out.println(s1 == s4);
            /*
            = 表示赋值 无论怎么比较,都是true
            new赋值,用双等号比较,就是false
             */

    (三),String类的方法

方法1:比较字符串的内容(与地址没有关系)

equals()方法:
1.需要传参(不传报错),传String类型的参数。
2.该方法有返回值 返回值为boolean类型。
3.访问权限public(在这个工程里都好使)。

 

package com.jsoft.morning;

import java.util.Scanner;
//String类的方法(自带的 拿过来就行)
public class Ch02 {
    public static void main(String[] args) {
        String s1="a";
        String s2="a";
       Scanner sc= new Scanner(System.in);
        System.out.println("请输入字符串1:");
        String s3 = sc.next();
        System.out.println("请输入字符串2:");
        String s4 = sc.next();
//        System.out.println(s3 == s4);
        System.out.println(s3.equals(s4));

    }
}

==和equals的区别

== 是地址比较       equals是值比较

方法2:获取字符串的长度

length()方法;                                           字符串是字符拼成的串 字符串长度就是字符的个数
1.不需要传参
2.有返回值(一定是整型int)                     
3.访问权限是public

 面试题:
        字符串获取长度的方法数组获取长度有什么区别?
        1.字符串获取长度调用length()方法
        2.数组获取长度是用的length属性

package com.jsoft.morning;
//String 字符串同时也是类 要是类的话就会有方法
public class Ch03 {
    public static void main(String[] args) {

        //字符串是字符拼成的串 字符串长度就是字符的个数
        //下标(索引)一定从0开始      
        String s1="abcdefg";
        System.out.println(s1.length());
        int[] arr=new int[10];
        System.out.println(arr.length);
    }
}

 

方法3:取出指定下标位置的字符

charAt( 下标 )方法        下标一定是从0开始

package com.jsoft.morning;

import java.util.Scanner;

public class Ch04 {
    public static void main(String[] args) {
        String s1= "abcdefg";
        //3.取出指定下标位置的字符
        System.out.println(s1.charAt(0));
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        char c = sc.next().charAt(0);
        //控制台的输出结果为 98    
        System.out.println(c+1);  ASCII码值


    }

}

方法4:判断指定字符串是否存在,返回值为字符串在s1的下标

indexof(String,int)——判断要查询的String字符串在是否存在于s1字符串当中。

 代表从int(下标)位置开始查找,包括当前位置。会返回从左到右第一个匹配的下标。如果不存在 返回-1。

lastIndexOf("a",12)代表从后往前找,在s1字符串当中下标为12的位置,以这为起点往前查找“a”字符。

package com.jsoft.morning;

public class Ch05 {
    public static void main(String[] args) {
        String s1 ="abcdefgabcdaaa";
        //长度为14 下标到13
//        System.out.println(s1.indexOf("abc",0));
        //从后往前找
        System.out.println(s1.lastIndexOf("a",12));
    }
}

方法5:字符串的截取

字符串的截取 
        如果传一个参数,从指定位置开始截取,直到字符串的末尾
        考虑一下?包不包括1的位置 包括起始位置的字符 会生成一个新的字符传 不会改变原有字符串。
        

package com.jsoft.morning;

public class Ch06 {
    public static void main(String[] args) {
        String str="abcdefgabcaaa";
        String s =str.substring(1);
        System.out.println(s);
        //不包含最后一个位置的字符
        String s1=str.substring(1,2);
        System.out.println(s1);
    }
}

方法6:改变字符串的大小写

package com.jsoft.morning;

import java.util.Arrays;

public class Ch07 {

    public static void main(String[] args) {
        String str ="abcdefg";
        String str1="     ABCDefg     ";
        String str2="123,234,345";

        /*
        下面所有的方法都是返回一个新的字符串
         */
        //转大写
        System.out.println(str.toUpperCase());
        //转小写
        System.out.println(str.toUpperCase());
        //判断字符串是否以***开头
        System.out.println(str.startsWith("a",2));
        //判断字符串是否以***结尾
        System.out.println(str.endsWith("f"));
        //忽略大小写的值进行比较内容
        //验证码的
        System.out.println(str.equalsIgnoreCase(str1));
        //去掉字符串前后的空格
        System.out.println(str1.trim());
        //根据指定的字符分割,分割之后,分割条件是消失
        String []  strings=str2.split(",");
//        System.out.println(Arrays.toString(strings));
        for(String string :strings ){
            System.out.println(string);
        }


    }
}

方法7:字符串的替换

package com.jsoft.afternoon;

public class Ch01 {
    public static void main(String[] args) {
        //字符串的替换
        String str="hello";
        System.out.println(str.replace('e', 'c'));
        str.replaceAll("ello","i");

    }

}

方法8:字符串和数组(数据结构——线性表)的转换

package com.jsoft.afternoon;

public class Ch02 {
    public static void main(String[] args) {
        //数组转字符串(自己练)
        // int [] arr=new int[]{1,2,3,4,5};
        // 1.字符串和数组(数据结构——线性表)的转换  //引用数据类型 第一类型是数组  第二类型是类   //Arrays 是数组的工具类
       //·转成字符型的数组
        String str="abcdefg";
//       char[] array= str.toCharArray();
//       for(char c:array){
//           System.out.println(c);
//       }
        //·转成字节型的数组
        //一般情况下,字节型数组在操作文件的时候常用的IO流
        byte [] array= str.getBytes();
        for(byte c: array){
            System.out.println(c);
        }


        //2.字符串和其他数据类型的转换

         int i=10;
        //把int类型转变为字符串
        String s= String.valueOf(i);
        //任何数据类型和字符串类型做加法,结果都是字符串
//        String s= i+" ";//空串(字面量)要与null区别//null 空    不推荐使用
        System.out.println(s);

    }
}

1.转成字节型的数组

2.转成字符型的数组

方法9:字符串和其他数据类型的转换

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值