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. C++11中的并发

    在 C++98 的时代,C++标准并没有包含多线程的支持,人们只能直接调用操作系统提供的 SDK API 来编写多线程程序,不同的操作系统提供的 SDK API 以及线程控制能力不尽相同.到了 C++ ...

  2. op应用:官方,wifidog,portal,uci,luci,脚本,框架,usb

    http://wiki.openwrt.org/doc/starthttp://downloads.openwrt.org/docs/buildroot-documentation.htmlhttp: ...

  3. fedora下eclipse安装tomcat插件

    首先下载tomcat插件: http://www.eclipsetotale.com/tomcatPlugin.html,下载最新的3.3版本: 由于我的eclipse是通过yum自动安装的,因此ec ...

  4. @bzoj - 4951@ [Wf2017]Money for Nothing

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 在这道题种你需要解决一个全世界人类从存在起就在面临的最深刻的问题 ...

  5. PLAY2.6-SCALA(四) 请求体解析器

    一个http请求是一个请求头后面跟着一个请求体,头部信息比较短,可以安全的缓存在内存中,在Play中头部信息使用RequestHeader类进行建模.请求体的内容可能较大,使用流stream的形式进行 ...

  6. Hdu 1729 Nim博弈

    点击打开题目链接 之前没做过这题,因为学弟问到我如果来求该题的sg值,才做了这题. 首先, 是多堆Nim博弈毫无疑问,这题是往一个有固定容量的箱子里放石子,和从一堆石子里面拿出石子是一个道理. 和传统 ...

  7. 洛谷P3324 [SDOI2015]星际战争

    题目:洛谷P3324 [SDOI2015]星际战争 思路: 类似<导弹防御塔>,因为题目保证有解,花费时间小于最终答案时一定无法消灭所有敌人,只要花费时间大于等于最终答案都可以消灭所有敌人 ...

  8. 2019.8.3 [HZOI]NOIP模拟测试12 C. 分组

    2019.8.3 [HZOI]NOIP模拟测试12 C. 分组 全场比赛题解:https://pan.baidu.com/s/1eSAMuXk 刚看这题觉得很难,于是数据点分治 k只有1和2两种,分别 ...

  9. 【数据库】sql2008卸载和默认实例的删除 标签: 数据库 2014-11-16 15:15 5878人阅读 评论(30)

    在安装sql2008的时候,会碰到这一步,要求创建实例,可以选择默认实例和命名实例,如果是第一次安装的话,可以选择默认实例,但是如果是第二次甚至更多次安装的 话,很多时候会出现不能用默认实例,只能自己 ...

  10. 微信小程序记录

    1.vs code 可以安装 Vetur-wepy 对代码高亮的提示. 2.取消swiper组件的手动滑动效果 在 swiper-item 中添加 catchtouchmove='catchTouch ...