Java_String类

Java常用实用类学习——String类

String类是java中用于处理字符序列的类。String类在java.lang包中,java把String类定义为final类,用户不能拓展String类,即String类不能有子类。

构造String对象

String对象习惯地被翻译为字符串对象。

常量对象

1
2
String hi = "你好";
String hello = "你好";

Java把用户程序的String常量都放在常量池,所以hi和hello的引用和实体是相同的

String对象

1
2
String s = new String("hello");
String t = new String("hello");

使用String类声明对象并创建对象,凡是new运算符构造构造出的对象都不放在常量池中,所以,尽管s和t的实体是相同(s.equals(t)的值是true),但二者引用不同( s==t 结果是false)。

String类还有两个常见方法

  1. String(char a[]);
1
2
3
4
5
6
char a[] = {'h','i'};
String s = new String(a);

//相当于

String s = new String("hi");
  1. String(char a[],int startIndex,int count);

提取字符数组a中的一部分字符创建对象,startIndex和count是截取的起始位置和截取长度

1
2
3
4
5
6
char a[] = {'你','好','呀'};
String s = new String(a,1,2);

//相当于

String s = new String("好呀");

不可变对象

对象的变量中存储的值不能再发生变化,原因是String是final类,String类也没有给其对象提供修改实体的方法。

String对象的并置

String对象可以使用”+“进行并置运算,即首尾相接得到一个新的String对象。

  • 参与并置运算的String对象中有一个是变量,那么就会得到新的引用和实体
  • 参与并置运算的对象都是常量,那么得到的仍然是常量
1
2
3
4
5
6
7
8
9
String hello = "你好";
String testOne = "你" + "好";
//hello、testOne的引用相同,即hello == testOne的结果为true

String you = new String("你");
String hi = new String("好");
String textTwo = you + hi;
//textTwo、testOne的引用不同,即textTwo == testOne的结果为false

String类的常用方法

public int length();

String类中的length()方法来获取一个String对象的字符序列的长度。

1
2
String s = "戈仑石人";
int n = s.length(); //n的值为4

public boolean equals(String s);

调用该方法比较当前String对象的字符序列是否与参数s指定的String对象的字符序列相同(比较实体是否相同)

1
2
3
4
5
String s1 = "加农炮";
String s2 = new String("加农炮");
s1.equals(s2); //true
s1 == s2; //false
//注意“==”和equals(String s)用法的区别。前者比较引用是否相同,后者判断实体是否相同

public boolean startsWith(String s);

判断当前对象的字符序列前缀是否为参数指定的s

1
2
3
String s = "每张卡牌生来平等,皇家巨人也不例外";
s.startsWith("每张"); //true
s.startsWith("卡牌"); //false

public boolean endWith(String s);

判断当前对象的字符序列后缀是否为参数指定的s

1
2
3
String s = "每张卡牌生来平等,皇家巨人也不例外";
s.endsWith("例外"); //true
s.endsWith("卡牌"); //false

public int compareTo(String s);

对象调用该方法按字典序与参数s的字符序列比较大小。相同返回0,大于s返回正值,小于s返回负值。

1
2
3
String s = "abcde";
s.compareTo("boy"); //负值
s.compareTo("aba"); //正值

public boolean contains(String s);

判断当前对象中是否包含参数s中的字符序列

1
2
3
String s = "瓦基丽武神"; 
s.contains("武神"); //true
s.contains("丽丽"); //false

public int indexOf(String s);

从0位置开始索引首次出现参数s出现位置,并返回该位置,如果没有检索到则返回-1

1
2
String s = "上海自来水来自海上";
int n = s.indexOf("海"); //n等于1

public int lastIndexOf(String s);

从0位置开始索引最后一次出现参数s出现位置,并返回该位置,如果没有检索到则返回-1

1
2
String s = "上海自来水来自海上";
int n = s.lastIndexOf("海"); //n等于7

public String substring(int startpoint);

复制从位置startpoint到最后位置上的字符到新的字符序列。

1
2
String s1 = "我喜欢唱、跳、rap、篮球";
String s2 = s7.substring(3); //s2的字符序列是:唱、跳、rap、篮球

PS:substring(int start,int end)为复制从start位置至end-1位置

public String trim();

调用该方法将得到一个新的对象,该对象是当前对象的字符序列去掉前后空格的字符序列

String对象与基本数据的相互转换

将String转换为基本型

使用Java.lang包中的Integer类调用其类方法***public static int parseInt(String s)***可以将由”数字“字符组成的字符序列转化为int型数据。

1
2
3
int x;
String s = "1234";
x = Integer.parseInt(s);

类似,使用java.lang包中的Byte、Short、Long、Float、Double类调用相应的类方法

1
2
3
4
5
Floatpublic static byte parseByte(String s) throws NumberFormatException
public static short parseShort(String s) throws NumberFormatException
public static long parseLong(String s) throws NumberFormatException
public static float parseFloat(String s) throws NumberFormatException
public static double parseDouble(String s) throws NumberFormatException

将基本型转换为String对象

可以使用以下String类的类方法

1
2
3
4
5
public static String valueOf(byte n)
public static String valueOf(int n)
public static String valueOf(long n)
public static String valueOf(float n)
public static String valueOf(double n)

例:

1
2
3
4
String s1 = String.valueOf(1234);

//也可以用到上面介绍的并置运算,即使用一个空字符串与基本型数据进行并置运算
String s2 = "" + 1234;

基本型数据的进制表示

可以把整形数据(例如int、long型数据的二进制、八进制、或十六进制)转换成String对象,即让String对象封装的字符序列是int或long型数据的二进制、八进制或十六进制。

Integer和long类的下列类方法返回整数的进制的String对象表示(负数返回补码),即返回的String对象封装的字符序列是参数的相应进制

1
2
3
4
5
6
7
public static String toBinaryString(int i)       //二进制
public static String toOctalString(int i) //八进制
public static String toHexString(int i) //十六进制

public static String toBinaryString(long i)
public static String toOctalString(long i)
public static String tohexString(long i)

关于main()方法的参数化

使用解释器java.exe执行程序来执行主类时,从键盘输入若干字符序列 ,用空格分开,它们分别对应args[0]、args[1]、args[2]……

对象的String表示

所有的类都默认是java.lang包中Object类的子类或间接子类。Object类有一个public String toString()方法,一个对象调用该方法返回String对象的字符序列的一般形式为

1
2
3
4
5
创建对象的类的名字@对象的引用

System.out.println(object);
//等价于
System.out,println(object.toString)
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2023-2024 LittleWin
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信