java String类

复制代码
// java.lang.String 
        
// charAt() 方法用于返回指定索引处的字符。索引范围为从 0 到 length() - 1。
char charAt = "abc".charAt(1);
System.out.println(charAt); // b

// compareTo() 方法用于比较
int compareTo = "abc".compareTo("abc");
System.out.println(compareTo); // 0

// compareToIgnoreCase() 方法用于按字典顺序比较两个字符串,不考虑大小写。
int compareToIgnoreCase = "abc".compareToIgnoreCase("ABC");
System.out.println(compareToIgnoreCase); // 0

// concat() 方法用于将指定的字符串参数连接到字符串上。
String concat = "abc".concat("def");
System.out.println(concat); // abcdef

// contentEquals() 方法用于将此字符串与指定的 StringBuffer 比较。
StringBuffer sb = new StringBuffer("abc");
boolean contentEquals = "abc".contentEquals(sb);
System.out.println(contentEquals); // true

// copyValueOf() 复制,返回指定数组中表示该字符序列的字符串
String copyValueOf = String.copyValueOf(new char[] { 'h', 'e', 'l', 'l', 'o' });
System.out.println(copyValueOf); // hello

// endsWith() 方法用于测试字符串是否以指定的后缀结束。
boolean endsWith = "abc".endsWith("bc");
System.out.println(endsWith); // true

// equals() 方法用于将字符串与指定的对象比较。
boolean equals = "abc".equals("abc");
System.out.println(equals); // true

// equalsIgnoreCase() 方法用于将字符串与指定的对象比较,不考虑大小写。
boolean equalsIgnoreCase = "abc".equalsIgnoreCase("ABC");
System.out.println(equalsIgnoreCase); // true

// getBytes()  将此 String 编码为 byte 序列
byte[] getBytes = "abc".getBytes();

// getChars() 方法将字符从字符串复制到目标字符数组。
char[] newChar = new char[2];
"abc".getChars(0, 2, newChar, 0);
System.out.print(newChar); // ab

// indexOf() 方法有以下四种形式:
//public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
//int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
String string = "aaa456ac";
//查找指定字符是在字符串中的下标。在则返回所在字符串下标;不在则返回-1.  
System.out.println(string.indexOf("b")); // indexOf(String str); 返回结果:-1,"b"不存在  
// 从第四个字符位置开始往后继续查找,包含当前位置  
System.out.println(string.indexOf("a", 3));//indexOf(String str, int fromIndex); 返回结果:6  
//(与之前的差别:上面的参数是 String 类型,下面的参数是 int 类型)参考数据:a-97,b-98,c-99  
// 从头开始查找是否存在指定的字符  
System.out.println(string.indexOf(99));//indexOf(int ch);返回结果:7  
System.out.println(string.indexOf('c'));//indexOf(int ch);返回结果:7  
//从fromIndex查找ch,这个是字符型变量,不是字符串。字符a对应的数字就是97。  
System.out.println(string.indexOf(97, 3));//indexOf(int ch, int fromIndex); 返回结果:6  
System.out.println(string.indexOf('a', 3));//indexOf(int ch, int fromIndex); 返回结果:6  

// intern() 方法返回字符串对象的规范化表示形式。
// http://www.runoob.com/java/java-string-intern.html

// length() 方法用于返回字符串的长度。
System.out.println("abc".length()); // 3

// matches() 方法用于检测字符串是否匹配给定的正则表达式。
System.out.println("abc".matches("(.*)b(.*)")); // true

// regionMatches() 方法用于检测两个字符串在一个区域内是否相等。
System.out.println("abc".regionMatches(1, "bc", 0, 2)); // true

// replace() 方法通过用 newChar 字符替换字符串中出现的所有 oldChar 字符,并返回替换后的新字符串。
System.out.println("abc".replace('a', 'A')); // Abc

// split() 方法根据匹配给定的正则表达式来拆分字符串。
String[] split = "a,b,c".split(",");
System.out.println(ArrayUtils.toString(split)); // {a,b,c}

// startsWith() 方法用于检测字符串是否以指定的前缀开始。
System.out.println("abc".startsWith("ab")); // true

// subSequence() 方法返回一个新的字符序列,它是此序列的一个子序列。
System.out.println("abc".subSequence(0, 2)); // ab

// toCharArray() 方法将字符串转换为字符数组。
char[] charArray = "abc".toCharArray();
System.out.println(charArray); // abc

// toLowerCase() 方法将字符串转换为小写。
System.out.println("ABC".toLowerCase()); // abc

// toString() 方法返回此对象本身(它已经是一个字符串)。
System.out.println("abc".toString()); // abc

// toUpperCase() 方法将字符串小写字符转换为大写。
System.out.println("abc".toUpperCase()); // ABC

// trim() 方法用于删除字符串的头尾空白符。
System.out.println(" abc ".trim()); // abc

// valueOf(primitive data type x) 返回给定data type类型x参数的字符串表示形式。
System.out.println(String.valueOf((int) 123)); // 123

// 参考:http://www.runoob.com/java/java-string.html
// 中文文档:http://www.runoob.com/manual/jdk1.6/java/lang/String.html
// 官方文档:https://docs.oracle.com/javase/8/docs/api/
复制代码

 

posted @   草木物语  阅读(235)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2017-07-07 java 数组声明定义 数组内存分配 数组初始化 数组引用 数组的遍历
点击右上角即可分享
微信分享提示