JAVA入门———Scanner类 AND String类

Scanner类

  • 作用:用于获取用户键盘录入的结果
  • 标准输入流:public static final InputStream in;
  • 常用Scanner的构造方法:Scanner( InputStream source) + InputStream:
    IO流——字节输入类型 (类名可直接打点调用静态成员变量)

想要创建对象,就看提供的构造方法,若有空参构造,则不用传参,若无空参构造,则new时必须传入参数

注意:

  1. new Scanner时要导入包——import java.util.Scanner;
  2. 若想连续输入相同或不同的数据,不需要创建多个对象,一个即可——nextLine例外
  3. 使用nextLine( )方法时:若先输入整数,再想输入字符串时必须创建一个新的对象
  4. 使用next( )方法录入字符串时: 录入的字符串中间有空格,空格后面的内容不录入
scanner.hasNextInt()——判断键盘输入数据的类型是否为int型
scanner.hasNextDouble() ——判断键盘输入数据的类型是否为double型
…NextBoolean() …NextLine() …NextShort() …

String类

  • String 类代表字符串 Java 程序中的所有字符串字面值(如 “abc” )都作为此类的实例实现
  • 字符串是常量 被创建后内容不能被改变,能变的是指向——因为字符串的值是在方法区的常量池中划分空间 分配地址值的
public String():空构造
 eg:    // String()
        String s1 = new String();
        s1 = "abc";
        System.out.println(s1.toString());
public String(String original):把字符串常量值转成字符串
eg:		// String(String original)
		String s = new String("abcdef");
        System.out.println(s.toString());
public String(byte[] bytes):把字节数组转成字符串
eg:	byte[] b = {97,98,99,100,101,102,103};
        String s = new String(b);
        System.out.println(s);
public String(byte[] bytes,int index,int length):把字节数组的一部分转成字符串(index:表示的是从第几个索引开始, length表示的是长度)
eg:	byte[] b = {97,98,99,100,101,102,103};
 		String s1 = new String(b,1,4);
        System.out.println(s1);——————————结果输出:bcde
public String(char[] value):把字符数组转成字符串
eg:		char[] arr = {'没','有','秘','密','的','你'};
        String str = new String(arr);
        System.out.println(str);
public String(char[] value,int index,int count):把字符数组的一部分转成字符串
eg:		char[] arr = {'没','有','秘','密','的','你'};
		String s = new String(arr, 2, 2);
        System.out.println(s);——————————结果输出:秘密 
public int length():返回此字符串的长度
eg: 	String s = "没有秘密的你";
        System.out.println(s.length());——————————结果输出:6
public boolean equals(Object obj):比较字符串的内容是否相同,区分大小写
eg:	boolean b1 = "zy".equals("zy");
        System.out.println(b1); ——————————结果为:true
public boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写
eg:	boolean b2 = "Zy".equalsIgnoreCase("ZY");
        System.out.println(b2);——————————结果为:true
public boolean contains(String str):判断字符串中是否包含传递进来的字符串
eg:		boolean b3 = "亲爱的热爱的".contains("爱");
        System.out.println(b3);——————————结果为:true
public boolean startsWith(String str):判断字符串是否以传递进来的字符串开头
eg:		boolean b4 = "没有秘密的你".startsWith("没");
        System.out.println(b4);
public boolean endsWith(String str):判断字符串是否以传递进来的字符串结尾
eg:	boolean b5 = "没有秘密的你".endsWith("你");
        System.out.println(b5);
public boolean isEmpty():判断字符串的内容是否为空串""。
eg: 	boolean b6 = "".isEmpty();			————————方法一
        System.out.println(b6);
        boolean b7 = "".length() == 0;		————————方法二
        System.out.println(b7);
public char charAt(int index):获取指定索引位置的字符
eg:	char ch = "我爱学习学习爱我".charAt(6);
        System.out.println(ch);———————————— 爱

遍历字符串

eg:	    for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            System.out.println(c);
        }
public int indexOf(int ch):返回指定字符在此字符串中第一次出现处的索引
eg:	int zifu = "没有秘密的你".indexOf('的');
        System.out.println(zifu);———————————— 4

        zifu = "亲爱的热爱的".indexOf('我');
        System.out.println(zifu);———————————— -1

如果查询不到索引则返回-1

public int indexOf(String str):返回指定字符串在此字符串中第一次出现处的索引。
eg:	int zifuchuan = "烈火英雄真好看".indexOf("英雄");
        System.out.println(zifuchuan);———————————— 2

输出字符串第一次出现时的第一个字符的索引

public int indexOf(int ch,int fromIndex):返回指定字符在此字符串中从指定位置后第一次出现处的索引。
eg:	int c1 = "好好学习天天向上所以我们要好好学习".indexOf("学",3);
        System.out.println(c1);—————————————— 15
public int indexOf(String str,int fromIndex):返回指定字符串在此字符串中从指定位置后第一次出现处的索引。
eg:	int c2 = "老九门分为上三门和平三门还有下三门".indexOf("三门",7);
        System.out.println(c2);———————————— 10
public int lastIndexOf(String str,int fromIndex):返回指定字符在此字符串中从指定位置后从后往前第一次出现处的索引。
eg:	int c3 ="假如我年少有为不自卑我懂得什么是我的珍贵".lastIndexOf("我");
        System.out.println(c3);—————————————— 16
