常用方法

获取字符串长度

public int length()

字符串Unicode操作

这部分用的不多,不是很清楚,先记载在这。

//获取指定索引处的元素对应的unciode编码
public int codePointAt(int index)
//获取指定索引处之前的元素对应的unciode编码
public int codePointBefore(int index)
//获取指定的开始索引到结束索引之间元素的unciode编码的个数
public int codePointCount(int beginIndex, int endIndex)
//获取指定索引处的元素开始偏移codePointOffset的索引
public int offsetByCodePoints(int index, int codePointOffset)

字符串字符操作

//获取字符串指定索引处的字符
public char charAt(int index)
//获取字符串中元素对应的字符数组
void getChars(char dst[], int dstBegin)
//获取字符串中部分元素对应的字符数组
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
//获取字符串对应的字符数组
public char[] toCharArray()

字符串字节操作

public byte[] getBytes(String charsetName)
public byte[] getBytes(Charset charset)
public byte[] getBytes()

字符串比较操作

(1)比较是否相等

比较两个字符串是否相等,anObject 必须为字符串,否则返回 false。

public boolean equals(Object anObject)

比较两个字符串是否相等,忽略大小写。

public boolean equalsIgnoreCase(String anotherString)

这两个方法可用于比较String与StringBuilder,StringBuffer是否相等。

String、StringBuilder、StringBuffer 都实现了 CharSequence 接口,StringBuilder是非线程安全的,而StringBuffer是线程安全的。

public boolean contentEquals(StringBuffer sb)
public boolean contentEquals(CharSequence cs)

(2)比较大小

字符串比较大小。

按字符串中字符的字典顺序(也就是字符对应的Unicode值)比较两个字符串,返回 thisString - anotherString 的差值。

thisString > anotherString,返回值大于0

thisString = anotherString,返回值等于0

thisString > anotherString,返回值小于0

public int compareTo(String anotherString)

字符串比较大小,忽略大小写。比较方法和上面相同。

public int compareToIgnoreCase(String str)

(3)部分比较是否相等

当某个字符串调用该方法时,表示从当前字符串的 toffset 位置开始,取一个长度为 len 的子串,然后从另一个字符串 other 的 ooffset 位置开始也取一个长度为 len 的子串,然后比较这两个子串是否相同,如果这两个子串相同则返回 true,否则返回 false。

public boolean regionMatches(int toffset, String other, int ooffset, int len)

比上面的方法多了一个 boolean 类型的 ignoreCase 参数,用来确定比较时是否忽略大小写,当 ignoreCase 为 true 表示忽略大小写。为 false 时和上面方法就相同了。

public boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)

字符串测试操作

(1)开头测试

字符串的指定索引处是否以指定的一个子字符串开头。

public boolean startsWith(String prefix, int toffset)

字符串是否以指定的一个子字符串开头。

public boolean startsWith(String prefix)

(2)结尾测试

字符串是否以指定的一个子字符串结尾

public boolean endsWith(String suffix)

(3)包含测试

字符串是否包含指定的字符序列。

public boolean contains(CharSequence s)

(4)匹配测试

字符串是否能匹配指定的正则表达式。

public boolean matches(String regex)

(5)为空测试

该方法其实是判断字符串的长度是否为0,所以无法判断含有空格的空串。

public boolean isEmpty()

字符串索引操作

返回在此字符串中第一次出现的指定子串 str 的索引。没有则返回 -1

从指定的索引 fromIndex 处开始向后搜索(包括该索引),返回在此字符串中第一次出现的指定子字符串 str 的索引。没有则返回-1。

如果是 int 型参数 ch,则代表的是子串对应的 Unicode 值。

public int indexOf(int ch)
public int indexOf(int ch, int fromIndex)
public int indexOf(String str)
public int indexOf(String str, int fromIndex)

返回在此字符串中最后一次出现的指定的子字符串 str 的索引。

从指定的索引 fromIndex 处开始向前搜索(包括该索引),返回在此字符串中最后一次出现的指定子字符串 str 的索引。没有则返回-1。

如果是 int 型参数 ch,则代表的是子串对应的 Unicode 值。

