本文共 4103 字,大约阅读时间需要 13 分钟。
Java中的字符串是对象类型,属于java.lang.String类。与其他语言的基本数据类型不同,字符串在Java中是对象化的类型,需要使用类方法来进行操作。
字符串在Java中必须使用双引号"包裹,例如:
String str = "Hello World";
可以使用String类的构造函数来创建字符串对象:
String(String source):直接复制源字符串内容的新字符串。String(char[] value):使用字符数组初始化字符串。String(char[] value, int offset, int count):从字符数组中截取一部分创建字符串。String(String format, Object... args):用于格式化字符串。new String("abc")创建的字符串会返回两个对象。String类的原始类型进行操作,以防止性能问题。+运算符可以使用+运算符连接多个字符串:
String s1 = new String("Hello");String s2 = new String("World");String result = s1 + s2; // "HelloWorld" 对于长字符串,可以分行书写:
String longStr = new String("这是一\n长字符串,\n包含多行内容。"); 可以将字符串与其他数据类型连接,通过调用toString()方法:
int a = 42;double b = 3.1416;String str = new String("结果:") + a + b; // "结果:42.0" +运算符会生成新的字符串对象,可能导致性能问题。StringBuilder或StringBuffer。使用length()方法:
String str = new String("Hello World");int length = str.length(); // 11 indexOf()和lastIndexOf():返回子字符串的首次和最后一次出现位置。contains():检查字符串是否包含某个字符或子字符串。使用charAt(int index)方法:
String str = new String("abcde");char c = str.charAt(2); // 'c' substring(int beginIndex):从指定位置截取到末尾。substring(int beginIndex, int endIndex):从指定位置截取到指定位置。使用trim()方法去掉前导和尾部空格:
String str = new String(" Hello World ");String trimmedStr = str.trim(); // "Hello World" 使用replace()方法替换字符或子字符串:
String str = new String("Project A");String newStr = str.replace("r", "R"); // "Project A" 使用startsWith()和endsWith()方法:
String str = new String("bcdefg987654321");boolean startsWithB = str.startsWith("bc");boolean endsWith21 = str.endsWith("21"); 使用equals()和equalsIgnoreCase()方法:
String str1 = new String("Hello");String str2 = new String("HELLO");boolean equals = str1.equals(str2); // falseboolean equalsIgnoreCase = str1.equalsIgnoreCase(str2); // true 使用compareTo()方法:
String str1 = new String("abc");String str2 = new String("abd");int compareResult = str1.compareTo(str2); // -1 使用toLowerCase()和toUpperCase()方法:
String str = new String("HeLLo WoRLd");String lowerStr = str.toLowerCase(); // "hello world"String upperStr = str.toUpperCase(); // "HELLO WORLD" 使用split()方法:
split(String regex):按正则表达式分割字符串。split(String regex, int limit):限制分割次数。String ip = new String("192.168.1.1");String[] parts = ip.split("\\.");// parts: ["192", "168", "1", "1"] String str = new String("a,b,c,d");String[] parts = str.split("\\,| ");// parts: ["a", "b", "c", "d"] format()方法String.format(format, args)String.format(format, args)和String.format(Locale locale, format, args)import java.util.Date;public class DateFormatter { public static void main(String[] args) { Date date = new Date(); String formattedDate = String.format("%tF", date); // "2024-05-20" System.out.println(formattedDate); }} public class GeneralFormat { public static void main(String[] args) { String formattedInt = String.format("%d", 1000 / 6); // "166" String formattedBool = String.format("%b", 1 > 2); // "false" String formattedHex = String.format("%x", 1000); // "3e8" System.out.println(formattedInt); // 166 System.out.println(formattedBool); // false System.out.println(formattedHex); // 3e8 }} String emailRegex = "\\w+@\\w+(\\.\\w{2,3})*\\.\\w{2,3}";String email = "19980504@xjbsmail.com";if (email.matches(emailRegex)) { System.out.println("有效邮箱"); // 有效} else { System.out.println("无效邮箱"); // 无效} \\w:表示非空白字符+:表示一次或多次*:表示零次或多次[]:表示字符集合():表示组{}:表示限定修饰符append():追加字符串或任意类型数据。insert(int index, String value):插入字符串。delete(int start, int end):删除子串。toString():将生成器转换为字符串。StringBuilder sb = new StringBuilder("Hello");sb.append(" World!"); // "Hello World!"sb.insert(5, "!!!"); // "Hello!!! World!"sb.delete(4, 7); // "Hell World!"String finalStr = sb.toString(); // "Hell World!" 通过以上方法,可以高效地进行字符串操作,提升代码性能和可读性。
转载地址:http://ughg.baihongyu.com/