Java8 API学习2 - java.lang.CharSequence, java.lang.String
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...)
- 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()
- 不太常用, 但是还算容易理解, 略
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)
- 参数为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)
- 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>
Java8 API学习2 - java.lang.CharSequence, java.lang.String的更多相关文章
- Java数据持久层框架 MyBatis之API学习四(xml配置文件详解)
摘录网址: http://blog.csdn.net/u010107350/article/details/51292500 对于MyBatis的学习而言,最好去MyBatis的官方文档:http:/ ...
- Java中的java.lang.Class API 详解
且将新火试新茶,诗酒趁年华. 概述 Class是一个位于java.lang包下面的一个类,在Java中每个类实例都有对应的Class对象.类对象是由Java虚拟机(JVM)自动构造的. Class类的 ...
- Json for Java API学习
首先声明:本文来个非常多网友的博客,我通过參考了他们的博客,大致的了解了一些项目中经常使用的Json in java 类和方法,以及关于json的个人理解 个人对json的一些简单理解 在近期的学习中 ...
- java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: scala/runtime/java8/JFunction1$mcII$sp
报错信息: Exception in thread "main" java.lang.BootstrapMethodError: java.lang.NoClassDefFound ...
- 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 ...
- Java数据持久层框架 MyBatis之API学习六(Mapper XML 文件详解)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- Java数据持久层框架 MyBatis之API学习一(简介)
对于MyBatis的学习而言,最好去MyBatis的官方文档:http://www.mybatis.org/mybatis-3/zh/index.html 对于语言的学习而言,马上上手去编程,多多练习 ...
- 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>, 原因为 ...
- java.time包常用类API学习记录
Java8出来已那么多年了,java.time包之前一直没有使用过,最近正好有用到,在此做个记录. 上图列出了java.time包下的类,接下来我们详细看下其中每个类的用法. Clock:获取到当前时 ...
随机推荐
- Nginx教程(三) Nginx日志管理 (转)
Nginx教程(三) Nginx日志管理 1 日志管理 1.1 Nginx日志描述 通过访问日志,你可以得到用户地域来源.跳转来源.使用终端.某个URL访问量等相关信息:通过错误日志,你可以得到系统某 ...
- PLAY2.6-SCALA(九) WebSockets
WebSockets是一种支持全双工通信的套接字.现代的html5通过js api使得浏览器天生支持webSocket.但是Websockets在移动端以及服务器之间的通信也非常有用,在这些情况下可以 ...
- LeedCode OJ -- String to Integer (atoi)
点击打开题目链接 题目意思就是自己实现一个atoi函数,也就是将字符串转换成int型. 关于INT_MAX和INT_MIN, 只是在<limits.h>文件中定义的宏..分别是int型可以 ...
- Python之路,Day1 - Python基础1 --转自金角大王
本节内容 Python介绍 发展史 Python 2 or 3? 安装 Hello World程序 变量 用户输入 模块初识 .pyc是个什么鬼? 数据类型初识 数据运算 表达式if ...else语 ...
- BZOJ 1500 洛谷2042维护序列题解
BZ链接 洛谷链接 这道题真是丧心病狂.... 应该很容易就可以看出做法,但是写代码写的....... 思路很简单,用一个平衡树维护一下所有的操作就好了,重点讲解一下代码的细节 首先如果按照常规写法的 ...
- 【NS2】添加mUDP、mUdpSink和mTcpSink模块
根据柯老师的教材可知,mUDP是UDP的延伸,除了具有UDP的功能外,还能记录所发送的包的信息.mUdpSink可以把接收到的包的信息记录 到文件中.mTcpSink是TCPsink的延伸,除了具有T ...
- Ubuntu修改root密码,ssh 允许root用户登录
1,切换为root用户 2,passwd root(or others) 3,输两次密码 4,重启. ssh允许root用户登录: 1,vim /etc/ssh/sshd_config 2,修改Per ...
- @atcoder - CODE FESTIVAL 2017 Final - J@ Tree MST
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定 N 个点,第 i 点有一个点权 Xi,再给定一棵边带权的树 ...
- LeetCode74 Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Libev源码分析08:Libev中的信号监视器
Libev中的信号监视器,用于监控信号的发生,因信号是异步的,所以Libev的处理方式是尽量的将异步信号同步化.异步信号的同步化方法主要有:signalfd.eventfd.pipe.sigwaiti ...