首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Java基础-字符串处理String类

本文是java基础的一篇,介绍java.lang.String类。

1、平台默认字符集

平台默认字符集是java虚拟机启动时决定的,通常依操作系统的locale和字符集来而定,所以别使用默认字符集,避免产生移植问题。

The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.

2、构造函数

我们很少使用String构造函数,通常是使用字面量来定义一个String,如:

String s = "软匠"

当我们从byte数组、char数组和int数组构造String时会使用到构造函数。

String(char value[])

String(int[] codePoints, int offset, int count)

String(byte bytes[], Charset charset)

不要使用默认字符集来构造String,即不要使用下面这个方法,默认字符集在不同操作系统可能不一样,会导致乱码、加解密失败等情况。

String(byte bytes[])

3、String的方法

字符串长度

public int length()

是否空字符串,默认的实现是length==0,通常语义上我们需要判断一个String是否null,空串(""),非可见字符(空格、回车换行、制表符等)

public boolean isEmpty() {

return value.length == 0;

}

public static boolean isEmpty(final String s) {

return s == null || s.trim().isEmpty();

}

获取第i个字符

public char charAt(int index)

转换为byte数组

public byte[] getBytes(Charset charset)

不要使用默认字符集

public byte[] getBytes()

转换为char数组,当需要按字符处理String时,先转换为char数组再处理可以提高性能。

public char[] toCharArray()

是否包含子串

public boolean contains(CharSequence s)

查找子串

public int indexOf(String str)

public int indexOf(String str, int fromIndex)

public int lastIndexOf(String str)

public int lastIndexOf(String str, int fromIndex)

查找子串通常是配合截取子串来使用的

public String substring(int beginIndex)

public String substring(int beginIndex, int endIndex)

子串开头和结尾

public boolean startsWith(String prefix)

public boolean endsWith(String suffix)

格式化字符串

public static String format(String format, Object... args)

尽量使用format来格式化字符串,而不是字符串拼接,这样代码阅读性更好,如:

String s = "姓名:" + user.getName() + ",年龄:" + user.getAge()

String s = String.format("姓名:%s, 年龄:%d", user.getName(), user.getAge())

正则匹配

public boolean matches(String regex)

子串替换

public String replaceFirst(String regex, String replacement)

public String replaceAll(String regex, String replacement)

字符串分割与合并

public String[] split(String regex)

分割功能String类已经提供,合并的话可以使用遍历数组或集合然后用StringBuilder来拼接各个对象的toString

大小写转换

public String toLowerCase()

public String toUpperCase()

其它类型转为String

我们通过使用对象的toString方法来把对象转成一个String,对于原生类型没有toString方法,我们使用String的valueOf来转换。

public static String valueOf(boolean b)

public static String valueOf(char data[])

public static String valueOf(char c)

public static String valueOf(double d)

public static String valueOf(float f)

public static String valueOf(int i)

public static String valueOf(long l)

public static String valueOf(Object obj) {

return (obj == null) ? "null" : obj.toString();

}

对于对象使用valueOf来转成String还省去了null判断的好处。

把String转成其它类型

原生类型的Wrapper都提供了对象的valueOf方法来把String转成对应的Wapper。

public static Boolean valueOf(String s)

public static Integer valueOf(String s) throws NumberFormatException

public static Long valueOf(String s) throws NumberFormatException

public static Double valueOf(String s) throws NumberFormatException

public static Float valueOf(String s) throws NumberFormatException

4、字符串拼接

能用String.format完成的就不要用字符串拼接,如果需要字符串拼接,考虑使用StringBuilder来完成。

如:

StringBuilder sql = new StringBuilder("SELECT * FROM users WHERE 1=1");

if (query.getId() > 0) {

sql.append(" AND id=:id");

}

if (query.getAge() > 0) {

sql.append(" AND age >= :age");

}

return sql.toString();

本文了介绍了java语言的字符串处理,字符串处理是一门编程语言的基础功能需要熟练掌握。

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180411G0NOU000?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券