04-课后作业2-动手动脑及String类整理

1. 动手动脑之String.equals()方法

 
public class StringEquals {
    
/**
     * @param args the command line arguments
     */
    
	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 字符,并返回替换后的新字符串。

public String replace(char oldChar, char newChar)   用字符newChar替换当前字符串中所有的oldChar字符,并返回一个新的字符串。

参数:

     oldChar -- 原字符。

     newChar -- 新字符。

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

 toUpperCase()   toLowerCase()

字符串中字符的大小写转换
1)public String toLowerCase()//返回将当前字符串中所有字符转换成小写后的新串
2)public String toUpperCase()//返回将当前字符串中所有字符转换成大写后的新串

trim():

 String trim():

 trim() 方法用于删除字符串的头尾空白符,截去字符串两端的空格,但对于中间的空格不处理。

 参数:无

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

toCharArray():

char[] toCharArray():

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

  参数:无

  返回值:字符数组

原文地址:https://www.cnblogs.com/cj-125/p/7744270.html