【Java学习】String 类
(17)

String类

所属位置

java.lang包(默认引入)

String类的常用方法

public int length()

对象名.length()

获取一个String 对面的字符序列长度

public Boolean equals(String s)

s1.equals(s2)

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

public boolean startsWith(String s)

对象名.startsWith(String s)

String对象调用startsWith(String s)方法,判断当前String对象的字符串序列前缀是否是参数指定的String对象s的字符序列

public boolean endsWith(String s)

对象名.endsWith(String s)

String对象调用endsWith(String s)方法,判断当前String对象的字符串序列后缀是否是参数指定的String对象s的字符序列

public int compareTo(String s)

s1.compareTo(S2);

String对象调用compareTo(String s)方法,按字典顺序与参数指定的String对象s字符串比较大小

public boolean contains(String s)

s1.contains.(s2);

String对象调用contains(String s)方法判断当前的String对象的字符序列是否包含参数s的字符序列

public int indexOf(String s)

文件名.indexOf(String s(,int i))

String对象调用indexOf方法,到第一次出现String对象s的地方,i代表开始检索的位置,输出结果为位置值

public int lastindexOf(String s)

文件名.lastindexOf(String s(,int i))

String对象调用indexOf方法,到最后一次出现String对象s的地方,i代表开始检索的位置,输出结果为位置值

public int indextwo()

文件名.indextwo(String s);

String对象调用indexOf方法,到第2次出现String对象s的地方,i代表开始检索的位置,输出结果为位置值

public String substring(int startpoint)

对象名.substring(int startpoint)

从startpoint开始复制到最后,粘贴到一个新的数组

public String trim()

对象名.trim();

调用trim()方法重新得到个去掉前后空格后的字符序列

getChars()

s.getChars(3,6,a,0);//s字符串第4位到第6位,从a[0]写入数组

public char[] toCharArray()

char [] p = password.toCharArray();

public boolean matches(String regex)

对象名.matches(String regex)

判断当前String对象的字符序列是否和regex指定的正则表达式匹配

字符串的应用

将字符型转换成数字型

int x;
String s = '879';
x = Integer.parseInt(s);

将数字型转换成字符型

String str = String.valueOf(5645.55);

字符串的替换

public String replaceAll(String regex,String replacement)

String str ="12hello567bird".replaceAll("[a-zA-Z]+","你好");

1553566336101834.jpg

字符串的分解


public String[] split(String regex)

按照正则表达式regex作为分割,将字符串分割出来的数组放在String数组当中

本文为作者admin发布,未经允许禁止转载!
上一篇 下一篇
评论
暂无评论 >_<
加入评论