CharSequence

public interface CharSequence

字符序列接口, 实现此接口的非抽象类有String, StringBuffer, StringBuilder. 从结果来讲, 该接口的主要作用就是使得三者之间在进行一部分操作(比如查找, 比较)时变得容易.

概念说明

主要是char value和codepoint value的意义及区别, surrogate code point的含义. 简单地说char代表一个2字节的字符, codepoint才代表一个完整的字符(因为少数字符是4字节的, 比如emoji), 具体请参考其他人的文章.

CharSequence定义的方法

char charAt(int)
int length()
CharSequence subSequence(int, int)
String toString() //其实重定义toString没有用, 因为Object类已经实现了这个方法, 就当做这里是一种强调吧

java8新增2个default方法

public default IntStream chars()
public default IntStream codePoints()

分别转换为以char和codepoint为单位的java stream.注意转换后为int类型的unicode编码.

String

public final class String
implements java.io.Serializable, Comparable<String>, CharSequence

这是一个final class.

其中, Serializable 是空接口; Comparable<String>函数式接口, 其方法为int compareTo(T to).

String的构造函数 - 13个

1.以下几个容易理解(6个)

String()
String(byte[])
String(char[])
String(String)
String(StringBuilder)
String(StringBuffer)

2.从数组中截取字符(2个)

String(byte bytes[], int offset, int length)
String(char value[], int offset, int count)

3.从非Unicode编码的字节流中截取字符串, 最后一个参数为编码类型(4个)

String(byte[], String)
String(byte[], Charset)
String(byte[] bytes, int offset, int length, String charsetName)
String(byte[] bytes, int offset, int length, Charset charset)

4.从int类型的codepoint数组转化为String

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

String的静态方法

1.生成新String, 实际是调用了构造函数String(char[], int, int)

String copyValueOf(char[])
String copyValueOf(char[], int, int)
String valueOf(char[])
String valueOf(char[], int, int)

2.format, 类似于sprintf

String format(String, Object...)
String format(Locale, String, Object...)
  1. join, 可以认为split的逆操作吗
String join(CharSequence, CharSequence...) //或者是数组CharSequence[]
String join(CharSequence, Iterable<? extends CharSequence)

4.valueOf转换, 实际是调用了Integer.toString()等方法

String valueOf(char)
String valueOf(double)
String valueOf(float)
String valueOf(int)
String valueOf(Object obj) //=>obj.toString()
String valueOf(long)

String的对象方法

(除非特别说明, 否则该方法不支持正则)

1.常用, 略

char charAt(int)

boolean isEmpty()

boolean equals(Object) //override

boolean equalsIgnoreCase(String) int indexOf(int ch) //=> indexOf(ch, 0), *用int表示ch可能是因为考虑到了codepoint吧

int indexOf(int ch, int fromIndex)

int indexOf(String str) //=>indexOf(str, 0)

int indexOf(String str, int fromIndex) int lastIndexOf(int)

int lastIndexOf(int, int)

int lastIndexOf(String)

int lastIndexOf(String, int) int length() String concat(String)

String trim()

  1. 不太常用, 但是还算容易理解, 略
boolean startWith(String prefix) //=>startsWith(prefix, 0)
boolean startsWith(Stirng prefix, int offset)
boolean endsWith(String) //note: no overloads
int hashCode() //override Object#hashCode

3.compareTo, 注意其返回值的含义:

(1) this.charAt(k)-anotherString.charAt(k)

(2) this.length()-anotherString.length()

int CompareTo(String)
int CompareToIgnoreCase(String)

4.*codepoint相关, 这里还是不太明白

//这两个见CharSequence接口说明
IntStream chars()
IntStream codePoints() int codePointAt(int index)

int codePointBefore(int)

int codePointCount(int beginIndex, int endIndex)

int offsetByCodePoints(int index, int codePointOffset)

  1. 参数为CharSequence, 表明可以和StringBuffer等类型直接比较

    (还有replace(), 未列出, 见10.)
boolean contains(CharSequence)
boolean contentEquals(CharSequence)

6.get/set/to

(String是常量字符串当然没有set...)

byte[] getBytes()
byte[] getBytes(String) //设置编码
byte[] getBytes(Charset)
void getChars(int srcBegin, int srcEnd, char[] dst[], int dstBegin) char[] toCharArray()

String toLowerCase()

String toLowerCase(Locale) //设置Locale, 使用其他语言的大小写转换规则

String toUpperCase()

String toUpperCase(Locale)

7.intern

