String类的认识

String

 

String():无参构造

Stringbyte[] bytes:将字节数转换成字符串

Public String byte[] bytes,int index,int length:将字节数组的一部分转化为字符串

Public String char[] value:将字符转化成字符串

Public string char[] value,int index,int count:将字符数组中的一部分转化为字符串

Public StringString original:将一个字符串常量构造成一个字符串对象

例如:

package com.TextDome1;
 
public class StringDome1 {
 
public static void main(String[] args) {
String s=new String();
System.out.println("s:"+s);
System.out.println("-----------------");
byte[] byt= {'I','l','o','v','e','u'};
String s1=new String(byt);
System.out.println("s1:"+s1);
String s2=new String(byt,1,4);
System.out.println("s2:"+s2);
System.out.println("----------");
char[] c= {'我','爱','迪','丽','热','巴'};
String s3=new String(c);
System.out.println("s3:"+s3);
String s4=new String(c,1,5);
System.out.println("s4:"+s4);
System.out.println("---------------");
String s5=new String("abc");
System.out.println("s5:"+s5);
}
}
运行结果如下:
/**
 * s:
 * ---------------
 * s1:Iloveu
 * s2:love
 * ---------------
 * s3:我爱迪丽热巴
 * s4:爱迪丽热巴
 * ---------------
 * s5:abc
 */

以上便是String的主要构造方法,具体用法在例子中已经体现出来

String最大的特点就是当字符串被赋值后,就不能改变他的值

String作为形式参数的时候,它的效果和实际类型是一样的,对实际参数没有什么影响。

例如:

package com.TextDome1;
 
public class StringDome2 {
 
public static void main(String[] args) {
String str1="hello";
String str2="迪丽";
System.out.println("str1:"+str1+",str2:"+str2);
System.out.println("-----------------");
change(str1,str2);
System.out.println("str1:"+str1+",str2:"+str2);
}
public static void change(String s1,String s2) {
s1+="world";
s2+="热巴";
}
}
运行结果:
/**
 *str1:hello,str2:迪丽
 *-----------------
 *str1:hello,str2:迪丽
 */

模拟用户登录账号示例:

package com.TextDome1;
import java.util.Scanner;
/**
 * @author 杰哥
 *模拟用户登录三次
 */
public class StringDome4 {
 
public static void main(String[] args) {
String username="radim";
String password="radim";
for(int x=0;x<3;x++) {
Scanner sc=new Scanner(System.in);
System.out.println("请您输入用户名");
String str1=sc.nextLine();
System.out.println("请您继续输入密码");
String str2=sc.nextLine();
if(str1.equals(username)&&str2.equals(password)) {
System.out.println("恭喜您登录成功");
break;
}else {
if((2-x)==0) {
System.out.println("请您尽快联系管理员解决");
}else {
System.out.println("您还有"+(2-x)+"次机会");

}
}
}

}
}

猜字小游戏的示例:

package com.TextDome1;
import java.util.Scanner;
/**
 * @author 杰哥
 *猜字小游戏
 */
public class StringDome5 {
private StringDome5() {
 
}
public static void method() {
 int num=(int)( Math.random()*100)+1 ;
 while(true) {
 Scanner sc=new Scanner(System.in);
 System.out.println("请输入您要猜的数");
 int num1=sc.nextInt();
 if(num1>num) {
  System.out.println("您猜的有点大了,继续猜");
 }else if(num1<num) {
  System.out.println("您猜的有点小了,继续猜");
 }else {
  System.out.println("恭喜您,终于猜对了");
  break;
 }
 }
}
public static void main(String[] args) {
 StringDome5.method();
}
}

 

String类的一些常用功能:

 

1)基本判断功能:

虽然很简单,看看就能懂,但是个人建议自己手打一遍巩固理解

 

  /* 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():判断字符串是否为空

public String concat(String str):字符串的特有功能:拼接功能 +拼接符是一个意思

   */

package com.TextDome1;
public class StringDome3 {
 
public static void main(String[] args) {
String s1="helloreba";
String s2="Helloreba";
System.out.println(s1.equals(s2));
System.out.println("--------------");
System.out.println(s1.equalsIgnoreCase(s2));
System.out.println("--------------");
System.out.println(s1.contains("reba"));
System.out.println(s1.contains("baby"));
System.out.println("---------------");
System.out.println(s1.startsWith("hell"));
System.out.println(s1.endsWith("baby"));
System.out.println("----------------");
System.out.println(s1.isEmpty());
System.out.println("---------------");
System.out.println(s1.concat(" nice"));
}
}
运行结果如下:
/**
 * @author 杰哥
 *false
 *--------------
 *true
 *--------------
 *true
 *false
 *---------------
 *true
 *false
 *----------------
 *false
 *---------------
 *helloreba nice
 */

2)获取功能:


