您现在的位置是:主页 > news > 学做网站游戏教程/石家庄网站建设案例
学做网站游戏教程/石家庄网站建设案例
admin2025/6/10 18:21:22【news】
简介学做网站游戏教程,石家庄网站建设案例,长沙网站开发湖南微联讯点靠谱,团员信息查询系统入口字符串 1.字符串定义 字符串:简单理解为:由一个签字将若干个字符串起来的串儿,叫字符串官方理解:字符串是由多个字符组成的一串数据(字符序列)字符串可以看成是字符数组 通过观察API发现: 1、String代表的是字符串。属…
字符串
1.字符串定义
字符串:简单理解为:由一个签字将若干个字符串起来的串儿,叫字符串官方理解:字符串是由多个字符组成的一串数据(字符序列)字符串可以看成是字符数组
通过观察API发现:
1、String代表的是字符串。属于java.lang包下,所以在使用的时候不需要导包
2、String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。(对象)
3、字符串不变; 它们的值在创建后不能被更改。
字符串是常量,一旦被赋值,字符串本身不能被修改。
2.String的构造方法(注意String构造方法是将字符或字节数组转换为字符串,而用String的valueOf方法是将数组转化为字符串,其中包括字符数组)
(1).public String()
public class StringDemo1 {public static void main(String[] args) {String s=new String("奥里给");System.out.println(s.toString());//String类中重//写toString()方法System.out.println(s.length());//如果字符串中没有//字符,返回0
(2).public String(byte[] bytes)根据一个字节数组创建出一个字符串对象
byte[] bytes={95,97,98,99,100};String s1=new String(bytes);System.out.println("bytes字节数组对应的字符串为:s1:"+s1);//注意这里对应的是Ascll码对应的字符串 结果:_abcdSystem.out.println(s1.length());
(3).public String(byte[] bytes,int offset,int length)将字节数组中的一部分转化成字符串
byte[] bytes1={'d','a','d','r','q','y','q'};String s2=new String(bytes1,2,3);System.out.println("bytes1字节数组的部分对应的字符串为:"+s2);//结果:drq
4.public String(char[] value) 将字符数组转成一个字符串
char[] chars=new char[]{'s','e','r','q',};String s3=new String(chars);System.out.println("chars字符数组对应的字符串为:"+s3);//chars字符数组对应的字符串为:serq
5.public String(char[] value,int index,int count)将字符数组的一部分转化为字符串
char[] chars1={'s','e','r','q','a','q','o','p'};String s4=new String(chars1,2,5);System.out.println("chars字符数组的部分对应的字符串为:"+s4);//chars字符数组的部分对应的字符串为:rqaqo
6.public String(String original)
初始化新创建的String对象,使其表示与参数相同的字符序列;
// 换句话说,新创建的字符串是参数字符串的副本。
String s5="大家好";String s6=new String(s5);System.out.println(s6);System.out.println(s6.length());
3. 字符串是常量,它的值在创建后不能被改变
String s = "hello";s += "world";请问s的值是什么?
内存图辅助理解:
public class StringDemo2 {public static void main(String[] args) {String s="Hello";s+="world";System.out.println("拼接后的字符串为:"+s);}
}
4. String s = new String(“hello”)和String s = “hello”;的区别?
字符串比较之看程序写结果
字符串拼接之看程序写结果
注意事项:
1、==比较引用数据类型的时候,比较的时候地址值
2、String类中使用equals方法比较的是字符串的值,因为String类中重写了equals方法
字符串new与不new的区别
举例:1.
public class StringDemo3 {public static void main(String[] args) {String s1=new String("hello");String s2="hello";System.out.println(s1==s2);//falseif (s1!=null){System.out.println(s1.equals(s2));}//trueelse System.out.println("s1为null");}
}
2.看程序写结果
public class StringDemo4 {public static void main(String[] args) {String s1=new String("helloworld");String s2=new String("helloworld");System.out.println(s1==s2);//falseSystem.out.println(s1.equals(s2));//trueString s3="myname";String s4=new String("myname");System.out.println(s3==s4);//falseSystem.out.println(s3.equals(s4));//trueString s5="happy";String s6="happy";System.out.println(s5==s6);//trueSystem.out.println(s5.equals(s6));//true}
}
5.字符串相加的情况
(1)、字符串如果是变量相加,是先在常量池中开辟空间,然后再做拼接。
(2)、字符串如果是常量相加,是先相加,然后再去常量池中去找,如果找到了,就返回,如果没有找到就开辟新的空间,存储拼接后的值。
举例:
public class StringDemo5 {public static void main(String[] args) {String s1="hello";String s2="world";String s3="helloworld";String s4="hello"+"world";System.out.println(s3==(s1+s2));//falseSystem.out.println(s3==s4);//trueString s5=s1+s2;System.out.println(s5==s4);//flaseSystem.out.println(s5==s3);//false}
}
6 String类的判断功能:
(1) boolean equals(Object obj)比较字符串中的内容是否相同,区分大小写
public class StringDemo6 {public static void main(String[] args) {// boolean equals(Object obj)比较字符串中的内容是//否相同,区分大小写String s1="helloworld";String s2=new String("helloworld");System.out.println(s1.equals(s2));//true
(2) boolean equalsIgnoreCase(String str)比较字符串中的内容是否相同,忽略大小
String s3="Hellowoldpeace";String s4=new String("HELLOWOLDpeaCE");System.out.println(s3.equalsIgnoreCase(s4));//true
(3) boolean contains(String str)判断大的字符串中是否包含小的字符串,如果包含,返回true,反之返回false,区分大小写
String s5="happybirthday";String s6=new String("birthday");String s7="hyb";System.out.println(s5.contains(s6));//trueSystem.out.println(s5.contains(s7));//false
(4). boolean startsWith(String str)测试此字符串是否以指定字符串开头
String s8=new String("ooodayuuooo");String s9="ooo";String s10="ooodayuuoo";System.out.println(s8.startsWith(s9));//trueSystem.out.println(s8.startsWith(s10));//true
// 区分大小写
(5) . boolean endsWith(String str) 测试此字符串是否以指定字符串结尾区分大小写
String s11=new String("jsndool");String s12="ool";String s13="deool";System.out.println(s11.endsWith(s12));//trueSystem.out.println(s11.endsWith(s13));//false
(6). boolean isEmpty()判断字符串是否为空
String s14="ssssssssss";String s15="";String s16=null;System.out.println(s14.isEmpty());//falseSystem.out.println(s15.isEmpty());//true//System.out.println(s16.isEmpty());//NullPointerException}
}
7. String类的获取功能:
(1)int length() 获取字符串的长度
public class StringDemo7 {
public static void main(String[] args) {// int length() 获取字符串的长度String s =new String("sdjhfakjhdajkfhjhakfj");System.out.println(s.length());//21
(2) int indexOf(int ch)返回指定字符第一次出现的字符串内的索引。
String s5="day one day";System.out.println(s5.indexOf('s'));//-1System.out.println(s5.indexOf('a'));//1
(3) int indexOf(String str)返回指定子字符串第一次出现的字符串内的索引, 返回的是小串的第一个字符在大串中出现的索引位置
String s2="everyday happy birthday";String s3="happy";String s4="birt";System.out.println(s2.indexOf(s3));//9 注意空格//也算一个字符System.out.println(s2.indexOf(s4));//15System.out.println(s2.indexOf('o'));//-1 没有则//返回-1
(4) int indexOf(int ch,int fromIndex)返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。如果找到了,返回的是字符在整个字符串中的索引
String s6=new String("justoneday");System.out.println(s6.indexOf('o',3));//注意无论//从索引几开始,返回的都是该字符在整个字符串中的索引System.out.println(s6.indexOf('l',4));//-1System.out.println(s6.indexOf('d',1000));//-1 //注意此方法不会报出索引越界异常
(5) .int indexOf(String str,int fromIndex) indexOf()方法不会报出索引越界异常,返回指定字符串第一次出现的字符串内的索引,以指定的索引开始搜索。
String s7="yydswonderful";System.out.println(s7.indexOf("won",3));//4 返回//的是子字符串的首字母在整个字符串中首次出现的索引System.out.println(s7.indexOf("ful",1000));//-1System.out.println(s7.indexOf("yydo",0));//-1
(6) . char charAt(int index)返回指定索引处的字符
String s1=new String("doublekill");System.out.println(s1.charAt(1));//获取索引为1的//字符,所以结果为o
(7) String substring(int start)从指定位置处截取字符串,包括开始截取的位置,截取到末尾如果给的索引值不存在,报错
String s8=new String("hardworkingeveryday");String s9=s8.substring(11);System.out.println(s9);//everyday// System.out.println(s8.substring(1000));//StringIndexOutOfBoundsExceptionSystem.out.println(s8.substring(0));//hardworkingeveryday
(8) String substring(int start,int end) // 截取字符串的一部分出来,截取的串从start位置开始截取,截取到end-1的位置结束左闭右开 [,) 含头不含尾。
String s10="sgdv天都黑了znja";System.out.println(s10.substring(4,8));//天都黑了// System.out.println(s10.substring(4,100));//StringIndexOutOfBoundsExceptio
8.String类的转换功能:
(1) byte[] getBytes()使用平台的默认字符集将此String编码为字节序列,将结果存储到新的字节数组中。将字符串转成字节数组 与s=new String(bytes);是相反的方法
public class StringDemo8 {public static void main(String[] args) {String s = "happyBrithDAY";byte[] bytes = s.getBytes();System.out.println(bytes);//[B@4554617c字符数组的//地址值for (int i = 0; i < bytes.length; i++) {System.out.print(bytes[i] + ",");//104,97,112,112,121,66,114,105,116,104,68,65,89// 输出为ASCLL码值}System.out.println();
(2) char[] toCharArray() 将字符串转成字符数组
String s1 = "hellonicetomeetyou";char[] chars = s1.toCharArray();for (int i = 0; i < chars.length; i++) {System.out.print(chars[i] + ",");//h,e,l,l,o,n,i,c,e,t,o,m,e,e,t,y,o,u,}System.out.println();
(3). static String valueOf(char[] chs)将字符数组转成字符串
char[] chars1=new char[]{'w','a','努','力','赚','钱','y','y','d','s'};String s2=String.valueOf(chars1);//注意这是//static修饰需要用类名调用System.out.println(s2);//wa努力赚钱yyds
(4) static String valueOf(int i)将int类型的数据转成字符串 此方法不仅可以转int型,其他基本数据类型也可以转
int i=100000000;String s3=String.valueOf(i);System.out.println(s3);//100000000
(5) String toLowerCase()将字符串中的内容全部转小写
String s4="HELLOGOODmorNINg";String s5=s4.toLowerCase();System.out.println(s5);//hellogoodmorning
(6) String toUpperCase()将字符串中的内容全部转大写
String s6="more and more";String s7=s6.toUpperCase();System.out.println(s7);//MORE AND MORE
(7) String concat(String str)将小括号中str的字符串拼接到大字符串的后面
String s8="How are you";String s9="I am fine,thank you";String s10=s8.concat(s9);System.out.println(s10);//How are youI am fine,thank you
(8) public String[] split(String regex) //需求:求出字符串中的每一个单词 一定注意这里返回的是一个字符串数组,要用一个字符串数组去接收
String s11="do - good - evening - every- one";String[] strings=s11.split("-");for (int j=0;j< strings.length;j++){System.out.print(strings[j]);//do good evening every one}
}
9.String类的其他功能:
(1) 替换功能
- String replace(char old,char new)将新的字符替换字符串中指定的所有字符,并返回一个替换后的字符串,但原字符s不变
public class StringDemo9 {public static void main(String[] args) {// String replace(char old,char new) 将新的字符//替换字符串中指定的所有字符,并返回一个替换后的字符串String s="2022/1/20";String s1=s.replace('/','-');System.out.println(s1);//2022-1-20System.out.println(s);//2022/1/20
- String replace(String old,String new)将新的字符串替换字符串中指定的所有字符串,并返回一个替换后的字符串
String s2="goodafternoon";String s3=s2.replace("afternoon","morning!!!!");System.out.println(s3);//goodmorning!!!!System.out.println(s2.replace("opp","good"));//goodafternoon要被替换的字符串不存在的话就返回原来字符串
(3)String trim()去除字符串两空格去除字符串两空格仅仅去头部和尾部的空格
String s4=" 努力赚钱 ";String s5=s4.trim();System.out.println(s5);//努力赚钱
(4)按字典顺序比较两个字符串
int compareTo(String str)
比较字符串是否相同,如果相同返回0,如果两串不同的话,1.例如s10是s6的子串的情况下compareTo的结果就用s6.length-s10.length
2.如果它不是s6的字串的话就用s6与s9第一个不相同的字母的ASCLL码值去相减,例如就用o的ASCLL码值减去p的ASCLL码值结果为-1.
- int compareTo(String str)
int compareToIgnoreCase(String str)(忽略大小写)
/*此算法原码为int len1 = value.length;int len2 = anotherString.value.length;int lim = Math.min(len1, len2);char v1[] = value;char v2[] = anotherString.value;int k = 0;while (k < lim) {char c1 = v1[k];char c2 = v2[k];if (c1 != c2) {return c1 - c2;}k++;}return len1 - len2;
}
String s6="hello";String s7="hello";String s8="qwe";String s9="hellpq";String s10="hel";System.out.println(s6.compareTo(s7));//0System.out.println(s6.compareTo(s8));//-9System.out.println(s6.compareTo(s9));//-1System.out.println(s6.compareTo(s10));//2}
}
总结果: