引用类型:默认值为null;
1. 一些常用的基础方法:
public String(byte[] bytes,int index,int length);将字节数组的一部分转换成字符串
public String(char[] value);将字符数组转换成字符串 将字符串转化为字符数组 toCharArray
public String(char[] value,int index,int length);将字符数组的一部分转换成字符串
public String(String original) 将一个字符串常量构造成字符串
public int length()返回此字符串的长度
面试题:(重点)
数组,字符串,集合中有没有length();
数组中没有length方法,属于属性,带括号的叫方法
字符串中有 length()
集合里面没有length(), 获取集合的元素数:size()
注释:只有字符串里面有length(),其他都是length属性;
public class Student { //构造一个空字符串 public static void main(String[] args) { String s=new String (); System.out.println(s);//结果是一个空字符,说明这个类重写的是一个toString() //说明String类本身就重写了toString()重写之后就不是地址值了 System.out.println("---------------------"); byte[] bys= {97,98,99,100,101};//字节数组 String s2=new String(bys); System.out.println(s2); System.out.println("------------------"); String s3=new String(bys,1,2); System.out.println(s3); System.out.println("------------"); char[] ch= {'我','爱','你'}; String s4=new String(ch); System.out.println(s4); System.out.println("-----------"); String s5=new String(ch,1,2); System.out.println(s5); String s6=new String("abc"); String s7="abc"; String s8=new String(""); String s9=null; System.out.println(s6); System.out.println(s7); System.out.println(s8); System.out.println(s9); } }字符串最大的特点:字符串一旦被赋值,其值不能发生改变
面试题:
String s1=new String("abc");
String s1="abc"
这两个分别创建了几个对象
第一个创建了两个: 方法区以及堆内存
第二个创建了一个: 只在方法区
举例:
public class StringDemo { public static void main(String[] args) { String s ="hello"; s+="world"; System.out.println(s); System.out.println("--------"); String s1="abc"; String s2="world"; //String 做形式参数,和基本类型是一样的,对实际参数没有影响,形式参数的改变没有影响 //StringBuffer:字符串缓冲区 System.out.println("s1:"+s1+",s2:"+s2); change(s1,s1); System.out.println("s1:"+s1+",s2:"+s2); } public static void change(String s1,String s2) { s2=s1+"java"; s2=s1+"hello"; } }
equals:默认比较的是地址值,String 本身就重写了equals(),比较的是内容是否相同
public class StringDemo2 { public static void main(String[] args) { String s1=new String("abc"); String s2="abc"; System.out.println(s1==s2); System.out.println(s1.equals(s2)); } }
注释:结果会是true,这个必须在源代码中观看。
2.String一些常用的判断方法:
boolean equals(Object obj);将字符串与指定的对象相比较
boolean equalsIgnoreCase(String str);将此String与另一个String相比较,不考虑大小写
boolean contains(String str);判断当前大字符串中是否包含子字符串。(重点)
boolean startsWith(String str);以str字符串开头(重点)
boolean endswith(String str);以str字符串结尾(重点)
boolean isEmpty();判断字符串是否为空
boolean concat();字符串的特有功能,吧、拼接功能和+拼接复功能一样
举例:
public class StringDemo3 { public static void main(String[] args) { String s1="helloworld"; String s2="world"; String s3="HELLOWORLD"; String s4="java"; String s5="l love"; String s6=""; //boolean equals(Object obj);将字符串与指定的对象相比较 System.out.println(s1.equals(s2)); System.out.println("-----------------------"); // boolean equalsIgnoreCase(String str);将此String与另一个String相比较,不考虑大小写 System.out.println(s1.equalsIgnoreCase(s3)); System.out.println("------------------------"); //boolean contains(String str);判断当前大字符串中是否包含子字符串 System.out.println(s1.contains(s2)); System.out.println("----------------------"); //boolean endswith(String str);以str字符串结尾 System.out.println(s1.startsWith(s2)); System.out.println(s1.endsWith(s2)); System.out.println("------------------------"); //boolean concat();字符串的特有功能,吧、拼接功能和+拼接复功能一样 System.out.println(s5.concat(s4)); System.out.println("-----------------------"); // boolean isEmpty();判断字符串是否为空 System.out.println(s1.isEmpty()); System.out.println(s6.isEmpty()); } }
3.String中一些常用的操作方法:
public int length() 返回此字符串的长度。
public char charAt(int index) 返回指定索引处的 char 值
public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的索引
public int indexOf(int ch,int fromIndex)
返回在此字符串中第一次出现指定字符处的索引,从指定的索引开始搜索。
public int indexOf(String str)返回指定子字符串在此字符串中第一次出现处的索引
public int indexOf(String str,int fromIndex)
返回指定子字符串在此字符串中第一次出现处的索引,从指定的索引开始。
public String substring(int beginIndex)截取字符串,默认从此字符串到末尾。
public String substring(int beginIndex,int endIndex)截取字符串,默认从指定开始到指定结尾。
举例:
public class StringDemo4 { public static void main(String[] args) { String s="StringBuffer"; System.out.println(s.length()); System.out.println("----------------"); System.out.println(s.charAt(5)); System.out.println("----------------"); System.out.println(s.indexOf('f')); System.out.println("----------------"); System.out.println(s.indexOf('u', 5)); System.out.println(s.indexOf('u', 9)); System.out.println("----------------"); System.out.println(s.indexOf("u")); System.out.println("----------------"); System.out.println(s.substring(5)); System.out.println(s.substring(6, 7)); } }
通过以上的练习与掌握,基本可以写一个猜数字小游戏。
import java.util.Scanner; /**要打开游戏必须要有自己的用户名及密码,三次机会 * 进去后才能进行游戏 * @author 田伟 * */ public class 登录模拟 { public static void main(String[] args) { String usename="wt5264"; String usepass="wt521"; int count=3; for(int i=0;i<3;i++) { count--; Scanner sc=new Scanner(System.in); System.out.print("请输入您的用户名:"); String useName=sc.nextLine(); System.out.print("请输入您的密码:"); String usePass=sc.nextLine(); if(useName.equals(usename)&&usePass.equals(usepass)) { System.out.println("恭喜您登录成功"); 游戏中心.game(); break; } else if(count>0){ System.out.println("对不起,您输入的秘密有误"); System.out.println("还有"+count+"次机会,请重新输入"); } else { System.out.println("对你起,你的机会已经用完,请联系我们的管理员,电话15191309274"); } } } }
import java.util.Scanner; /**首先我要写一个猜数字游戏,那就必须是在一定的区域,然后用猜的数和实际数字进行对比,提示。 * 因此java印出来一个关键字 * public static double random()返回带正号的 double 值,该值大于等于 0.0 * 且小于 1.0。返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。 * 一般情况况下包前不包后。 * @author 田伟 * */ public class 游戏中心 { public static void game() { System.out.println("游戏开始"); int i=(int) (Math.random()*100); int sum=0; while(true) { sum++; Scanner sc=new Scanner(System.in); System.out.println("请输入您要猜的数"); int num=sc.nextInt(); if(num>99||num<0) { System.out.println("你猜的数字不符合要求"); } else if(num>i) { System.out.println("您猜的数字有点大,请继续再猜"); }else if(num<i) { System.out.println("您猜的数字有点小,请继续再猜"); }else { System.out.println("恭喜您,猜对了!"); System.out.println("您一共猜了"+sum+"次"); break; } } } }