首页 > Java > java教程 > 正文

Java 中的 split() 函数

PHPz
发布: 2024-08-30 15:35:26
原创
358 人浏览过

Java split() 函数用于根据正则表达式或给定的分隔符将字符串拆分为字符串数组。结果对象是一个包含分割字符串的数组。在返回的结果数组中,我们可以传递元素数量的限制。

广告 该类别中的热门课程 JAVA 掌握 - 专业化 | 78 课程系列 | 15 次模拟测试

让我们看一下例子,

  • 字符串: Java@SplitFunctions
  • 正则表达式: @
  • 结果:“Java”、“SplitFunctions”

上面的示例根据所需正则表达式的匹配来分割字符串。

语法:

split函数的语法如下,

public String[] split(String regex)
登录后复制

在上面的签名中,regex 是正则表达式的定界。它指定用于分割字符串的字符。最后,结果返回值返回字符串数组,该数组根据正则表达式的匹配拆分字符串。

Java 中 split() 函数如何工作?

在Java Split()函数中,我们使用各种方法分割字符串; string 类提供了两种方法来分割字符串。

让我们看看可用的签名如下,

  • public String[] split(String regex, int limit)
  • public String[] split(String regex)

1.公共字符串[] split(字符串正则表达式)

此方法使用给定字符串上的正则表达式分割字符串;整个字符串分割字符串,结果返回形式为数组字符串。在Java 1.4中,引入了上述方法。让我们看看分割字符串的示例以及相应的结果输出,

代码:

String string_1="JavaProgram";
System.out.println(Arrays.toString(s.split("a")));
登录后复制

输出:

Java 中的 split() 函数

2. public String[] split(String regex, int limit)

当我们需要将Java字符串拆分为有限数量的字符串时,我们使用此方法;为此,我们采用这种方法;让我们看一下字符串的示例,其中包含包含名称和地址的字符串变量,分隔符为逗号,以下地址中包含逗号,因此我们采用这种方法。

代码:

String s = "Spencer Plaza, New York, USA";
String[] data = s.split(",", 2);
System.out.println("Name = "+data[0]); // Spencer Plaza
System.out.println("Address = "+data[1]); //New York,USA
登录后复制

限制参数是可选的。表示分割数量的整数,分割限制之后的项目将不包含在数组中。上面的第一种方法通过将限制传递为 0 来使用第二种方法。

代码:

public String[] split(String regex) {
return split(regex, 0);
}
登录后复制

split() 函数示例

以下示例如下:

示例#1

代码:

import java.io.*;
public class Program_1
{
public static void main(String args[]) {
String string_1 = new String("Welcome-to-JavaProgramming");
System.out.println("Output Value :" );
for (String res: string_1.split("-"))
{
System.out.println(res);
}
}
}
登录后复制

输出:

Java 中的 split() 函数

对需要提供和分割分隔符参数的字符串使用 split() 函数,我们将使用分隔符作为逗号(,),返回结果将是数组分割。每次分割操作后,输出都会打印称为数组元素的每个字符串,如下所示,

示例#2

代码:

import java.io.*;
class Program_2
{
public static void main(String []args)
{
String string_1 = "String, Functions, Split, Methods, Demo";
String[] split_array = string_1.split(", ");
for (int i=0; i < split_array.length; i++)
{
System.out.println(split_array [i]);
}
}
}
登录后复制

输出:

Java 中的 split() 函数

在这里,我们传递 split,它作为该函数的第二个参数。这限制了分割字符串的数量。

示例#3

代码:

import java.io.*;
public class Program_3
{
public static void main(String[] args)
{
String string_1 = "JavatSplittFunction";
System.out.println("Result:");
String[] arrSplit = string_1.split("t", 0);
for (String a : arrSplit)
{
System.out.println(a);
}
System.out.println("Length of Split Array: "+ arrSplit.length);
}
}
登录后复制

输出:

Java 中的 split() 函数

带有 limit 参数的 Split() 方法

带有 limit 参数的 Split() 方法会分割有限数量的字符串。 split() 和带有 limit 参数的 split() 之间的区别在于它限制了分割后返回的字符串数量。对于限制,我们需要为 split() 函数提供一个输入参数。让我们看看带有 limit 参数的 split() 方法的用法,

public String[] split(String regex, int limit)
登录后复制

这里参数regex对正则表达式进行界定,限制是针对结果阈值。限制有 3 个值,它们是:

  • limit > 0: If the limit is set as >0, the resultant array length must not be more significant than n, which is applied at most limit-1 times. The last entry contains input with the last matched delimiter.
  • limit < 0:If the limit is set as <0, the resultant array with any number of lengths can apply the pattern as many times as possible.
  • limit = 0: If the limit is set as equal to 0, the resulting array has any number of lengths, but the empty string will be discarded, and this limit is functional as many times as possible.
  • This parameter’s return value will be an array of string objects by splitting the given string accords to the limit parameter. The PatternSyntacException will occur if the given regular expression syntax is invalid while executing the code. Let’s see the example program for the split() method on a string with the limit parameter.

    Example #4

    Code:

    public class Program_4
    {
    public static void main(String args[])
    {
    String string_1 = "238-347-9288";
    String[] stringArray = string_1.split("8",2);
    System.out.println("Split() Method with the Limit Parameter");
    System.out.println("\nLimit Value is +ve");
    System.out.println("Sub-Strings: "+stringArray.length);
    for(int i=0; i<stringArray.length; i++)
    {  System.out.println("string_1["+i+"] : "+stringArray[i]);
    }
    String[] stringArray2 = string_1.split("8",-3);
    System.out.println("Limit Value is -ve");
    System.out.println("Sub-Strings: "+stringArray2.length);
    for(int i=0; i<stringArray2.length; i++)
    {
    System.out.println("string_1["+i+"] : "+stringArray2[i]);
    }
    String[] stringArray3 = string_1.split("8",0);
    System.out.println("Limit Value is 0");
    System.out.println("Sub-Strings: "+stringArray3.length);
    for(int i=0; i<stringArray3.length; i++)
    {
    System.out.println("string_1["+i+"] : "+stringArray3[i]);
    }
    }
    }
    登录后复制

    Output:

    Java 中的 split() 函数

    The above program shows that the split() method works by the specified limit parameter, as seen by the output:

    1. The number of substrings in the resulting array is two when the limit is 2.
    2. Setting the limit to -3 causes the string to split into 4 substrings, including the trailing spaces.
    3. If you set the limit to 0, it eliminates the trailing spaces. As a result, the input string splits into 2 substrings.

    Conclusion

    At the end of the ‘Split() Function in Java’ article, we learned how to split strings using the split() method in different Java approaches. I hope in this article; you understand everything that has been shared with examples.

    以上是Java 中的 split() 函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!