String类(java.lang.String)就是Unicode字符序列,例如:"Java\u2122"

3.6.1 Substring 提取子串

String greeting = "Hello";
String s = greeting.substring(0, 3);
s结果为“Hel”

3.6.2 字符串拼接

int age = 13;
String rating = "PG" + age;
rating 结果为 "PG13"

3.6.3 字符串的不可变性

Java中没有一个方法可以直接修改字符串中的某个字符,只能通过 提取子串与拼接 间接实现。例如:

greeting = greeting.substring(0, 3) + "p!";

将greeting 由 “Hello”修改为“help”

3.6.4 判断字符串是否相等

s.equals(t)
"Hello".equals(greeting)
"Hello".equalsIgnoreCase("hello")
equals 用于判断两个字符串的值是否相等 String greeting = "Hello"; //initialize greeting to a string
if (greeting == "Hello") . . .
// probably true
if (greeting.substring(0, 3) == "Hel") . . .
// probably false
== 用于判断两个是否为同一个字符串,即存储地址是否相同

3.6.5 空字符串与Null字符串

空字符串是长度为0的字符串,可用如下方法判定:

if (str.length() == 0)
if (str.equals(""))

空字符串的内容为空,而Null字符串不为空,存储的值是 null

if (str != null && str.length() != 0)

用于判断一个字符串既不是空,又不是null。

3.6.6 字符串的编码点与编码单元 (Code Points and Code Units)

Java 的 String 由 char 序列构成,而 char是UTF-16编码的编码序列,UTF-16是用于表示Unicode 的编码点的。即:编码点是编码字符串的最小单元,一个编码点的内容由UTF-16 表示,也就是char,但是有些Unicode字符由2个(一对)编码单元构成,例如( U+1D546)也就是一个unicode有多个UTF-16,多个char,一个unicode就是一个编码点。

String greeting = "Hello";
int n = greeting.length(); // is 5.

length 返回的是String的编码单元的数量。

如果要得到String只能中字符的真正的数目,也就是编码点的数量,使用下列方法:

int cpCount = greeting.codePointCount(0, greeting.length());
char first = greeting.charAt(0); // first is 'H'
char last = greeting.charAt(4); // last is 'o'

charAt方法返回的也是 编码单元处的内容。

如果要获取第i个编码点,也就是第i个真正的字符,可以使用下列方法

int index = greeting.offsetByCodePoints(0, i);
int cp = greeting.codePointAt(index);

3.6.7 String API

String类有很多方法,重要的如下:

char charAt(int index)
returns the code unit at the specified location.You probably don’t want to call this method unless you are interested in low-level code units.
• int codePointAt(int index) 5.0
returns the code point that starts at the specified location.
• int offsetByCodePoints(int startIndex, int cpCount) 5.0
returns the index of the code point that is cpCount code points away from the code point at startIndex .
• int compareTo(String other)
returns a negative value if the string comes before other in dictionary order, a positive value if the string comes after other in dictionary order, or 0 if the strings are equal.
• IntStream codePoints() 8
returns the code points of this string as a stream. Call toArray to put them in an array.
• new String(int[] codePoints, int offset, int count) 5.0
constructs a string with the count code points in the array starting at offset .
• boolean equals(Object other)
returns true if the string equals other .
• boolean equalsIgnoreCase(String other)
returns true if the string equals other , except for upper/lowercase distinction.
• boolean startsWith(String prefix)
• boolean endsWith(String suffix)
returns true if the string starts or ends with suffix int indexOf(String str)
• int indexOf(String str, int fromIndex)
• int indexOf(int cp)
• int indexOf(int cp, int fromIndex)
returns the start of the first substring equal to the string str or the code point cp ,starting at index 0 or at fromIndex , or -1 if str does not occur in this string.
• int lastIndexOf(String str)
• int lastIndexOf(String str, int fromIndex)
• int lastindexOf(int cp)
• int lastindexOf(int cp, int fromIndex)
returns the start of the last substring equal to the string str or the code point cp ,starting at the end of the string or at fromIndex .
• int length()
returns the number of code units of the string.
• int codePointCount(int startIndex, int endIndex) 5.0
returns the number of code points between startIndex and endIndex - 1 .
• String replace(CharSequence oldString, CharSequence newString)
returns a new string that is obtained by replacing all substrings matching oldString in the string with the string newString .You can supply String or StringBuilder objects
for the CharSequence parameters.
• String substring(int beginIndex)
• String substring(int beginIndex, int endIndex)
returns a new string consisting of all code units from beginIndex until the end of the string or until endIndex - 1 .
• String toLowerCase()
String toUpperCase()
returns a new string containing all characters in the original string, with uppercase characters converted to lowercase, or lowercase characters converted to uppercase.
• String trim()
returns a new string by eliminating all leading and trailing whitespace in the original string.
• String join(CharSequence delimiter, CharSequence... elements) 8 Returns a new string joining all elements with the given delimiter.

3.6.8 在线阅读API文档

Java的文档保存在JDK 安装目录的docs/api/index.html(如果安装的话)

3.6.9 构建String

有时候需要把一些短的字符串构建成一个长的字符串,如果使用连接(+)的方法,费时,非空间,这里提供StringBuilder类(java.lang.StringBuilder)解决此问题。

