Java 中提供的许多用于在字符串中执行操作的方法称为字符串函数。方法有compare()、concat()、equals()、split()、length()、replace()、compareTo()等。 Java 中的字符串是常量,并使用文字或关键字创建。字符串字面量使 Java 内存变得高效,并且该关键字在普通内存中创建 Java 字符串。字符串表示字符值数组,该类由Serialized、Comparable、CharSequence接口等三个接口实现。它以序列化或可比较的方式表示字符序列。
广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试以下是java中字符串函数的主要概念:
在 Java 中,创建 String 对象有两种主要方法:
使用字符串文字: 双引号用于在 Java 中生成字符串文字。
示例:
String s= "Hello World!";
使用new关键字:可以使用“new”创建Java字符串。
示例:
String s=new String ("Hello World!");
用于获取对象信息的方法在 Java 中称为访问器方法。一种与字符串相关的访问器方法是 length() 方法。这将返回字符串对象中的字符数。
public class Exercise { public static void main(String args[]){ String s1="Hello"; String s2="World"; System.out.println("string length is: "+s1.length()); System.out.println("string length is: "+s2.length()); }}
输出:
该方法返回一个新字符串,即 string1 和 string2 组合在末尾。 Concat() 方法可以与字符串文字一起使用来完成此操作。字符串通常也使用 + 运算符连接。
public class ExerciseNew { public static void main(String args[]){ String s1="Hello"; s1=s1.concat("What is your good name?"); System.out.println(s1); }}
输出:
printf() 和 format() 函数输出格式化的数字。字符串有一个类似的类方法,名为format()。它产生一个 String 对象。与一次性打印命令相比,String 对象中可访问的静态 format() 方法允许构造可重复使用的格式化字符串。
以下是不同的方法:
Method | Description |
char charAt(int index) | It returns the char value of the particular index as mentioned. |
int length() | It returns the length of the string |
static String format(String format, Object… args) | It returns a string that is duly formatted. |
static String format(Locale l, String format, Object… args) | It returns a formatted string along with the given locale. |
String substring(int beginIndex) | It returns the substring, which starts from begin index. |
String substring(int beginIndex, int endIndex) | It returns a substring for a given start index position and ends index. |
boolean contains(CharSequence s) | It returns true or false after doing a match between the sequence of char values. |
static String join(CharSequence delimiter, CharSequence… elements) | It returns a string that is joined |
static String join(CharSequence delimiter, Iterable extends CharSequence> elements) | It returns a joined string, the same as above. |
boolean equals(Object another) | It checks the equality of the string. It does so with the given object. |
boolean isEmpty() | It checks if a given string is empty or not. |
String concat(String str) | It concatenates the specified string like the above example. |
String replace(char old, char new) | It replaces all occurrences of the specified old char value. With new value. |
String replace(CharSequence old, CharSequence new) | It replaces all occurrences of the given specified CharSequence with the new one. |
static String equalsIgnoreCase(String another) | It compares with another string, but It is not case-sensitive. |
String[] split(String regex) | It returns a split string based on matching the regex. |
String[] split(String regex, int limit) | It returns a split string that matches regex and limit. |
String intern() | It returns a string that is interned. |
int indexOf(int ch) | It returns the selected char value index. |
int indexOf(int ch, int fromIndex) | It returns the specified char value index, which starts with a given index. |
int indexOf(String substring) | It returns the selected substring index. |
int indexOf(String substring, int fromIndex) | It returns the selected substring index, which starts with a given index. |
String toLowerCase() | It returns a string with all chars in lowercase. |
String toLowerCase(Locale l) | It returns a string in lowercase with a specified locale. |
String toUpperCase() | It returns a string with all chars in uppercase. |
String toUpperCase(Locale l) | Same as above but with a specified locale. |
String trim() | It removes the starting and ending whitespaces of this string. |
static String valueOf(int value) | It converts another data type into a string. It is called an overloaded method. |
In this section, we have discussed some examples of string functions in Java.
Code:
public class IsEmptyExercise{ public static void main(String args[]){ String s1=""; String s2="Hello"; System.out.println(s1.isEmpty()); // true System.out.println(s2.isEmpty()); // false }}
Output:
Code:
public class StringTrimExercise{ public static void main(String args[]){ String s1=" HelloWorld "; System.out.println(s1+"How are you doing today"); // without trim() System.out.println(s1.trim()+"How are you doing today"); // with trim() }}
Output:
Code:
public class StringLowerExercise{ public static void main(String args[]){ String s1="HELLO HOW Are You TODAY?"; String s1lower=s1.toLowerCase(); System.out.println(s1lower);} }
Output:
Code:
public class ReplaceExercise{ public static void main(String args[]){ String s1="hello how are you today"; String replaceString=s1.replace('h','t'); System.out.println(replaceString); }}
Output:
Code:
public class EqualsExercise{ public static void main(String args[]){ String s1="Hi"; String s2="Hey"; String s3="Hello"; System.out.println(s1.equalsIgnoreCase(s2)); // returns true System.out.println(s1.equalsIgnoreCase(s3)); // returns false } }
Output:
Apart from the above-mentioned characteristics, functions, and methods, there are other facts with the String class. The string class is a final class, which is why String class objects are immutable in nature. JVM reserves a special memory area for string classes; this area is called the String constant pool.
In the String library, available with Java. Lang, overriding the String references are possible, but the content or literals cannot be copied. Any number closed in double quotes is also treated as a string.
Students should test these codes in an IDE and modify them to enhance their understanding further. String manipulation is very important to know in any programming language, and developers use it daily.
以上是Java 中的字符串函数的详细内容。更多信息请关注PHP中文网其他相关文章!