请先查看前置知识:
【JAVA】基础1-字符串、堆、栈、静态与引用类型
https://www.cnblogs.com/remyuu/p/15990274.html
import java.util.Scanner;
//notepad++ 的快速注释 ctrl + k 取消时ctrl + q
/*
代码学习知识点清单
1-Scanner的使用
2-String的两种创建方法
3-使用Scanner规则搜索关键字
4-字符串-获取长度
5-字符串-获取特定关键字索引
6-字符串-根据索引获取关键字
7-字符串-截取
8-字符串-查询关键字
9-字符串-构造方法与直接赋值的原理
10-字符串-比较字符串大小
11-字符串-判断是否为空字符串
12-字符串-分割文本!
13-字符串-大小写,空格处理
*/
public class Learn01{
public static void main(String[] args){
System.out.println("-------------------------------");
System.out.println("------------Learn01-----------");
System.out.println("-------------------------------");
// Scanner s = new Scanner(System.in);
// int num1 = s.nextInt();
// System.out.println("请用户再输入一个");
// int num2 = s.nextInt();
// System.out.println("sum="+(num1+num2));
//s.close();//将构造方法关闭
// //也可以用nextFloat,nextDouble
//字符串变量创建两种方法
String str1 = "我是一个string1";//一般方法
String str2 = new String("我是第二个"); //构造方法
System.out.println(str1 + str2);
//String使用Scanner方法进行过滤
String input = "1 fish 2 fish red fish blue fish";
Scanner ss = new Scanner(input).useDelimiter("\\s*fish\\s*");
//Scanner方法给对象构造方法(new)的时候,方法需要传入数据源,
//即Scanner( ... ),可以再添加筛选器,分割文本!
//筛选的格式 \\s*...\\s*
System.out.println(ss.nextInt());
System.out.println(ss.nextInt());
System.out.println(ss.next());
System.out.println(ss.next());
ss.close();
//练习使用Scanner筛选器分割文本
String TestScannerString = "1 2 3 4 3 1 2 3 3 4 3 2";
Scanner MyScanner = new Scanner(TestScannerString).useDelimiter("\\s* \\s*");
for (int i=0;i<2;i++)
System.out.println(i + ":" +MyScanner.next());
/*关于字符串的知识点
字符串不能够被修改,string与其他基本类型不同(比如int等),
string在变量区储存的是字符串在常量区的地址,重新赋值的话
只会改变其的指向的内存地址*/
//字符串的操作 "1 2 3 4 3 1 2 3 3 4 3 2"
///1,length(),取得字符串的长度
System.out.println("上面字符串长度"+TestScannerString.length());
System.out.println("-------------------------------");
///2-indexOf(..)根据字符查找位置
//2-1indexOf(String str),查找字符串子串从前往后 注:索引从0开始哦
System.out.println(TestScannerString.indexOf("4"));//输出6
//2-2,lastIndexOf(String str),从后往前查找子串
System.out.println(TestScannerString.lastIndexOf("4"));//输出18
//如果查找不到 返回-1
//2-3,indexOf(int i),还可以传入ASCII/字符
System.out.println(TestScannerString.lastIndexOf(49));//49是1 输出10
//2-4,lastIndexOf(int i)同理,不赘述。
System.out.println("-------------------------------");
///3-charAt(int index)根据位置获取字符
System.out.println(TestScannerString.charAt(0));
//索引不能超出范围,最大索引是长度-1
System.out.println("-------------------------------");
///4-substring(...)截取字串
//4-1,substring(int beginIndex),从指定位置(包括)往后截取
System.out.println(TestScannerString.substring(3));
//4-2,substring(int beginIndex,int endIndex),从指定位置(包括)截取到指定位置(不包括)
System.out.println(TestScannerString.substring(3,6));//强调,不包括结束索引
System.out.println("-------------------------------");
String strTest = new String("www.pornhub.com");//方法构造,会new在堆中,再引用字符常量
String strTestDirect = "www.pornhub.com";//在栈中直接引用字符常量的地址,没有中间商
///5-判断字符串
//5-1,startsWith(String str)判断字符串是否以...开头
System.out.println(strTest.startsWith("www.")); //输出true
//5-2,endsWith(String str)判断字符串是否以...结尾
System.out.println(strTest.endsWith("www.")); //输出false
//5-3,equals(String str)判断字符串是否相等
System.out.println(strTest.equals("www.pornhub.com")); //输出true
//5-4, == 比较字符串的引用地址
System.out.println(strTest == "www.pornhub.com"); //输出false
System.out.println(strTestDirect == "www.pornhub.com"); //输出true
/*字符串构造方法的本质 !!重点!!
构造方法和直接赋值在内存的存储方式不一样
内存有四个区:栈 堆 方法区 运行常量区
//字符串("www.pornhub.com")会存储在方法区的运行常量区
//以下直接赋值的方式现在栈中创建str1变量,存储一个地址,指向常量区
//str1和str2都是指向同一个地址
String str1 = "www.pornhub.com";
String str2 = "www.pornhub.com";
String tr3 = new String("www.pornhub.com");
//但是需要注意的是,通过new出来的对象是存储在堆里面的,而不是常量区当中
//换而言之,方法中的"www.pornhub.com"会在运行常量区复制到堆中的那个新的对象中
//总的来说,栈中的变量指向堆,堆中的对象再指向运行常量区,相当于中间商
//另外一点,如果初始化对象赋值为null,则只能索引对象(栈),不能索引对象在堆中的引用。
然后需要区别 == 和 equals
==是比较引用 equal比较的是字符不管你怎么引用
*/
//5-5,equalsIgnoreCase(String str),忽略大小写地判断字符串是否相等
System.out.println(strTest.equalsIgnoreCase("wWw.PoRnHuB.CoM"));//输出true
//5-6,compareTo(String str),比较字符串大小
//挨个字符比较,遇到不同的字符就返回ascii差值,
//若前面字符相等长度不等则返回长度差 ascii的计算:主体-客体
//作用:一般用于字母的排序中
System.out.println(strTest.compareTo("www.pornhub.com"));//输出0
System.out.println(strTest.compareTo("www.pornhub")); //输出4
System.out.println(strTest.compareTo("xww.pornhub.com"));//输出-1
//5-7,contains(String str),判断是否包含字串...,不管有多少个
System.out.println(strTest.contains("www.")); //输出true
//5-8,isEmpty(),判断是否为空字符串
System.out.println(strTest.isEmpty()); //输出false
String testStringEmpty = "";
System.out.println(testStringEmpty.isEmpty()); //输出true
testStringEmpty = " ";
System.out.println(testStringEmpty.isEmpty()); //输出false
//如果是赋值null,是不可以操作的。
System.out.println("-------------------------------");
///6-字符串转换操作
//6-1,toLowerCase(),转小写
System.out.println(strTest.toLowerCase());
//6-2,toUpperCase(),转大写
System.out.println(strTest.toUpperCase());
//6-3,split(...),字符串分割,用字符串数组来接收
//6-3-1,split(String str)
String[] strs = strTest.split("\\.");//非常特殊!!!
System.out.println(strs); //输出了地址
for(String temps : strs)
System.out.println(temps);
//6-3-2,split(String str,int limit)
//6-4,trim(),剔除前后的空格,中间不剔除
strTest = " www.pornhub.com ";
System.out.println(strTest = strTest.trim()); //输出了www.pornhub.com
//6-5,replace(,),可以替换字符或文本(只能字符替换字符,文本替换文本,包括空字符)
System.out.println(strTest = strTest.replace("www","i love"));//输出了i love.pornhub.com
System.out.println(strTest = strTest.replace('i','u')); //输出了u love.pornhub.com
}
}
浙公网安备 33010602011771号