Course note: Strings,String Methods

Strings

要用数组的思想来理解String

与数组声明方法类似,也与类的实例化思想相同

The String object is immutable, meaning once a String object is created, its value cannot be lengthened or shortened; none of the characters canchange .

当两个String直接用+相连:

 

如果像计算相加的结果,需要加()

 

String Methods
 

String concat(String s)

Concatenates the specified strings to the end of this string. Same as +.

Output:

 

 

int length()

– Returns the length of the string;
用法就是数组的:数组名.length 返回元素个数int

 

char charAt(int index)

– returns the specifed character at the position of index in the String
返回该数组的第 index+1个元素

返回

 

String toUpperCase()

– returns a new string by converting all of the characters in this string to uppercase
 

String toLowerCase()

– returns a new string by converting all of thecharacters in this string to lowercase
 

 

String substring(int begin, int end)

- returns a String from this string using characters between the indices begin (inclusive) and end
(exclusive)


String substring(int begin)

– returns a String from this string from the beginning index to the end.
 

 

注意index起始为0,因为这是数组。所以indx是指第index+1个元素

 

String replace(char oldChar, char newChar)

- Returns a string resulting from replacing all occurrences of oldChar in this string with
newChar

新的替换旧的

 

int compareTo(String s)

– Compares this String with thep arameter String s - returns an int <0 if this String precedes the argument String s, an int >0 if this String follows the argument String s, otherwise an int =0 if they are equal

安照ASCII表的顺序,从左向右一个个比较。

 

 

boolean equals (Object s)

– returns true if this String should be considered the same as the parameter object s, which could be another String
 

boolean equalsIgnoreCase (String s)

– compares this string to another String ignoring case Considerations returning true if they should be considered equal.

同长度,同一个char(不区分大小写) 则为true

 

按照数组的思想,以下s1和s2

s1==s2为假,因为两个Hello是在不同的堆里面,此时比较的是s1和s2分别储存的地址(用上面的method比较才会为真)

如果是以以下形式

s1==s2为真,因为此时比较的是它们的值

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值