String类型

String 类提供了连接两个字符串的方法:

string1.concat(string2);

返回 string2 连接 string1 的新字符串。也可以对字符串常量使用 concat() 方法,如:

"我的名字是 ".concat("Runoob");

更常用的是使用'+'操作符来连接字符串,如:

"Hello," + " runoob" + "!"

结果如下:

charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。

public class Test { public static void main(String args[])
{ String s = "www.runoob.com";
char result = s.charAt(6);
System.out.println(result); } }

以上程序执行结果为:

n

endsWith() 方法用于测试字符串是否以指定的后缀结束。
public class Test { public static void main(String args[])
{ String Str = new String("菜鸟教程:www.runoob.com");
boolean retVal;
retVal = Str.endsWith( "runoob" );
System.out.println("返回值 = " + retVal ); retVal = Str.endsWith( "com" );
System.out.println("返回值 = " + retVal ); } }

以上程序执行结果为:

返回值 = false
返回值 = true
contains() 方法用于判断字符串中是否包含指定的字符或字符串。
public class Main { public static void main(String[] args)
{ String myStr = "Runoob";
System.out.println(myStr.contains("Run"));
System.out.println(myStr.contains("o"));
System.out.println(myStr.contains("s")); } }

以上程序执行结果为:

true
true
false


posted @ 2021-09-28 22:08  权。  阅读(109)  评论(0编辑  收藏  举报