public int lastIndexOf(int ch)
public int lastIndexOf(int ch, int fromIndex)
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)

字符串修改操作

(1)截取

指定开始索引(包含)和结束索引(不包含),截取字符串。

subSequence 和 substring 一样,源码里直接调用的 substring。

public String substring(int beginIndex)
public String substring(int beginIndex, int endIndex)
public CharSequence subSequence(int beginIndex, int endIndex)

(2)拼接

会将 str 拼接到源字符串后边,返回一个新的字符串。

public String concat(String str)

(3)替换

将指定字符 oldChar 或字符序列 target 全部替换为新的字符 newChar 或者新的字符序列 replacement。

public String replace(char oldChar, char newChar)
public String replace(CharSequence target, CharSequence replacement)

只会替换第一次出现的匹配正则表达式 regex 的子串。

public String replaceFirst(String regex, String replacement)

会替换所有匹配正则表达式 regex 的子串。

public String replaceAll(String regex, String replacement)

(4)分割

以能匹配正则表达式 regex 的分割符进行分割,分割成指定元素个数 limit 的数组。

public String[] split(String regex, int limit)
public String[] split(String regex)

(5)转换大小写

转换为小写,可以指定语言环境,一般直接用默认的语言环境,也就是不指定。

public String toLowerCase(Locale locale)
public String toLowerCase()

转换为大写

public String toUpperCase(Locale locale)
public String toUpperCase()

(6)去空格

注意:该方法只会去除字符串前后的空格,中间的去不掉。

public String trim()

静态方法

join拼接新字符串

用指定分隔符 delimiter 分割数组或集合的元素,拼接成新的字符串。

public static String join(CharSequence delimiter, CharSequence... elements)
public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

示例:

System.out.println(String.join(":", "aa", "bb", "cc")); // aa:bb:cc
List<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("wang");
list.add("bo");
System.out.println(String.join(" ", list)); //hello world wang bo

format格式化字符串

将对象数组按指定格式转换为字符串。

参考文档:http://www.cnblogs.com/fsjohnhuang/p/4094777.html

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

valueOf转换字符串

将各种数据类型转换为字符串

public static String valueOf(Object obj)
public static String valueOf(char data[])
public static String valueOf(char data[], int offset, int count)
public static String valueOf(boolean b)
public static String valueOf(char c)
public static String valueOf(int i)
public static String valueOf(long l)
public static String valueOf(float f)
public static String valueOf(double d)

copyValueOf拼接字符串

将字符数组中的元素拼接为字符串,可以指定开始索引 offset 和元素总数 count。

public static String copyValueOf(char data[], int offset, int count)
public static String copyValueOf(char data[])

示例:

char[] array = new char[]{'a','b','c','d'};
System.out.println(String.copyValueOf(array));//abcd
System.out.println(String.copyValueOf(array, 2, 1));//c

工具方法

获取子字符串出现的次数

/**
* 获取字符串中子字符串出现的次数
* @param srcStr
* @param str
*/
public static void strNum(String srcStr, String str){
int number = 0;
for (int i = 0; i < srcStr.length(); i++) {
if (srcStr.regionMatches(i, str, 0, str.length())) {
number ++;
}
}
System.out.println("number=" + number);
}

获取子字符串的所有索引

(1)递归实现

/**
* 获取字符串中所有子字符串的索引
* @param srcStr
* @param str
* @param i
*/
public static void strIndex(String srcStr, String str, int i){
int index = srcStr.indexOf(str, i);
if (index != -1) {
System.out.println("index=" + index);
strIndex(srcStr, str, index + str.length());
}
}

(2)while循环实现

/**
* 获取字符串中所有子字符串的索引
* @param srcStr
* @param str
* @param i
*/
public static void strIndex(String srcStr, String str, int i){
while (true) {
int index = srcStr.indexOf(str, i);
if (index == -1) {
return;
}
System.out.println("index=" + index);
i = index + str.length();
}
}

