String类中常用的方法
@Test
public void demo(){ // 以下为String中的常用的方法及注释, 最常用的注释前有**标注
String s = "abcdefg123456"; // 返回下标对应的ASCII码
int codePointAt = s.codePointAt(7);
// **返回字符串s和字符串Q的,第一个不相等字符的ASCII码差值,如果查找结束发现都相等,则返回两个字符串的长度差值
int compareTo = s.compareTo("Q");
// 返回下标前一位对应的ASCII码
int codePointBefore = s.codePointBefore(7);
// 返回代码点长度
int codePointCount = s.codePointCount(4, 7);
// 返回从index=4处偏移到codePointOffSet=7之后的索引
int offsetByCodePoints = s.offsetByCodePoints(4, 7);
// 将字符串变成字节数组
byte[] getBytes = s.getBytes();
// **将字符串变成字符数组
char[] toCharArray = s.toCharArray();
// **返回指定索引出的字符
char charAt = s.charAt(7);
// **是否包含字符串"contains"
boolean contains = s.contains("contains");
// **将字符串s中的c全部替换为C
String replaceAll = s.replaceAll("c", "C");
// 将字符串中的1替换为一
String replace = s.replace("1", "一");
// **截取字符串s中的索引从0到3的字符
String substring = s.substring(0, 3);
// 截取字符串s中索引从4开始的字符
String substring1 = s.substring(4);
// 字符串s是否以6结尾
boolean endsWith = s.endsWith("6");
// 字符串是否以a开头
boolean startsWith = s.startsWith("compareTo");
// **将字符串s以字符c进行分割
String[] split = s.split("c");
// 将字符串s以字符c进行分割,分割成3份(如果最大能够分割到3份的话,如果最大不能分割到3份,则默认分割到最大份数)
String[] split1 = s.split("c", 3);
// 将字符串7890拼接到字符串s的后面,与+拼接字符串的区别是,+号左右的字符串都可以是null,而concat左右都不允许,否则会报空指针异常
String concat = s.concat("7890");
// 返回字符串是否相等,与equals区别,equals仅限于两个String类型字符串进行比较,而contentEquals可以比较类型为CharSequence的,
// 而且如果比较的是StringBuffer还会加锁
boolean contentEquals = s.contentEquals("contentEquals");
// 比较两个字符串是否相等(忽略大小写),
boolean equalsIgnoreCase = s.equalsIgnoreCase("Abcdefg123456");
// **比较两个字符串是否相等(不忽略大小写)
boolean equals = s.equals("contentEquals");
// 判断字符串是否为空
boolean empty = s.isEmpty();
// **获取字符串的长度
int length = s.length();
// **返回指定字符在字符串s中的索引,如果找不到返回-1
int indexOf = s.indexOf("一");
// 将字符串全部转换为小写
String toLowerCase = s.toLowerCase();
// 将字符串全部转换为大写
String toUpperCase = s.toUpperCase();
// 去除字符串前后的空格
String trim = " abc ".trim();
// 将指定数量的字符串(s,7890,AAA)以固定格式(,)进行拼接
String join = String.join(",",s, "7890", "AAA"); System.out.println("codePointAt = " + codePointAt);
System.out.println("compareTo = " + compareTo);
System.out.println("codePointBefore = " + codePointBefore);
System.out.println("codePointCount = " + codePointCount);
System.out.println("offsetByCodePoints = " + offsetByCodePoints);
System.out.println("getBytes = " + Arrays.toString(getBytes));
System.out.println("toCharArray = " + Arrays.toString(toCharArray));
System.out.println("charAt = " + charAt);
System.out.println("contains = " + contains);
System.out.println("replaceAll = " + replaceAll);
System.out.println("replace = " + replace);
System.out.println("substring = " + substring);
System.out.println("substring1 = " + substring1);
System.out.println("endsWith = " + endsWith);
System.out.println("startsWith = " + startsWith);
System.out.println("split = " + Arrays.toString(split));
System.out.println("split1 = " + Arrays.toString(split1));
System.out.println("concat = " + concat);
System.out.println("contentEquals = " + contentEquals);
System.out.println("equalsIgnoreCase = " + equalsIgnoreCase);
System.out.println("indexOf = " + indexOf);
System.out.println("toLowerCase = " + toLowerCase);
System.out.println("toUpperCase = " + toUpperCase);
System.out.println("join = " + join);
System.out.println("equals = " + equals);
System.out.println("empty = " + empty);
System.out.println("length = " + length);
System.out.println("trim = " + trim);
}
上述打印结果为: codePointAt = 49
compareTo = 16
codePointBefore = 103
codePointCount = 3
offsetByCodePoints = 11
getBytes = [97, 98, 99, 100, 101, 102, 103, 49, 50, 51, 52, 53, 54]
toCharArray = [a, b, c, d, e, f, g, 1, 2, 3, 4, 5, 6]
charAt = 1
contains = false
replaceAll = abCdefg123456
replace = abcdefg一23456
substring = abc
substring1 = efg123456
endsWith = true
startsWith = false
split = [ab, defg123456]
split1 = [ab, defg123456]
concat = abcdefg1234567890
contentEquals = false
equalsIgnoreCase = true
indexOf = -1
toLowerCase = abcdefg123456
toUpperCase = ABCDEFG123456
join = abcdefg123456,7890,AAA
equals = false
empty = false
length = 13
trim = abc
以上为个人总结记录使用,希望能够帮助其他人,如有不足或错误欢迎大家指正。
String类中常用的方法的更多相关文章
- String类中常用的方法(重要)
1.字符串与字节 public String(byte[] byte); 将全部字节变成字符串 public String (byte[] byte,int offset,int length) 将部 ...
- Java中String类中常用的方法
1.字符串与字符数组的转换 用toCharArray()方法将字符串变为字符数组 String str = "abcdef"; char c[] = str.tocharArray ...
- 27 string类中常用的方法列表
1. 获取方法 int length() 获取字符串的长度 char charAt(int index) 获取特定位置的字符 (角标越界) int indexOf(String str) 获取 ...
- 2017.12.11 String 类中常用的方法
1.编写程序将 "jdk" 全部变为大写,并输出到屏幕,截取子串"DK" 并输出到屏幕 package demo; import java.util.Scann ...
- Java String类中的intern()方法
今天在看一本书的时候注意到一个String的intern()方法,平常没用过,只是见过这个方法,也没去仔细看过这个方法.所以今天看了一下.个人觉得给String类中加入这个方法可能是为了提升一点点性能 ...
- String对象中常用的方法
String对象中常用的方法 1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码.strObj.charCodeAt(index)说明:index将被处理字符的从零开始 ...
- Java——String类中的compareTo方法总结
String类的定义: java.lang 类 String java.lang.Object java.lang.String 所有已实现的接口:Serializable, C ...
- String类中的equals()方法:
String类中的equals()方法: public boolean equals(Object anObject) { //如果是同一个对象 if (this == anObject) { ret ...
- Java用代码演示String类中的以下方法的用法
用代码演示String类中的以下方法的用法 (1)boolean isEmpty(): 判断字符串是不是空串,如果是空的就返回true (2)char charAt(int index): 返回索引上 ...
随机推荐
- 第三十三章 System V共享内存与信号量综合
用信号量解决生产者.消费者问题 实现shmfifo ip.h #ifndef _IPC_H #define _IPC_H #include <unistd.h> #include < ...
- 27 个问题突破所有重难点,BroadcastReceiver 、ContentProvider 知多少?「建议收藏」
前言 距离上次更新过去一周多了,打破了之前两到三天一更的惯例,主要还是这部分内容太多了. 原先想把 BroadcastReceiver .ContentProvider 分两篇来总结,但的确,这两大组 ...
- 微服务SpringCloud之GateWay服务化和过滤器
Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spring Cloud Gateway 注册到服务中心,Spring Cloud Gateway 默认就会代理服务中心的所 ...
- CSPS模拟 48
??? 分数越来越低??? T1 String Master 题目过于毒瘤,以至于我都不想改 T2 Tourist Attractions 稍微转化题意是求无向图的三角形个数 由于坚信bitset不是 ...
- 分布式系统中session一致性问题
业务场景 在单机系统中,用户登陆之后,服务端会保存用户的会话信息,只要用户不退出重新登陆,在一段时间内用户可以一直访问该网站,无需重复登陆.用户的信息存在服务端的 session 中,session中 ...
- Unicode字符串和非Unicode字符串
什么是Unicode? Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每 ...
- OpenCV的Mat构造函数
1.函数说明 构造函数:public Mat(int rows, int cols, MatType type, IntPtr data, long step = 0) 可以通过数据指针构造Mat对象 ...
- 易初大数据 2019年11月8日 linux 王庆超
◆linux系统的特点: (1)完全免费. (2)高效.安全.稳定. (3)支持多种硬件平台. (4)友好的用户界面. (5)强大的网络功能. (6)支持多任务.多用户. ◆linux体系: 1.li ...
- pscp命令详解
注意:只能在winds下执行 环境准备 1.先下载pscp软件,这里是我的云盘地址: 链接:https://pan.baidu.com/s/1mkzRMv-aosC94KbMcMea9w 提取码:k0 ...
- 设计模式(Java语言)-单例模式
单例模式,简而言之就是在整个应用程序里面有且仅有一个实例,在程序的任何时候,任何地方获取到的该对象都是同一个对象.单例模式解决了一个全局的类被频繁创建和销毁的,或者每次创建或销毁都需要消耗大量cpu资 ...