课下作业04-2String的使用方法

1.动手动脑之String.equals()方法
public class StringEquals { 
public static void main(String[] args) { 
String s1=new String("Hello");
String s2=new String("Hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
String s3="Hello";
String s4="Hello";
System.out.println(s3==s4);
System.out.println(s3.equals(s4));
}
}

结果截图:

2.String类的Length()、charAt()、 getChars()、replace()、 toUpperCase()、 toLowerCase()、trim()、toCharArray()方法使用说明

Length():

参数:无

返回值:调用此方法的字符串的长度(int)

charAt() :

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

返回值:返回指定索引处的字符

getChars():

void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin):

将字符从此字符串复制到目标字符数组。

参数:

srcBegin -- 字符串中要复制的第一个字符的索引。

srcEnd -- 字符串中要复制的最后一个字符之后的索引。

dst -- 目标数组。

dstBegin -- 目标数组中的起始偏移量。

replace():

String replace(char oldChar, char newChar)

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

参数:

     oldChar -- 原字符。

     newChar -- 新字符。

  返回值:替换后生成的新字符串。

toUpperCase():

 String toUpperCase()

 toUpperCase() 方法将字符串小写字符转换为大写。

  参数:无

 返回值:字符转换为大写后的字符串。

toLowerCase():

 String toLowerCase()

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

 参数:无

返回值:字符转换为小写后的字符串。

trim():

 String trim():

 trim() 方法用于删除字符串的头尾空白符。

 参数:无

返回值:删除头尾空白符的字符串。

toCharArray():

char[] toCharArray():

toCharArray() 方法将字符串转换为字符数组。

  参数:无

  返回值:字符数组

原文地址:https://www.cnblogs.com/ggrm/p/7739612.html