Java 中的 split() 函数
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")));
输出:
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); } } }
输出:
对需要提供和分割分隔符参数的字符串使用 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]); } } }
输出:
在这里,我们传递 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); } }
输出:
带有 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:
The above program shows that the split() method works by the specified limit parameter, as seen by the output:
- The number of substrings in the resulting array is two when the limit is 2.
- Setting the limit to -3 causes the string to split into 4 substrings, including the trailing spaces.
- 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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
