您的位置:首页 > 房产 > 家装 > String常用方法详解

String常用方法详解

2024/10/6 18:26:15 来源:https://blog.csdn.net/Lwehne/article/details/139718744  浏览:    关键词:String常用方法详解

 


        @auth:别晃我的可乐

        @date:2024年06月16日


比较大小

  • equals(Object obj): 用于比较字符串内容是否相等。
  • compareTo(String anotherString): 按字典顺序比较两个字符串。

String str1 = "hello";
String str2 = "world";boolean isEqual = str1.equals(str2);
int comparison = str1.compareTo(str2);

翻转

  • StringBuilder.reverse(): 可以将字符串翻转。

String str = "hello";
StringBuilder reversed = new StringBuilder(str).reverse();
String reversedStr = reversed.toString();

截取

  • substring(int beginIndex): 返回从beginIndex开始到字符串末尾的子字符串。
  • substring(int beginIndex, int endIndex): 返回从beginIndex开始到endIndex结束的子字符串(不包括endIndex位置的字符)。

String str = "hello world";
String substr1 = str.substring(6); // "world"
String substr2 = str.substring(0, 5); // "hello"

转换大小写

  • toUpperCase(): 将字符串转换为大写形式。
  • toLowerCase(): 将字符串转换为小写形式。

String str = "Hello World";
String upperCase = str.toUpperCase(); // "HELLO WORLD"
String lowerCase = str.toLowerCase(); // "hello world"

去除空格

  • trim(): 去除字符串两端的空格。

String str = "  hello world  ";
String trimmed = str.trim(); // "hello world"

替换子串

  • replace(char oldChar, char newChar): 将字符串中的oldChar替换为newChar。
  • replace(CharSequence target, CharSequence replacement): 将字符串中的target子串替换为replacement。

String str = "hello world";
String replaced1 = str.replace('o', '0'); // "hell0 w0rld"
String replaced2 = str.replace("world", "Java"); // "hello Java"

查找子串

  • indexOf(String str): 返回第一次出现指定子字符串的索引。
  • lastIndexOf(String str): 返回最后一次出现指定子字符串的索引。

String str = "hello world";
int index1 = str.indexOf("l"); // 2
int index2 = str.lastIndexOf("l"); // 9

拼接字符串

  • concat(String str): 将指定字符串连接到此字符串的末尾。

String str1 = "Hel";
String str2 = "lo";
String combined = str1.concat(" ").concat(str2); // "Hello"

  • "+"操作符: 也可以用"+"操作符来进行字符串拼接。

String str1 = "Hello";
String str2 = "World";
String combined = str1 + " " + str2; // "Hello World"

判断是否包含子串

  • contains(CharSequence s): 判断字符串是否包含指定的字符序列。

String str = "hello world";
boolean contains = str.contains("world"); // true

切割字符串

  • split(String regex): 根据给定正则表达式将字符串拆分为子字符串数组。

String str = "apple,banana,orange";
String[] fruits = str.split(",");
// fruits数组: ["apple", "banana", "orange"]

格式化字符串

  • format(String format, Object... args): 使用指定的格式字符串和参数返回格式化的字符串。

String formatted = String.format("The value of %s is %d", "x", 5);
// "The value of x is 5"

获取字符串长度

  • length(): 返回字符串的长度。

String str = "hello";
int length = str.length(); // 5

判断字符串是否为空

  • isEmpty(): 判断字符串是否为空(长度为0)。

String str = "";
boolean isEmpty = str.isEmpty(); // true

字符串转换为字符数组

  • toCharArray(): 将字符串转换为字符数组。

String str = "hello";
char[] charArray = str.toCharArray(); // ['h', 'e', 'l', 'l', 'o']

查询指定索引位的字符

  • charAt(int index): 用于返回指定索引处的字符。index:要返回的字符的索引,范围从 0 到 length() - 1。

String str = "Hello";
char result = str.charAt(1);
System.out.println(result); // 输出 'e'

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com