- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用以下代码:
String sample = "::";
String[] splitTime = sample.split(":");
// extra detail omitted
System.out.println("Value 1 :"+splitTime[0]);
System.out.println("Value 2 :"+splitTime[1]);
System.out.println("Value 3 :"+splitTime[2]);
我遇到了 ArrayIndexOutofBound
异常。 String.split()
如何处理连续或尾随/开始的定界符?
另见:
最佳答案
Alnitak 是正确的,默认情况下尾随的空字符串将被丢弃。
如果你想要尾随空字符串,你应该使用split(String, int)
并传递一个负数作为 limit
参数。
The
limit
parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array's length will be no greater than n, and the array's last entry will contain all input beyond the last matched delimiter. If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
请注意 split(aString)
是 split(aString, 0)
的同义词:
This method works as if by invoking the two-argument
split
method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
此外,您应该使用循环从数组中获取值;这避免了可能的 ArrayIndexOutOfBoundsException
。
因此,您更正后的代码应该是(假设您想要尾随的空字符串):
String sample = "::";
String[] splitTime = sample.split(":", -1);
for (int i = 0; i < splitTime.length; i++) {
System.out.println("Value " + i + " : \"" + splitTime[i] + "\"");
}
输出:
Value 0 : ""Value 1 : ""Value 2 : ""
关于java - 如何拆分仅包含定界符的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/785586/
我有一列data.frame,其中一些空格应该作为分隔符,而某些只是一个空格。 #input data dat <- data.frame(x=c("A 2 2 textA1 textA2 Z1",
所以我试图分解一个包含答案列表的字符串。 例如:答案:1. 梳理。 2.拇指。 3.坟墓(地下墓穴)。 4. 子宫。 5.面包屑。 6. 炸弹。 7. NumPy 。 8.沉着冷静。 9. 屈服。 有
我有包含多个查询的文件。如何使用 DELIMITER 将它们分开?我像在 mysql 中一样尝试过,但它没有编译: DELIMITER % CREATE SEQUENCE logins_seq I
这个问题在这里已经有了答案: Does -* have any special meaning in regular expression? (1 个回答) 关闭 8 年前。 我有一个像 22 +
我正在尝试使用 sscanf 解析以下字符串: query=testword&diskimg=simple.img 如何使用 sscanf 解析出“testword”和“simple.img”? ss
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示: foo 4.500 bar 3.00 1.3 3 foo bar 如何读取由空格而不是换行符分隔的文件?有什么方法可以设置
这是我的代码: With ad.Tables(2) For i As Integer = 0 To .Rows.Count - 1 If .Rows(i)("name") &
我正在排队,例如 数组大小:4 来自 cin,我想首先检查字符串是否正是这个,然后提取整数。 我找到了读取字符串并提取整数的方法: string start; getline (cin
我正在尝试读取以这种形式编写的文本文件: AB523:[joe, pierre][charlie][dogs,cat] ZZ883:[ronald, zigomarre][pele]
我是 PIG 的新手,有人可以帮助我如何加载一个包含多个字符(在我的例子中为“^^”)作为列分隔符的文件。 例如我有以下列的文件aisforapple^^bisforball^^cisforcat^^
var wrapped_string = "shouldn't this\ work?" JSLint给出了严重的擒纵机构错误。 那么我们如何在 JavaScript 中执行heredoc呢?我有一个
问题:我需要读取 CSV 文件。我使用 FileHelpers 库来实现这一点。 问题是我需要一个动态定界符(用户定义),这意味着任何东西都可以是定界符(逗号、分号、制表符、换行符,以及其他任何东西)
我试图读入一个由 powershell 执行的 sql 文件。一切正常,除了 powershell 因单词定界符而抛出错误。所以我的问题是如何在我的 sql 文件中使用定界符(我只能对 sql 文件进
我需要能够通过 DBI 发出“创建触发器”。我似乎无法使分隔符命令正常工作。任何人都可以找到一种方法来完成这项工作吗? 代码: use strict; use DBI; my $dbargs = {m
我知道我可以使用 Mustache.tags('[[', ']]'); 更改默认分隔符 我深入研究了源代码,但找不到也不知道如何更改不转义 HTML 定界符,默认情况下为 {{{ }}}。感谢您的帮助
我正在尝试获取 std::string 值并将其用作 std::getline() 函数中的分隔符但无论我做什么,编译器都会给我错误。这就是我想要做的: std::stringstream ss(s)
我正在尝试实现我自己的 beanstalkd 客户端作为学习围棋的一种方式。 https://github.com/kr/beanstalkd/blob/master/doc/protocol.txt
我正在编写一个显示代码和输出的样式指南。它目前的结构使得代码只需要描述一次,并以其原始版本和解释版本显示,如下所示: #{ image_tag 'image.png' } PLACE_THE
是否可以将默认字段分隔符从逗号更改为其他字符,例如用于导出的 '|'? 最佳答案 下面是一个使用选项卡的示例。 到一个文件: CSV.open("myfile.csv", "w", {:col_sep
我是一名优秀的程序员,十分优秀!