Java基础——常用类String及相关字符串操作

记录Java中的常用类String的构造器、方法与关于字符串的操作。

String的构造器:

String()  无参构造

String(byte[] bytes) 根据字节数组创建String对象

String(byte[] bytes, int offset, int length) 根据字节数组bytes中从offset开始的length个字节生成String对象

String(byte[] bytes, int offset, int length, String charsetName) 指定了charsetName编码格式

String(byte[] bytes, String charsetName) 把bytes字节数组中的字节根据charsetName编码格式转换为String对象.

String(char[] value) 根据字符数组创建String对象

String(char[] value, int offset, int count) 把value数组从offset开始的count个字符生成 String对象

String(String original) 根据已有的String创建新的String对象.

String(StringBuffer buffer) 根据 StringBuffer对象创建String对象

String(StringBuilder builder) 根据StringBuidler对象创建String对象

 

String类的方法:

char

charAt(int index) 返回指定索引值对应的字符

int

compareTo(String anotherString) 字符串大小的比较, String类实现了Comparable接口, 根据字符的码值比较, 如果第一个字符串大返回正数,第二个字符串大返回负数,相等返回0

int

compareToIgnoreCase(String str) 忽略大小写

String

concat(String str) 在当前字符串 的后面连接 str .

boolean

contains(CharSequence s) 判断在当前字符串中是否包含s串

boolean

endsWith(String suffix) 判断当前字符串是否以suffix结尾.

boolean

equals(Object anObject) 判断两个字符串中的字符是否一样

boolean

equalsIgnoreCase(String anotherString)

byte[]

getBytes() 返回当前字符串在默认编码下对应的字节数组

byte[]

getBytes(String charsetName) 返回当前字符串在指定编码下对应的字节数组

void

getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 把当前字符串[srcBegin,srcEdn)范围的字符复制到dst数组的dstBegin位置

int

hashCode().

int

indexOf(int ch) 返回字符ch在当前字符串第一次出现的位置,字符在计算机中存储的字符的码值,  int num = ‘中’;

int

indexOf(int ch, int fromIndex) 返回字符ch在当前字符串从fromIndex开始第一次出现的位置

int

indexOf(String str) 返回字符串str在当前字符串第一次出现的位置

int

indexOf(String str, int fromIndex)

String

intern() 返回当前字符串对应的字符串字面量

boolean

isEmpty() 判断字符串是否为空串

int

lastIndexOf(int ch) 返回字符ch在当前字符串最后一次出现的位置

int

lastIndexOf(int ch, int fromIndex)

int

lastIndexOf(String str) 返回字符串str在当前字符串最后一次出现的位置

int

lastIndexOf(String str, int fromIndex)

int

length() 返回字符串中字符的个数.

boolean

matches(String regex) 判断当前字符串是否匹配指定的正则表达式

String

replaceAll(String regex, String replacement)把当前字符串中符合 regex正则表达式的字符替换为replacement

String[]

split(String regex) 使用正则表达式regex分隔当前字符串.

boolean

startsWith(String prefix)

String

substring(int beginIndex) 返回从beginIndex开始的字符串

String

substring(int beginIndex, int endIndex) 返回[beginIndex, endIndex)范围内的字符串

char[]

toCharArray() 把字符串转换为字符数组

String

toLowerCase() 把大写字母转换为小写字母

String

toString()

String

toUpperCase() 小写转大写

String

trim() 去掉字符串前后的空白字符

static String

valueOf(int i) 把基本类型转换为String对象.

static String

valueOf(Object obj)

其他类中有关字符串的操作:

返回值类型所属类方法及介绍
StringBuilderStringBuilderreverse() 继承了父类AbstractStringBuilder的reverse方法,时StringBuilder中的字符串反转。
static booleanCharacterisDigit() 判断一个字符是否为数字。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值