StringBuilder builder = new StringBuilder();
builder.append(ch); // appends a single character
builder.append(str); // appends a string
String completedString = builder.toString();

CoreJavaE10V1P3.6 第3章 Java的基本编程结构-3.6 字符串 String的更多相关文章

  1. CoreJavaE10V1P3.1 第3章 Java的基本编程结构-3.1 Java 最简程序

    3.1Java最简程序 FirstSample.java public class FirstSample { public static void main(String[] args) { Sys ...

  2. CoreJavaE10V1P3.5 第3章 Java的基本编程结构-3.5 操作符

    最基本的操作为赋值操作,= 即赋值操作符 基本的算术操作为加.减.乘.除取模.除取余数,其对应操作符为 +.-.*./.% 算术操作与赋值操作联合衍生为:+=:-=:*=:/=:%=: 由于处理器硬件 ...

  3. CoreJavaE10V1P3.10 第3章 Java的基本编程结构-3.10 数组(Arrays)

    数组是存储同一类型数据的数据结构 数组的声明与初始化 int[] a; int a[]; int[] a = new int[100]; int[] a = new int[100]; for (in ...

  4. CoreJavaE10V1P3.9 第3章 Java的基本编程结构-3.9 大数值(Big Numbers)

    如果基本的整型与浮点型不能满足需求,可以使用java.Math包提供的 BigInteger 和 BigDecimal 两个类,这两个类可以存储任意长度的数, BigInteger 实现的任意精度整数 ...

  5. CoreJavaE10V1P3.8 第3章 Java的基本编程结构-3.8 控制流程(Control Flow)

    通过使用条件语句.循环语句可以实现流程的控制. 3.8.1 块作用域(Block Scope) 块(Block)就是由一对花括号包围起来的部分.他指定了一个变量的生存范围,与一个方法的操作范围. Ja ...

  6. CoreJavaE10V1P3.7 第3章 Java的基本编程结构-3.7 输入输出(Input ,Output)

    3.7.1 读取输入 Scanner in = new Scanner(System.in); System.out.print("What is your name? "); S ...

  7. CoreJavaE10V1P3.4 第3章 Java的基本编程结构-3.4 变量

    1.在Java中,每一个变量都必须有一个类型,在变量声明是,类型必须在变量名之前.示例如下: double salary; int vacationDays; long earthPopulation ...

  8. CoreJavaE10V1P3.3 第3章 Java的基本编程结构-3.3 数据类型

    3.3 数据类型 这里所说的数据类型是指 Java的8中基本数据类型,是原生就存在的. 不同进制数的字面值表示方法 进制 字面值表示方法 例子 是否默认 JDK版本支持 2进制 0b或0B前缀(每4位 ...

  9. CoreJavaE10V1P3.2 第3章 Java的基本编程结构-3.2 注释

    3.2 注释 1. //形式注释 System.out.println("We will not use 'Hello, World!'"); // is this too cut ...

随机推荐

  1. leetcode Binary Tree Postorder Traversal 二叉树后续遍历

    先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...

  2. Ubuntu中的.bashrc文件

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...

  3. Oracle组函数、多表查询、集合运算、数据库对象(序列、视图、约束、索引、同义词)等

    count组函数:(过滤掉空的字段) select count(address),count(*) from b_user max() avg() min(),sum() select sum(age ...

  4. oracle导入数据

    oracle导入数据时候注意点: 1.imp system/admin@oracle9i file=E:\shujukuwenjian\2014-04-01.dmp fromuser=ptb_supe ...

  5. 《剑指Offer》面试题-用两个栈实现队列

    题目描述: 用两个栈来实现一个队列,完成队列的Push和Pop操作.队列中的元素为int类型. 输入: 每个输入文件包含一个测试样例.对于每个测试样例,第一行输入一个n(1<=n<=100 ...

  6. Jquery框架

    现在Jquery框架对于开发人员基本上是无人不知,无人不晓了,用起来十分的方便,特别是选择器十分强大,提高了我们的开发速度.但是好多人也只是停留在了会用的基础上,我个人觉得会用一个框架不算什么,只能说 ...

  7. MAKE gnu

    GNU Make 学习系列一:怎样写一个简单的Makefile 编程通常遵循一个相当简单的程序:编辑源文件,编译源代码成可执行的格式,调试结果.尽管将源代码翻译成可执行程序是常规的过程,如果做的不正确 ...

  8. iOS内存错误EXC_BAD_ACCESS的解决方法(message sent to deallocated instance)

    iOS开发,最郁闷的莫过于程序毫无征兆地就崩溃了,用bt命令打出调用栈,给出的是一堆系统EXC_BAD_ACCESS的信息,根本没办法定位问题出现在哪里.通常这样的崩溃出现,原因一般就是:调用了已经释 ...

  9. 不用char*作为hash_map的key

    尽量不用char*作为hash_map的key Posted on 2013-09-09 21:21 Springlie 阅读(83) 评论(0) 编辑 收藏 引子: 同事前几天用hash_map时发 ...

  10. SP2-1503: 无法初始化 Oracle 调用界面

    问题描述: win7下,cmd运行 输入sqlplus报一下错误 SP2-1503: 无法初始化 Oracle 调用界面 SP2-0152: ORACLE 不能正常工作 解决办法 1.cmd右键--以 ...