Java String类双语教学

String 是Java jdk文件中类库中的一个类,

String 类的设计出现旨在更好的处理字符串的检索, 比较, 获取子集(子字串), 复制并同时更改为upperclass 和 lowerclass, 同时也更加吻合了Java "Everything is an Object" 的面向对象设计语言的规则

1.常用的构造方法
String() 
Initializes a newly created String object so that it represents an empty character sequence.
默认的构造方法, 创建一个内容为空的String 对象

String(char[] value) 
Allocates a new String so that it represents the sequence of characters 
currently contained in the character array argument.
使用char[] 数组构造方法, 创建的String 对象内容完整拷贝自char[] 数组 

String(char[] value, int offset, int count) 
Allocates a new String that contains characters from a subarray of the character array argument.
使用char[] 数组构造方法, offset指定字符串截断的位置, count指定截断的字符个数, 实现指定字符的部分拷贝

String(String original) 
Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.
使用String 类型的构造方法, 创建的String 对象内容完整拷贝自传入的String 对象

可以看到, 第二和第四种构造方法别无二异, 都是完整拷贝穿入的内容, 不过前者采用了char[] 数组, 后者使用的是String 对象
此外, jdk 还为String类提供了使用byte 类型构造的方法, 不经常使用, 可自行翻阅Java 文档

2.检索的库方法
charAt(int index)
Returns the char value at the specified index.
获取指定位置的字符, index用于指定字符的位置

indexOf(int ch) 
Returns the index within this string of the first occurrence of the specified character.
获取指定字符的位置, ch 参数用于指定检索的字符(返回的位置是字符第一次出现的位置)

int indexOf(int ch, int fromIndex) 
Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
获取指定字符的位置 fromIndex 参数指定起始检索的位置(返回的位置是字符第一次出现的位置)

int indexOf(String str) 
Returns the index within this string of the first occurrence of the specified substring. 
获取指定子字符串位置, str 参数用于指定检索的子字符串(返回的位置是子字符串第一次出现的位置)

可以看到, 第一个方法和后面的方法是相反的. 合适的使用第三种方法可以提高检索效率, 若没有检索到要求的字符和字符串, 返回值为-1

3.获取子集(子字符串)的库方法
String substring(int beginIndex) 
Returns a new string that is a substring of this string.
获取指定起始位置的子字符串 beginIndex用于指定起始位置, 默认终点位置是原字符串的结尾

String substring(int beginIndex, int endIndex) 
Returns a new string that is a substring of this string. 
获取指定起始位置和终点位置的子字符串, beginIndex用于指定起始位置, endIndex 用于指定终点位置

第二个方法明显比第一个方法使用更多, 可以同时指定起始和终点位置

4.字符大小写转换的库方法
String toLowerCase() 
Converts all of the characters in this String to lower case using the rules of the default locale.
结果返回转换为小写的字符串

String toUpperCase() 
Converts all of the characters in this String to upper case using the rules of the default locale.
结果返回转会为大写的字符串

5.获取字符串长度信息的库方法
int length() 
Returns the length of this string.
返回字符串的长度信息
boolean isEmpty() 
Returns true if, and only if, length() is 0. 
确认字符串是否为空, 长度为零返回零

6.判定字符串相等的库方法
boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
当且仅当两个字符串长度和内容相同时, 结果返回真值(参数范围扩展到了Object类型)

boolean equalsIgnoreCase(String anotherString) 
Compares this String to another String, ignoring case considerations. 
忽略字符串的大小写进行内容比较

结语:
本文用到的方法均为public, 不用考虑使用权限, 直接使用即可;
本文用到的方法均不是static类, 只能通过创建String 对象进行调用;
Java 类库十分强大, 丰富的类库也是Java 一个优秀的特点, 熟练掌握Java 类库能使编程效率得到质的提升
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值