native String intern() //*可能是从字符串池中寻找是否有相同的字符串然后修改引用指向该字符串

8.正则表达式相关, 可以从参数名确定

(还有replaceAll()replaceFirst(), 未列出, 见10.)

boolean matches(String)
String[] split(String)
String[] split(String regex, int limit) //最多切分limit次

9.*不太理解...

boolean regionMatches(boolean, int, String, int, int)
boolean regionMatches(int, Stirng, int, int)
  1. replace
String replace(char, char)
String replace(CharSequence, CharSequence)
String replaceAll(String, String) //支持正则
String replaceFirst(String, String) //支持正则

11.substring

条件: 0<=beginIndex<=endIndex<=length, 注意和JavaScript或python的类似函数区分

CharSequence subSequence(int, int) //只是返回值不同
String substring(int beginIndex, int endIndex)
String substring(int beginIndex) //=>substring(beginIndex, this.length() - beginindex)
      </div>
</div>
posted @
2019-06-12 15:33 
星朝 
阅读(...) 
评论(...) 
编辑 
收藏

Java8 API学习2 - java.lang.CharSequence, java.lang.String的更多相关文章

  1. Java数据持久层框架 MyBatis之API学习四(xml配置文件详解)

    摘录网址: http://blog.csdn.net/u010107350/article/details/51292500 对于MyBatis的学习而言,最好去MyBatis的官方文档:http:/ ...

  2. Java中的java.lang.Class API 详解

    且将新火试新茶,诗酒趁年华. 概述 Class是一个位于java.lang包下面的一个类,在Java中每个类实例都有对应的Class对象.类对象是由Java虚拟机(JVM)自动构造的. Class类的 ...

  3. Json for Java API学习

    首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...

  4. java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1$mcII$sp

    报错信息: Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFound ...

  5. Java MyEclipse:The type java.lang.CharSequence cannot be resolved. It is indirectly referen

    从svn上下载项目后配置weblogic后启动报错: myeclipse  The type java.lang.CharSequence cannot be resolved. It is indi ...

  6. Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  7. Java数据持久层框架 MyBatis之API学习一(简介)

    对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...

  8. sprinbcloud学习之-Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>

    日志报错,提示Failed to bind properties under 'logging.level' to java.util.Map<java.lang.String>, 原因为 ...

  9. java.time包常用类API学习记录

    Java8出来已那么多年了,java.time包之前一直没有使用过,最近正好有用到,在此做个记录. 上图列出了java.time包下的类,接下来我们详细看下其中每个类的用法. Clock:获取到当前时 ...

随机推荐

  1. 【水滴石穿】mobx-todos

    我觉得代码在有些程序员手里,就好像是画笔,可以创造很多东西 不要觉得创意少就叫没有创意,每天进步一点点,世界更美好 首先源码地址为:https://github.com/byk04712/mobx-t ...

  2. ubuntu上制作应用程序的快捷图标启动

    最近在研究Go语言,对比了几种流行的IDE,发现GoLand是使用体验最好的,没有之一.这也印证了网友们常说的那句话“JetBrain出品,必属精品”. 在ubuntu环境下使用GoLand,直接到J ...

  3. 微信端的user-Agent

    在iPhone下,返回 Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Ge ...

  4. JMeter 参数意义

    样本数目:运行时得到的取样器响应结果个数 最新样本:最近一个取样器结果的响应时间 平均:所有取样器结果的响应时间平均值 偏离:所有取样器结果的响应时间标准差 吞吐量:每分钟响应的取样器结果个数 中值: ...

  5. Otracle数据库定时任务--dbms_job

    一.dbms_job涉及到的知识点 1.创建job: variable jobno number; dbms_job.submit(:jobno, --job号 'your_procedure;'-- ...

  6. springboot对shiro进行mock单元测试

    环境:junit-5.Spring5.0.x.Spring Boot 2.0.x 以下是用来权限测试的接口: @ApiOperation("[可接入]分页查询管理员") @ApiR ...

  7. 2019-4-7-VisualStudio-解决方案筛选器-slnf-文件

    title author date CreateTime categories VisualStudio 解决方案筛选器 slnf 文件 lindexi 2019-04-07 11:34:59 +08 ...

  8. HDU 5672 String【尺取法】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...

  9. android 数据存储----android短信发送器之文件的读写(手机+SD卡)

    本文实践知识点有有三: 1.布局文件,android布局有相对布局,线性布局,绝对布局,表格布局,标签布局等.各个布局能够嵌套的.本文的布局文件就是线性布局的嵌套 <LinearLayout x ...

  10. python print函数