public String substring(int start):从指定位置开始截取字符串,默认到末尾。
eg:	String s = "我有一碗酒那碗酒赠吾兄";
        String s1 = s.substring(5);
        //String s1 = s.substring(s.indexOf('那'));————————含头
        System.out.println(s1);————————————那碗酒赠吾兄
public String substring(int start,int end):从指定位置开始到指定位置结束截取字符串。
eg:	String str = "你是我未曾拥有无法捕捉的亲昵";
        String st = str.substring(7,11);
        //String st = str.substring(str.indexOf('无'),str.indexOf('的'));————————含头不含尾
        System.out.println(st);—————————————— 无法捕捉
public byte[] getBytes():把字符串转换为字节数组。
eg:	byte[] b = "abcdefg".getBytes();
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i]);    ——————————输出:97 98 99 100 101 102 103
        }

把字节数组转换为字符串
String c = new String(b, 0, b.length);
System.out.println( c );

public char[] toCharArray():把字符串转换为字符数组。
public static String valueOf(char[] chs):把字符数组转成字符串。
eg:	String s3 = "有一种力量无人能抵挡";
        char arr[] = s3.toCharArray();——————————字符串转化为字符数组
        String s1 = new String(arr);——————————字符数组转化为字符串
        System.out.println(s1);
public static String valueOf(int i):把int类型的数据转成字符串。
	注意:String类的valueOf方法可以把任意类型的数据转成字符串。
public String toLowerCase(): 把字符串转成小写。
eg:	String s2 = "SJEFNWELBFI".toLowerCase();
        System.out.println(s2);——————————————————sjefnwelbfi
        char ch = 'A'+32;————————————另一种方法
        System.out.println(ch);
public String toUpperCase(): 把字符串转成大写。
eg:	String s = "lnfenf".toUpperCase();
        System.out.println(s);————————————————————LNFENF
        char sh = 'a'-32; ————————————另一种方法
        System.out.println(sh);
public String concat(String str):把字符串拼接。
eg:  String s = "sfsfeHJGKBKigiviviv";
      String s1 = s.substring(0,1).toUpperCase().concat(s.substring(1).toLowerCase());
      System.out.println(s1);
public String replace(char old,char new):将指定字符进行互换
eg:	String s = "那时候路很长,车马很慢,一生只够爱一人";
        String s1 = s.replace('那','古');
        System.out.println(s1);——————————————古时候路很长,车马很慢,一生只够爱一人
public String replace(String old,String new):将指定字符串进行互换
eg:	String s = "那时候路很长,车马很慢,一生只够爱一人";
		String s2 = s.replace("那时候","古代");
        System.out.println(s2);——————————————古代路很长,车马很慢,一生只够爱一人
eg:		 String s3 = s.replace("那时候","古时候").replace('够','能');————————链式编程
        System.out.println(s3);——————————————古时候路很长,车马很慢,一生只能爱一人
public String trim():去除两端空格
 eg:	String c = "    来天堂的魔鬼   ";
        String s4 = c.trim();
        System.out.println(s4);——————————————来自天堂的魔鬼
public int compareTo(String str):会对照ASCII 码表 从第一个字母进行减法运算 返回的就是这个减法的结果
  • 如果前面几个字母一样 会根据两个字符串的长度进行减法运算返回的就是这个减法的结果
  • 如果整个字符串一摸一样 返回0
eg:	String s = "DongXiaoJie";
        String s2 = "DongXiaojie";
        int num = s2.compareTo(s);
        System.out.println(num);———————————— 32
public int compareToIgnoreCase(String str):忽略大小写的比较
eg:	String s = "DongXiaoJie";
        String s2 = "DongXiaojie";
		int num2 = s2.compareToIgnoreCase(s);
        System.out.println(num2);———————————— 0

常见问题

eg:    String s1 = "没有秘密的你";
        s1 = "亲爱的"+"热爱的";
        System.out.println(s1);——————————结果输出:亲爱的热爱的

s1的内容没有改变,而是指向地址改变

eg:	String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);——————————false
        System.out.println(s1.equals(s2));——————————true
        
        String s5 = "hello";
        String s6 = "hello";
        System.out.println(s5 == s6);——————————true
        System.out.println(s5.equals(s6));——————————true

        String s3 = new String("hello");
        String s4 = "hello";
        System.out.println(s3 == s4);——————————false
        System.out.println(s3.equals(s4));——————————true

String 重写了Object类的equal() 比较的是,两个字符串字面上的内容是否相同
== :判断地址是否相同 —————— equals( ):判断内容是否相同

 eg:    String s1 = "Hello";
        String s2 = "Hello";
        String s3 = "Hel" + "lo";——————编译期能确定结果为:hello
        String s4 = "Hel" + new String("lo");————————编译期不能确定地址值
        String s5 = new String("Hello");
        String s6 = s5.intern();
        String s7 = "H";
        String s8 = "ello";
        String s9 = s7 + s8;——————用两个变量拼成,编译期不能确定变量的地址值
        System.out.println(s1 == s2); ——————————true——地址相同
        System.out.println(s1 == s3); ——————————true——地址相同
        System.out.println(s1 == s4);——————————false——地址不相同 
        System.out.println(s1 == s9);——————————false——地址不相同
        System.out.println(s4 == s5);——————————false——地址不相同 
        System.out.println(s1 == s6); ——————————true——地址相同
        System.out.println(s5 == s6);——————————false——地址不相同 
    }

s1 == s2 : 先去常量池中,看有没有该字符串,如果没有就创建,如果有就把之前的地址值赋值过来
s5.intern()方法:取出调用者的地址值

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值