Java-String的常用方法有哪些(java,string,移动开发)

时间:2024-05-10 04:48:16 作者 : 石家庄SEO 分类 : 移动开发
  • TAG :

    Java-String%E7%9A%84%E5%B8%B8%E7%94%A8%E6%96%B9%E6%B3%95%E6%9C%89%E5%93%AA%E4%BA%9B

一、String类

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

二、String类构造方法

1、public String()

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

String str1 =newString();

String str2 =newString("asdf");

2、public String(String value)

String str2 =newString("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)

参数

返回值

返回指定索引处的字符。

实例

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)

参数

返回值

如果给定对象与字符串相等,则返回 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)

参数

返回值

如果参数表示的字符序列是此对象表示的字符序列的后缀,则返回 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)

参数

返回值

如果给定对象与字符串相等,则返回 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)

参数

返回值

替换后生成的新字符串。

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()

参数

返回值

转换为小写的字符串。

public class Test {

public static void main(String args[]) {

String Str = new String("WWW");

System.out.print("返回值 :" );

System.out.println( Str.toLowerCase() );

}

}

以上程序执行结果为:

返回值 :www

本文:Java-String的常用方法有哪些的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Java怎么实现国际化下一篇:

15 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18