关于string:JavaString的常用方法总结

6次阅读

共计 3111 个字符,预计需要花费 8 分钟才能阅读完成。

Java-String 的罕用办法总结:

  一、String 类

  String 类在 java.lang 包中,java 应用 String 类创立一个字符串变量,字符串变量属于对象。java 把 String 类申明的 final 类,不能继承。String 类对象创立后不能批改,由 0 或多个字符组成,蕴含在一对双引号之间。

  二、String 类构造方法

  1、public String()

  无参构造方法,用来创立空字符串的 String 对象。

  String str1=new String();

  String str2=new String(“asdf”);

  2、public String(String value)

  String str2=new String(“asdf”);

  3、public String(char[]value)

  char[]value={‘a’,’b’,’c’,’d’};

  String str4=new String(value);

  4、public String(char chars[],int startIndex,int numChars)

  char[]value={‘a’,’b’,’c’,’d’};

  String str5=new String(value,1,2);

  5、public String(byte[]values)

  byte[]strb=new byte[]{65,66};

  String str6=new String(strb);

  三、String 类罕用办法

  1、public char charAt(int index)

  参数

  index– 字符的索引。

  返回值

  返回指定索引处的字符。

  实例

  public class Test{
  public static void main(String args[]){
  String s=”www”;
  char result=s.charAt(1);
  System.out.println(result);
  }
  }
复制
  以上程序执行后果为:

  w

  2、public boolean equals(Object anObject)

  参数

  anObject– 与字符串进行比拟的对象。

  返回值

  如果给定对象与字符串相等,则返回 true;否则返回 false。

  实例

  public class Test{
  public static void main(String args[]){
  String Str1=new String(“run”);
  String Str2=Str1;
  String Str3=new String(“run”);
  boolean retVal;
  retVal=Str1.equals(Str2);
  System.out.println(“ 返回值 =”+retVal);
  retVal=Str1.equals(Str3);
  System.out.println(“ 返回值 =”+retVal);
  }
  }
复制
  以上程序执行后果为:

  返回值 =true

  返回值 =true

  3、public boolean endsWith(String suffix)

  endsWith() 办法用于测试字符串是否以指定的后缀完结。

  参数

  suffix– 指定的后缀。

  返回值

  如果参数示意的字符序列是此对象示意的字符序列的后缀,则返回 true;否则返回 false。留神,如果参数是空字符串,或者等于此 String 对象(用 equals(Object) 办法确定),则后果为 true。

  实例

  public class Test{
  public static void main(String args[]){
  String Str=new String(“runooo”);
  boolean retVal;
  retVal=Str.endsWith(“run”);
  System.out.println(“ 返回值 =”+retVal);
  retVal=Str.endsWith(“ooo”);
  System.out.println(“ 返回值 =”+retVal);
  }
  }
复制
  以上程序执行后果为:

  返回值 =false

  返回值 =true

  4、public boolean equalsIgnoreCase(String anotherString)

  equalsIgnoreCase() 办法用于将字符串与指定的对象比拟,不思考大小写。

  参数

  anObject– 与字符串进行比拟的对象。

  返回值

  如果给定对象与字符串相等,则返回 true;否则返回 false。

  public class Test{
  public static void main(String args[]){
  String Str1=new String(“run”);
  String Str2=Str1;
  String Str3=new String(“run”);
  String Str4=new String(“RUN”);
  boolean retVal;
  retVal=Str1.equals(Str2);
  System.out.println(“ 返回值 =”+retVal);
  retVal=Str3.equals(Str4);
  System.out.println(“ 返回值 =”+retVal);
  retVal=Str1.equalsIgnoreCase(Str4);
  System.out.println(“ 返回值 =”+retVal);
  }
  }
复制
  以上程序执行后果为:

  返回值 =true

  返回值 =false

  返回值 =true

  5、public String replace(char oldChar,char newChar)

  replace() 办法通过用 newChar 字符替换字符串中呈现的所有 oldChar 字符,并返回替换后的新字符串。

  参数

  oldChar– 原字符。

  newChar– 新字符。

  返回值

  替换后生成的新字符串。

  public class Test{
  public static void main(String args[]){
  String Str=new String(“hello”);
  System.out.print(“ 返回值:”);
  System.out.println(Str.replace(‘o’,’T’));
  System.out.print(“ 返回值:”);
  System.out.println(Str.replace(‘l’,’D’));
  }
  }
复制
  以上程序执行后果为:

  返回值:hellT

  返回值:heDDo

  6、public String toLowerCase()

  toLowerCase() 办法将字符串转换为小写。

  参数

  无

  返回值

  转换为小写的字符串。

  public class Test{
  public static void main(String args[]){
  String Str=new String(“WWW”);
  System.out.print(“ 返回值:”);
  System.out.println(Str.toLowerCase());
  }
  }
复制
  以上程序执行后果为:

  返回值:www

原创申明,本文系作者受权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请分割 cloudcommunity@tencent.com 删除。

正文完
 0