迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中拆分逗号分隔字符串的三种方法

作者:迹忆客 最近更新:2023/03/26 浏览次数:

Java string 类充满了丰富的资源,你可以使用这些资源创建基本程序来解决复杂的问题陈述。本教程将展示一些很酷的方法来从逗号分隔的起点或你可能在字符串值集中使用的任何除法分割字符串。

此外,我们将使用 IndexOf(); 我们的第一个程序的方法并将 Split() 与 Java 的动态数组和字符串类集成。


在 Java 中使用 Indexof()Substring() 方法拆分逗号分隔的字符串

查看这个字符串:String java = "Java, is, a, fun, programming, language!";。此示例以逗号分隔,但可以是其他任何字符。

但是,这样做不会帮助你作为新手理解构建逻辑。以下核心概念在中级阶段为你修订。

了解 Java 中的 indexof() 方法

它返回用户指定字符串中指定值第一次出现的位置。

参数:

     
String String demoStr = 我,我,一个,字符串 假设起始索引设置为 0indexOf()"I" 搜索。
fromIndex 一个 int 值,指定开始搜索的索引位置。 它会让你玩索引。
char 包含特定字符的 int 值。 可选的

返回:一个 int 值表示该值在字符串中的第一次出现索引,如果它从未出现,则为 -1

Java 中 substring() 方法的重要性

它返回一个字符串,该字符串是该字符串的子字符串。它从给定点开始,以指定点的字符结束。

我们希望你已经成功地理解了这些方法的文档。现在让我们运行我们的第一个程序。

代码:

//Example 1
public class UseIndexofAndSubString {
	public static void main(String[] args) {
		// Specified string to search comma from and then split it in a substring
		String java = "Java, is, a, fun, programming, language!";
		String comma = ","; // To search ',' from the first occurrence of the specified string
		// Our starting index is 0 since we do not want to miss any occurrence in the
		// given string value while the end index is also initialized
		int start = 0, end;
		// Now the indexOf will find the specified character which in this case is a
		// comma (,)
		// Note: The second parameter (start) determines that the method starts from the
		// beginning, otherwise it will
		// Basically, we will be able to locate the occurrence of (comma) from where we
		// want it to be split
		end = java.indexOf(comma, start);
		// If the loop does not find (comma)
		// We already mentioned the return type of indexOf in case if the given char
		// never occurred, right? That is what the loop searches for!
		while (end != -1) {
			//// Substring will give us each occurrence until the loop is unable to find
			//// comma
			String split = java.substring(start, end);
			System.out.println(split); // print each sub string from the given string
			// Then add 1 to the end index of the first occurrence (search more until the
			// condition is set false)
			start = end + 1;
			end = java.indexOf(comma, start);
		}
		// Since there is no comma after the last index of the string (language!), the
		// loop could not find the end index; thus, it never printed the last word!
		// We will get the last instance of the given string using substring and pass
		// start position (Starts from L and ends at the !)
		String split = java.substring(start);
		// Brave, you got it all right!
		System.out.println(split);
	}
}

输出:

Java
is
a
fun
programming
language!

假设你想将 comma 更改为美元 $ 符号。

所有你需要做的:

String java = "Using$ another$ Character$ to$ split$ from!";
String sepFrom ="$"; //To search from the specified string

输出:

Using
another
Character
to
split
from!

在 Java 中使用字符串列表和拆分方法拆分逗号分隔的字符串

当我们讨论初级理解的重要性时,我们谈到了数组。

下面的程序很简单。你不需要找到开始和结束位置,也不需要运行 while 循环并比较返回值是否为-1。

Java 的 stringarrays 类用四行干净的代码总结了所有内容。同时,你需要了解两种必要的方法,这将有助于你一路走来。

Java 中的 Arrays.aslist()) 函数

参数:

  1. 数组元素值的类别。
  2. 用户指定的数组用于存储列表。
  3. 它为你提供了你定义的指定数组的列表视图。

语法:

List<String> ArraySting = Arrays.asList(IwillBeSplit.split(","));

Java 中的 Split() 函数

它使用我们作为用户指定为正则表达式的 split 方法中的两个参数和 0 的限制参数。

因此,它不包括输出集中尾随的空字符串。

  1. 参数:作为分隔符的正则表达式。
  2. 返回: 将 regex 表达式 的该字符串分开生成的字符串数组。

代码:

//Example 3 using Lists' String with Split Method to Split String From a Given demarcation
import java.util.Arrays;
import java.util.List;

public class UseStingListWithSplitMthd {
	public static void main(String[] args) {
		String IwillBeSplit = "There,are,seven,days,!&#^*,in,a,weak";
		// List is an interface in Java
		List<String> ArraySting = Arrays.asList(IwillBeSplit.split(","));
		// Using for each loop
		ArraySting.forEach(System.out::println);
	}
}

输出:

There
are
seven
days
!&#^*
in
a
weak

假设字符串是:

String IwillBeSplit = "You%replaced%the%comma%with%a%percentage";
List<String> ArraySting = Arrays.asList(IwillBeSplit.split("%"));

我们将得到:

输出:

You
replaced
the
comma
with
a
percentage

在 Java 中使用数组列表和拆分方法以逗号分隔字符串值

以下代码块不应使你感到困惑。因为,你已经熟悉 Arrays.asList());split()

然而,这里的不同之处在于解决方案的方法,它使用 ArrayList<String> 而不仅仅是 List<String>

那么区别是什么呢?

这并不难。list 是一个接口,而 ArrayList 是一个 Java 集合类。

前者提供一个静态数组,而后者生成一个动态数组来存储元素。

代码:

//Example 2 using ArrayLists and split method
import java.util.ArrayList;
import java.util.Arrays;

public class ArrayListSplitMethod {
	public static void main(String[] args) {
		String ALstr = "So,many,ways,to,separate,a,string,from,a,specified,position";

		ArrayList<String> Spliter = new ArrayList<>(Arrays.asList(ALstr.split(",")));
		Spliter.forEach(System.out::println);
	}
}

输出:

So
many
ways
to
separate
a
string
from
a
specified
position

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

如何在 Java 中延迟几秒钟的时间

发布时间:2023/12/17 浏览次数:217 分类:Java

本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。

如何在 Java 中把 Hashmap 转换为 JSON 对象

发布时间:2023/12/17 浏览次数:187 分类:Java

它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。

如何在 Java 中按值排序 Map

发布时间:2023/12/17 浏览次数:171 分类:Java

本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map 进行排序,并列出了一些示例代码来理解它。

如何在 Java 中打印 HashMap

发布时间:2023/12/17 浏览次数:192 分类:Java

本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。

在 Java 中更新 Hashmap 的值

发布时间:2023/12/17 浏览次数:146 分类:Java

本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。

Java 中的 hashmap 和 map 之间的区别

发布时间:2023/12/17 浏览次数:79 分类:Java

本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,

在 Java 中获取用户主目录

发布时间:2023/12/17 浏览次数:218 分类:Java

这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。

Java 中 size 和 length 的区别

发布时间:2023/12/17 浏览次数:179 分类:Java

这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。

Java 中的互斥锁

发布时间:2023/12/17 浏览次数:111 分类:Java

了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便