`
zbu57zbu
  • 浏览: 28131 次
社区版块
存档分类
最新评论

字符串相关方法

 
阅读更多

  字符串相关方法使用示例1
  public class StringTest{
  public static void main(String args[]){
  String s = new String("Welcome to china");
  //获取字符串长度
  System.out.println("字符串长度为:"+s.length()); 
  //查找字符串中第一次出现字符o的索引
  int firstPos = s.indexOf('o');
  System.out.println("第一个o字符的索引是:"+firstPos);    
  //查找字符串中最后一次出现字符o的索引
  int lastPos = s.lastIndexOf('o');
  System.out.println("最后一个o字符的索引是:"+lastPos);
  char c[] = new char[10];
  s.getChars(0,7,c,0);
  System.out.println("复制出的字符串是:"+ new String(c));
  }
  字符串相关方法使用示例2
  public class StringTest{
  public static void main(String args[]){
  String s = new String("Welcome to china");
  System.out.println(s.substring(3,8));
  System.out.println(s.replace('o','_'));
  System.out.println(s.toLowerCase());
  System.out.println(s.toUpperCase());
  }
  }
  常用方法总结:
  char charAt(int index) 
  返回指定索引处的 char 值。
  boolean equals(Object anObject) 
  比较此字符串与指定的对象。 
  String replace(char oldChar, char newChar) 
  返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的。
  String substring(int beginIndex) 
  返回一个新的字符串,它是此字符串的一个子字符串。
  String trim() 
  返回字符串的副本,忽略前导空白和尾部空白。 
  static String valueOf(boolean b) 
  返回 boolean 参数的字符串表示形式。 
  static String valueOf(char c) 
  返回 char 参数的字符串表示形式。 
  static String valueOf(char[] data) 
  返回 char 数组参数的字符串表示形式。 
  static String valueOf(char[] data, int offset, int count) 
  返回 char 数组参数的特定子数组的字符串表示形式。 
  static String valueOf(double d) 
  返回 double 参数的字符串表示形式。 
  static String valueOf(float f) 
  返回 float 参数的字符串表示形式。 
  static String valueOf(int i) 
  返回 int 参数的字符串表示形式。 
  static String valueOf(long l) 
  返回 long 参数的字符串表示形式。 
  static String valueOf(Object obj) 
  返回 Object 参数的字符串表示形式。 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics