当前位置: 代码迷 >> 综合 >> JAVA 字符串常用函数
  详细解决方案

JAVA 字符串常用函数

热度:53   发布时间:2024-02-11 15:54:17.0

字符串比较函数


public boolean equals(Object anObject) :只有参数是一个字符串且内容相同才返回true,区分大小写。

public boolean equalsIgnoreCase(String anotherString):  比较是否相等,区别大小写。


字符串获取函数


public int length():返回字符串的长度。

public String concat (String str):将指定字符串连接到该字符串的末尾,产生一个新的字符串。

public char charAt (int index):返回指定索引处的char值

public int indexof (String str):返回指定子字符串第一次出现在该字符串内的索引,如果没有返回-1。

public String substring(int beginIndex) :返回一个子字符串,从begininex开始截取字符串到字符串结尾。

public String substring(int beginindex,int endIndex):返回一个子字符串,从beginindex到endindex截取字符串,左闭右开。


字符串转换的方法


public char[] toCharArray():将此字符串转换为新的字符数组

public byte[] getBytes(): 使用平台的默认字符集将该 String 编码转化为新的字节数组

public String replace(CharSequence target,CharSequence repalcement):将与target匹配的字符串用replacement字符串来替换。


字符串分割功能


public String[] split(String regex):将此字符串按照给定的regex(正则表达式规则)拆分为字符串数组。

eg:正则表达式规则。“.” 需要写成“\\.”