Java 字符串类型常用方法的更多相关文章

  1. java 字符串类型String

    在本质上,字符串实际上一个char类型的数组,由java.lang.String类来表示,该类具有一系列的属性和方法,提供对字符串的一些操作.除此之外,java还提供了StringBuffer类来处理 ...

  2. Java字符串类型详解

    Java 字符串类主要有String.StringBuffer.StringBuilder.StringTokenizer 1.字符串类型底层都是使用char数组进行实现. 2.从jdk1.7以后,S ...

  3. java字符串类型数学运算表达式以及精度丢失问题

    字符串类型数学运算精度丢失问题 方式一:ScriptEngine 会精度丢失,可执行连续双括号 方式二:hutool ScriptUtil 会精度丢失,可执行连续双括号 方式三:hutool Scri ...

  4. java字符串类型和时间类型的转换

    类型转换 //reqeust.getParameter获取字符串直接赋值 1 public static Date date(String date_str) { try { Calendar zca ...

  5. 详解JAVA字符串类型switch的底层原理

    基础 我们现在使用的Java的版本,基本上是都支持String类型的.当然除了String类型,还有int.char.byte.short.enum等等也都是支持的.然而在其底部实现中,还是基于 整型 ...

  6. 详解:Java字符串类型"switch"的底层原理

    前言: 最近更新得会比较频繁,希望大家见谅哦! 也感谢关注我的人,我会更加更加努力去做的! 基础 我们现在使用的Java的版本,基本上是都支持String类型的.当然除了String类型,还有int. ...

  7. 学习笔记——Java字符串操作常用方法

    1.创建字符串 最常用的是使用String类的构造方法:String s=new String("abcd"); 也可采用J2SE5.0添加的StringBuilder类的字符串构 ...

  8. Java字符串的常用方法

    [转换] //int 10进制----> 转16进制Integer.toHexString(10) // int 10进制----> 转8进制Integer.toOctalString(1 ...

  9. java字符串类型常量拼接与变量拼接的区别

    前言 首先看下下面代码结果是什么? package cn.demo_01; public class StringDemo02 { public static void main(String[] a ...

随机推荐

  1. 本学期c#学习总结

    本学期c#学习总结 时间转瞬即逝,大一上半学期的学习生涯已经结束.虽然以前我没什么关于学习计算机的基础,但是经过了这几个月的学习我也还是有点收获的. 我发现c#语言的关键词有很多语言特性和固定的用法, ...

  2. Ubuntu安装pyucharm的专业版本

    看到了不错的教程,亲测有效. https://www.cnblogs.com/huozf/p/9304396.html

  3. 云笔记项目-Java反射知识学习

    在云笔记项目中,补充了部分反射的知识,反射这一部分基础知识非常重要,前面学习的框架Spring和MyBatis读取xml配置文件创建对象,以及JDBC加载驱动等都用了反射,但只知道有这个东西,具体不知 ...

  4. mysql,utf8,utf8mb4

    参考文章 https://www.cnblogs.com/beyang/p/7580814.html https://blog.csdn.net/testcs_dn/article/details/7 ...

  5. Quartz使用

    背景 很多时候,项目需要在不同时刻,执行一个或很多个不同的作业. Windows执行计划这时并不能很好的满足需求了,迫切需要一个更为强大,方便管理,集群部署的作业调度框架. 介绍 Quartz一个开源 ...

  6. 手游开发之lua的table 元表的运用

    元表在项目中的运用,其中就包括元方法这点.元方法是指__index和__newIndex,下面我总结下,更详细的例子讲解可以参考<lua程序设计 第2版>的第13章内容.长h短说,简言之有 ...

  7. 导出word文档 通过DocX组件

    根据DocX官方描述如下: In the application development process, it uses COM libraries and requires MS Word or ...

  8. [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. iview表格高度自适应只需要三步即可

    1. 需要增加到table表格里的 highlight-row :height="tableHeight" ref="table" 2.在return 定义一个 ...

  10. Linux驱动之poll机制的理解与简单使用

    之前在Linux驱动之按键驱动编写(中断方式)中编写的驱动程序,如果没有按键按下.read函数是永远没有返回值的,现在想要做到即使没有按键按下,在一定时间之后也会有返回值.要做到这种功能,可以使用po ...