Java中的字符串解析,带有分隔符选项卡“\t”使用拆分 [英] String parsing in Java with delimiter tab "\t" using split

查看:240
本文介绍了Java中的字符串解析,带有分隔符选项卡“\t”使用拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个以制表符分隔的字符串。我正在使用 split 函数完成此操作,并且它适用于大多数情况。当字段丢失时会出现问题,因此我不会在该字段中获取null,而是获取下一个值。我将解析后的值存储在字符串数组中。

I'm processing a string which is tab delimited. I'm accomplishing this using the split function, and it works in most situations. The problem occurs when a field is missing, so instead of getting null in that field I get the next value. I'm storing the parsed values in a string array.

String[] columnDetail = new String[11];
columnDetail = column.split("\t");

任何帮助将不胜感激。如果可能的话,我想将解析后的字符串存储到字符串数组中,这样我就可以轻松访问解析后的数据。

Any help would be appreciated. If possible I'd like to store the parsed strings into a string array so that I can easily access the parsed data.

推荐答案

String.split 使用正则表达式,您也不需要为拆分分配额外的数组。

String.split uses Regular Expressions, also you don't need to allocate an extra array for your split.

分割方法会给你一个列表。,问题是你试图预先定义一个标签出现的次数,但是怎么样你真的知道吗?尝试使用Scanner或StringTokenizer,只需了解分割字符串的工作原理。

The split-method will give you a list., the problem is that you try to pre-define how many occurrences you have of a tab, but how would you Really know that? Try using the Scanner or StringTokenizer and just learn how splitting strings work.

让我解释为什么\t不起作用以及为什么需要 \\\\ 要逃避 \\

Let me explain Why \t does not work and why you need \\\\ to escape \\.

好的,所以当你使用Split时,它实际上需要一个正则表达式(正则表达式),而在正则表达式中你想要定义要分割的字符,如果你写的是\t,那实际上并不意味着 \t 你想要拆分的是 \t ,对吧?所以,只需写一下 \t ,就告诉你的正则表达式处理器嘿,你被转义的角色分开嘿分裂所有字符看起来像 \t 。请注意区别?使用\意味着逃避某些事情。正则表达式中的 \ 意味着与您的想法完全不同。

Okay, so when you use Split, it actually takes a regex ( Regular Expression ) and in regular expression you want to define what Character to split by, and if you write \t that actually doesn't mean \t and what you WANT to split by is \t, right? So, by just writing \t you tell your regex-processor that "Hey split by the character that is escaped t" NOT "Hey split by all characters looking like \t". Notice the difference? Using \ means to escape something. And \ in regex means something Totally different than what you think.

所以这就是你需要使用的原因解决方案

So this is why you need to use this Solution:

\\t

告诉正则表达式处理器寻找\t。好的,为什么你需要两个em?好吧,第一个\逃脱第二个,这意味着它将如下所示:\t当你处理文本时!

To tell the regex processor to look for \t. Okay, so why would you need two of em? Well, the first \ escapes the second, which means it will look like this: \t when you are processing the text!

现在让我们说你正在寻找拆分\

Now let's say that you are looking to split \

那么你会留下\\但是看,那不行!因为\会试图逃避以前的char!这就是为什么你想要输出为\\,因此你需要有\\\\。

Well then you would be left with \\ but see, that doesn't Work! because \ will try to escape the previous char! That is why you want the Output to be \\ and therefore you need to have \\\\.

我真的希望上面的例子有帮助你明白为什么你的解决方案不起作用以及如何征服其他解决方案!

I really hope the examples above helps you understand why your solution doesn't work and how to conquer other ones!

现在,我已经给你这个回答,也许你现在应该开始看它们。

Now, I've given you this answer before, maybe you should start looking at them now.

其他方法

StringTokenizer

你应该看一下进入 StringTokenizer ,它是一个这种工作非常方便。

You should look into the StringTokenizer, it's a very handy tool for this type of work.

示例

 StringTokenizer st = new StringTokenizer("this is a test");
 while (st.hasMoreTokens()) {
     System.out.println(st.nextToken());
 }

这将输出

 this
 is
 a
 test

使用StringTokenizer的第二个构造函数来设置分隔符:

You use the Second Constructor for StringTokenizer to set the delimiter:

StringTokenizer(String str,String delim)

扫描程序

您还可以使用扫描仪作为评论员之一表示这可能看起来有点像这样

You could also use a Scanner as one of the commentators said this could look somewhat like this

示例

 String input = "1 fish 2 fish red fish blue fish";

 Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");

 System.out.println(s.nextInt());
 System.out.println(s.nextInt());
 System.out.println(s.next());
 System.out.println(s.next());

 s.close(); 

输出

 1
 2
 red
 blue 

意思是它会删除fish这个词,然后用fish作为分隔符给你剩下的。

Meaning that it will cut out the word "fish" and give you the rest, using "fish" as the delimiter.

取自Java API的示例

这篇关于Java中的字符串解析,带有分隔符选项卡“\t”使用拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