/**

 * String类的常用获取功能:

 *

 * public int length():获取字符串的长度

 * public char charAt(int index)返回指定索引处的 字符

 * public int indexOf(int ch)返回指定字符在此字符串中第一次出现处的 索引

 * 问题:既然传入的字符:这里为什么用int类型

 * 'a'和97 都表示a

 * public int indexOf(int ch,int fromIndex)返回在此字符串中第一次 出现指定字符处的索引,从指定的索引开始搜索。

 * public int indexOf(String str)返回指定子字符串在此字符串中第一次 出现处的索引

 * public int indexOf(String str,int fromIndex)回在此字符串中第一 次出现指定字符串处的索引,从指定的索引开始搜索。

 */

package com.TextDome1;
public class StringDome6 {
 
public static void main(String[] args) {
String str="abcdefcba";
System.out.println("length:"+str.length());
System.out.println("---------------");
System.out.println("charAt:"+str.charAt(3));
System.out.println("----------------");
System.out.println("index of:"+str.indexOf("c"));
System.out.println("-----------------");
System.out.println("index of:"+str.indexOf("c", 4));
System.out.println("-----------------");
System.out.println("index of:"+str.indexOf("a"));
System.out.println("------------------");
System.out.println("index of:"+str.indexOf("a",3));
System.out.println("-------------------");
}
}
运行结果:
/**
 * @author 杰哥
 *length:9
 *---------------
 *charAt:d
 *----------------
 *index of:2
 *-----------------
 *index of:6
 *-----------------
 *index of:0
 *------------------
 *index of:8
 *-------------------
*/

3)截取功能


/**

 * 截取功能

 * public String substring(int beginIndex):从指定位置开始截取,默 认截取到末尾,返回新的字符串

 * public String substring(int beginIndex, int endIndex):从指定 开始到指定位置末尾结束,包前不包含

 *

 */

package com.TextDome1;
public class StringDome6 {
 
public static void main(String[] args) {
String str="abcdefcba";
System.out.println("substring:"+str.substring(3));
System.out.println("---------------");
System.out.println("substring:"+str.substring(3, 7));
}
}
 
运行结果:
/**
 * @author 杰哥
 *substring:defcba
 *----------------
 *substring:defc
 */

4)转换功能


/**

 * String的常用转换功能:

 *

 * public byte[] getBytes() :将字符串转换为字节数组

 * public char[] toCharArray() :将字符串转换成字符数组(重点)

 * public static String valueOf(int i):将int类型的数据转换成字符 (重点)

 * 这个方法可以将任何类型的数据转化成String类型

 * public String toLowerCase():转成小写

 * public String toUpperCase():字符串中所有的字符变成大写

 * public String concat():将两个字符串拼接在一起

*replace():是将字符串中的一个字符替换成另一个字符

*trim():是去除字符串两端的空格

 */

package com.TextDome1;
public class StringDome8 {
 
public static void main(String[] args) {
String str="JavaSE";
byte[] byt=str.getBytes();
for(int x=0;x<byt.length;x++) {
System.out.print(byt[x]+",");
}
System.out.println("getBytes:"+byt);
System.out.println("---------------");
char[] ch=str.toCharArray();
for(int i=0;i<ch.length;i++) {
System.out.print(ch[i]+" ");
}
System.out.println("tochararrays:"+ch);
System.out.println("---------------");
System.out.println(str.valueOf(3)); //这个3是字符串“3”
System.out.println("----------------");
System.out.println(str.toLowerCase());
System.out.println("----------------");
System.out.println(str.toUpperCase());
String s1="hello";
String s2="world";
System.out.println("concat:"+s1.concat(s2));
System.out.println("---------------");
System.out.println(str.replace('a', 'A'));
System.out.println("-----------------");
System.out.println(str.trim());
}
}
运行结果:
/**
 * @author 杰哥
 *74,97,118,97,83,69,getBytes:[B@70dea4e
 *---------------------------
 *J a v a S E tochararrays:[C@5c647e05
 *-------------------------
 *3
 *-------------------------
 *javase
 *-------------------------
 *JAVASE
 *------------------------
 * concat:helloworld
 * -----------------------
 *  JAvASE 
 * -----------------------
* JavaSE
 * */

CompareTo的理解:

package com.TextDome1;
 
public class StringDome {
 
public static void main(String[] args) {
String s1="helloworld";
String s2="hello";
String s3="hello";//h的ASCII码是104
String s4="baby";//b的ASCII码是98
String s5="hello";//l的ASCII码是108
String s6="heabc";//a的ASCII码是97
System.out.println(s1.compareTo(s2));
System.out.println("------------");
System.out.println(s3.compareTo(s4));
System.out.println("--------------");
System.out.println(s5.compareTo(s6));
}

}
运行结果为:
/**
 * @author 杰哥
 *5
 *------------
 *6
 *-------------
 *11
 */

根据上例可以得出compareTo的理解:

当两个字符串的如s1s2一样,一个包含另一个的时候,输出的值是两者的长度差

当两个字符串的首字母相同,其他字母不同的时候,是第一个不同的字母的ASCII码之差

当两个字符串的首字母及部分字母相同时,找到不同字母,取第一个不同字母的ASCII码之差。

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/j_better/article/details/80098902