当前位置:   article > 正文

String的常用方法_public string chushouadd()

public string chushouadd()

String的常用方法

构造方法

public String() :初始化新创建的 String对象,以使其表示空字符序列。 
public String(char[] value) :通过当前参数中的字符数组来构造新的String。
public String(byte[] bytes) :通过使用平台的默认字符集解码当前参数中的字节数组来构造新的 String。
  • 1
  • 2
  • 3
// 无参构造 String str = new String(); 
// 通过字符数组构造 
char chars[] = {'a', 'b', 'c'}; 
String str2 = new String(chars); 
// 通过字节数组构造 
byte bytes[] = { 97, 98, 99 }; 
String str3 = new String(bytes);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

判断功能的方法

public boolean equals (Object anObject) :将此字符串与指定对象进行比较。 
public boolean equalsIgnoreCase (String anotherString) :将此字符串与指定对象进行比较,忽略大小 写。
  • 1
  • 2
// 创建字符串对象 
String s1 = "hello"; 
String s2 = "hello"; 
String s3 = "HELLO"; 
// boolean equals(Object obj):比较字符串的内容是否相同 
System.out.println(s1.equals(s2)); //true 
System.out.println(s1.equals(s3)); // false 
System.out.println("‐‐‐‐‐‐‐‐‐‐‐"); 
//boolean equalsIgnoreCase(String str):比较字符串的内容是否相同,忽略大小写 
System.out.println(s1.equalsIgnoreCase(s2)); // true 
System.out.println(s1.equalsIgnoreCase(s3)); // true 
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

获取功能的方法

public int length () :返回此字符串的长度。 
public String concat (String str) :将指定的字符串连接到该字符串的末尾。 public char charAt (int index) :返回指定索引处的 char值。 
public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。 
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符 串结尾。 
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到 endIndex截取字符串。含beginIndex,不含endIndex。
  • 1
  • 2
  • 3
  • 4
  • 5

转换功能的方法

 public char[] toCharArray () :将此字符串转换为新的字符数组。 
 public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
 public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使 用replacement字符串替换。
  • 1
  • 2
  • 3

分割功能的方法

public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
  • 1
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/427542
推荐阅读
相关标签
  

闽ICP备14008679号