packagecom.jacyzhu.string;publicclassDemo01{publicstaticvoidmain(String[] args){String name ="hello";// "hello"常量存储在字符串池中
name ="zhangsan";// "zhangsan"赋值给name变量,给字符串赋值时,并没有修改数据,而是重新开辟一个空间String name2 ="zhangsan";}}
Strings = new String(“Hello”); // 产生两个对象,堆、池各存储一个。
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做拆分
packagecom.jacyzhu.string;importcom.sun.xml.internal.ws.api.ha.StickyFeature;importjava.util.Arrays;publicclassDemo02{publicstaticvoidmain(String[] args){System.out.println("-----------字符串方法的使用1----------");// 字符串方法的使用// 1.length():返回字符串的长度// 2.charAt(int index):根据下标获取字符// 3.contains(String str):判断当前字符串中是否包含strString content ="java是世界上最好的java编程语言,java真香";System.out.println(content.length());// 26System.out.println(content.charAt(0));// jSystem.out.println(content.charAt(content.length()-1));// 香System.out.println(content.contains("java"));// trueSystem.out.println(content.contains("php"));// falseSystem.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"));// 0System.out.println(content.indexOf("java",4));// 11System.out.println(content.lastIndexOf("java"));// 20System.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 WorldSystem.out.println(content2.toUpperCase());// HELLO WORLDSystem.out.println(content2.toLowerCase());// hello worldString filename ="hello.java";System.out.println(filename.endsWith(".java"));// trueSystem.out.println(filename.startsWith("hello"));// trueSystem.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));// falseSystem.out.println(s1.equalsIgnoreCase(s2));// true 忽略大小写String s3 ="abc";// 97 98 99String s4 ="xyzw";// 120 121 122System.out.println(s3.compareTo(s4));// -23 97-120String s5 ="abc";// 长度 3String s6 ="abcxyz";// 长度 6System.out.println(s5.compareTo(s6));// -3 3-6String s7 ="abc";String s8 ="abc";System.out.println(s7.compareTo(s8));// 0}}
运行结果:
-----------字符串方法的使用1----------26
j
香
truefalse-----------字符串方法的使用2----------
java是世界上最好的java编程语言,java真香
[j, a, v, a, 是, 世, 界, 上, 最, 好, 的, j, a, v, a, 编, 程, 语, 言, ,, j, a, v, a, 真, 香]01120-----------字符串方法的使用3----------
hello World
HELLO WORLD
hello world
truetrue-----------字符串方法的使用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
-------------补充-------------falsetrue-23-30
练习
需求:
已知String str ="this is a text";1.将str中的单词单独获取出来
2.将str中的text替换为practice
3.在text前面插入一个easy
4.将每个单词的首字母改为大写
packagecom.jacyzhu.string;publicclassDemo03{/*
需求:
已知String str = "this is a text";
1.将str中的单词单独获取出来
2.将str中的text替换为practice
3.在text前面插入一个easy
4.将每个单词的首字母改为大写
*/publicstaticvoidmain(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.将每个单词的首字母改为大写---------ThisIsAText