Java中的字符串操作(String类的函数/方法)

String is a class in java, which provides some of the predefined methods that make string based problem solutions easier. We don’t need to write code for every operation, we have to just use its methods.

String是Java中的类,它提供一些预定义的方法,这些方法使基于字符串的问题解决方案更加容易。 我们不需要为每个操作编写代码,我们只需使用其方法即可。

In this post, we are going to learn some of the most useful methods of the string.

在本文中,我们将学习一些最有用的字符串方法。

声明和分配值给字符串对象 (Declaring and assigning value to string objet)

1 ) Declaration and assignment (separate statements)

1)声明和赋值(单独的语句)

    String msg;	//declaration
    msg = "Hello world"; // assignment

2 ) Declaration with initialization

2)初始化声明

    String msg = "Hello world";

Java String类方法 (Java String class methods)

1)s1.equals(s2) (1) s1.equals(s2))

This function is used to compare two strings; it returns boolean values ‘true’/ ‘false’. If s1 and s2 are exactly same it returns ‘true’ otherwise it returns ‘false’.

该函数用于比较两个字符串。 它返回布尔值'true'/'false'。 如果s1和s2完全相同,则返回“ true”,否则返回“ false”。

Example:

例:

    s1 = "Hello world"
    s2 = "Hello world"

Both strings are exactly same, so the function will returns ‘true’

两个字符串完全相同,因此该函数将返回“ true”

2)s1.equalsIgnoreCase(s2) (2) s1.equalsIgnoreCase(s2))

This function is also used to compare two strings but it ignores the case, it returns boolean values ‘true’/ ‘false’. If s1 and s2 are same (by ignoring the case) it returns ‘true’ otherwise it returns ‘false’.

此函数还用于比较两个字符串,但忽略大小写,它返回布尔值'true'/'false'。 如果s1和s2相同(忽略大小写),则返回“ true”,否则返回“ false”。

Example:

例:

    s1 = "Hello world"
    s2 = "HELLO world"

In this case, strings are same but characters are not in same case, still this function will return ‘true’.

在这种情况下,字符串是相同的,但字符不是相同的,但此函数仍将返回“ true”。

3)s1.length() (3) s1.length())

This function returns the length of the string s1 i.e. total number of characters of the string.

此函数返回字符串s1的长度,即字符串的字符总数。

Example:

例:

    String s1 = "Hello world!";
    System.out.println(s1.length());

Total number of characters in the string "Hello world!" are 12. Therefore, this function will return 12.

字符串“ Hello world!”中的字符总数 是12。因此,此函数将返回12。

4)s1.charAt(N) (4) s1.charAt(N))

This function is used to get the character from Nth index of the string. Remember, string’s index starts from 0.

此函数用于从字符串的 N 索引中获取字符。 请记住,字符串的索引从0开始。

Example:

例:

    String msg = "Hello world";
    System.out.println(msg.charAt(0));
    System.out.println(msg.charAt(6));

Output of this example with be "H" and "w" because "H" is at the 0th index and "w" is at the 6th index.

此示例的输出为“ H”“ w”,因为“ H”在第0 索引处,而“ w”在第6 索引处。

Read more: Java String | String.charAt(index) Method with Example

阅读更多: Java String | 带示例的String.charAt(index)方法

5)s1.indexOf(s2) (5) s1.indexOf(s2))

This function is used to get the starting index of any substring. Here, if substring s2 exists in the string s1, it will return starting position (index) of substring s2. If substring does not exist in the string, it returns -1.

此函数用于获取任何子字符串的起始索引。 在这里,如果子字符串s2存在于字符串s1中 ,它将返回子字符串s2的起始位置(索引)。 如果字符串中不存在子字符串,则返回-1 。

Example: (Case 1: If substring exists in the string)

示例:(情况1:如果字符串中存在子字符串)

    String msg = "Hello world";
    System.out.println(msg.indexOf("world"));

Output will be 6, because substring "world" initial index starts from 6th in the string "Hello world".

输出将是6,因为字符串中的 6 “世界,你好”“世界”的初始指数开始。

Example: (Case 2: If substring does not exist in the string)

示例:(情况2:如果字符串中不存在子字符串)

    String msg = "Hello world";
    System.out.println(msg.indexOf("Hi"));

Output will be -1, because substring "Hi" does not exist in the string "Hello world".

输出将为-1 ,因为字符串“ Hello world”中不存在子字符串“ Hi

6)s1.substring(N,M) (6) s1.substring(N,M))

This function is used to get the substring from the string. Here, function substring() will return the substring starting from Nth index to (M-1)th index.

此函数用于从字符串中获取子字符串。 在这里,函数substring()将返回从 N 索引到第(M-1) 索引的子字符串。

Let suppose value of N is 6 and value of M is 11, then function will return 6th, 7th, 8th, 9thth and 10th character, it does not consist Mth character.

让N个假设值是6,并且M的值是11,则函数将返回 6, 7, 8, 9和第 10 字符,它不包括 M字符。

Example:

例:

    String msg = "Hello world!";
    System.out.println(msg.substring(6,11));

Output will be "world".

输出将是“ world”

7)s1.compareTo(s2) (7) s1.compareTo(s2))

This function is used to compare two strings, if both strings are same it returns 0, if string s1 is less than s2 it returns negative value, if string s2 is less than string s1 it returns positive value. Negative and positive value depends on the difference in the ASCII codes of first dissimilar characters.

此函数用于比较两个字符串,如果两个字符串相同,则返回0,如果字符串s1小于s2,则返回负值;如果字符串s2小于字符串s1,则返回正值。 负值和正值取决于第一个不同字符的ASCII码的差异。

Example:

例:

    String s1,s2;

    s1 = "Hello";
    s2 = "Hello";
    System.out.print(s1.compareTo(s2));

    s1 = "Hello";
    s2 = "HELLO";
    System.out.println(s1.compareTo(s2));

    s1 = "Hello";
    s2 = "World";
    System.out.println(s1.compareTo(s2));

Output

输出量

    0
    32
    -15

8)s1.toUpperCase() (8) s1.toUpperCase())

This function returns uppercase string.

此函数返回大写字符串。

Example:

例:

    String msg = "Hello World!";
    System.out.println(msg.toUpperCase());

Output will be "HELLO WORLD!".

输出将为“ HELLO WORLD!”。

9)s1.toLowerCase() (9) s1.toLowerCase())

This function returns lowercase string.

此函数返回小写字符串。

Example:

例:

    String msg = "Hello World!";
    System.out.println(msg.toLowerCase());

Output will be "Hello world!".

输出将是“ Hello world!”

10)s1.trim() (10) s1.trim())

This function returns the trimmed string after removing leading and trailing spaces.

删除前导空格和尾随空格后,此函数将返回修剪后的字符串。

Example:

例:

    String msg = " Hello world! ";
    System.out.println(msg.trim());

Output will be "Hello world!".

输出将是“ Hello world!”

翻译自: https://www.includehelp.com/java/operations-on-string-in-java-functions-of-string.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值