Java学习--String

String类

String类位于java.lang包内,为final类型,也就是说该类不允许被继承。

构造方法

1、可以直接将字符串常量对象(用“”引起来的字符串)的引用赋给String类声明的对象。

String s = "hello";

2、以字符串为参数的构造方法。

String s = new String("hello");

3、以字符数组为参数的构造方法

char[] ch = {'h','e','l','l','o'};
String s = new String(ch);

以字符数组的一部分创建String对象:String(char a[],int startIndex,int length),从字符数组的第startIndex+1个开始,截取length个作为创建对象的实体。

char[] ch = {'h','e','l','l','o'};
String s = new String(ch,1,2);

s的实体为“el”。

常用方法

1、public int length()
获取字符串的长度。

2、public boolean equals(String s)
判断原字符串是否与指定字符串相同。相同返回true,否则返回false。
注意:与“==”不同,用“equals“比较的是两个对象中的实体是否相同,而”==“则比较的是对象的引用,比如:

String s = new String("hello");
String s0 = new String("hello");
System.out.print((s==s0)+" "+s.equals(s0));

结果为:false true

3、public boolean startsWith(String s)和public boolean endsWith(String s)
判断原字符串是否以指定字符串开始或结束。是则返回true,否则返回false

4、public int compareTo(String s)
将原字符串与指定字符串按字典顺序进行比较,如果原字符串大则返回正数,相等返回”0“,否则返回负数。

5、public boolean contains(String s)
判断原字符串中是否存在一个子串能与指定字符串进行匹配。

6、public int indexOf(String s)
返回原字符串中第一个能与指定字符串匹配的子串的索引位置。

7、public String trim()
去掉字符串的前后空格。

8、public String substring(int startpoint, int length)
返回原字符串中从指定位置处开始,长度为length的子串。

9、public boolean regionMatches(int firstSatrt, String other, int otherStart)
检查原字符串中从firstStart处开始长度为length的子串是否与指定字符串other中从otherStart处开始长度为length的子串相匹配

10、public char charAt(int index)
返回字符串中第index+1个字符

简单数据类型与字符串的相互转化

使用java.lang包中的Integer类的parseInt()方法将字符串类型转化成int:

String s = "123";
int n = Integer.parseInt(s);

类似的可以利用,Byte、Short、Long、Float、Double类中的
public static byte parseByte(String s) throws NumberFormatException
public static short parseByte(String s) throws NumberFormatException
public static long parseByte(String s) throws NumberFormatException
public static float parseByte(String s) throws NumberFormatException
public static double parseByte(String s) throws NumberFormatException
方法转化byte、short、long类型数据。

可以用String类的valueOf方法将byte、short、int、long、float、double类型的数据转化为String字符串

public static String valueOf(byte n)
public static String valueOf(short 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)

用Long类的下列方法可将long类型的整数转化不同的进制形式:

public static String toBinaryString(long n) 二进制
public static String toOctalString(long n) 八进制
public static String toHexString(long n) 十六进制
public static String toString(long n, int p) p进制

与正则表达式相关的几个常用方法

1、public boolean matches(String regex)
检查字符串是否能够匹配指定的正则表达式

2、public String replaceAll(String regex, String replacement)
将原字符串中所有能匹配上指定的正则表达式的子串全部替换为指定的字符串

3、public String[] split(String regex)
将原字符串通过能与regex正则表达式匹配的子串分成多个子串

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M FS

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值