使用分隔符制表符“ "在 Java 中解析字符串使用拆分 [英] String parsing in Java with delimiter tab " " using split

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

问题描述

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

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("	");

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

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 使用 Regular Expressions,你也没有需要为您的拆分分配一个额外的数组.

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.

让我解释一下为什么 不起作用以及为什么需要 \\ 来转义 \.

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

好的,所以当你使用 Split 时,它实际上需要一个正则表达式(正则表达式),在正则表达式中你想定义要拆分的字符,如果你写了 这实际上并不意味着 而你想要拆分的是 ,对吧?因此,通过编写 你告诉你的正则表达式处理器嘿,被转义的字符分割" NOT 嘿,被所有看起来像 ".注意到区别了吗?使用 意味着逃避某事.正则表达式中的 意味着与您想象的完全不同.

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 that actually doesn't mean and what you WANT to split by is , right? So, by just writing you tell your regex-processor that "Hey split by the character that is escaped t" NOT "Hey split by all characters looking like ". 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

告诉正则表达式处理器寻找 .好的,那你为什么需要两个 em?好吧,第一个 转义了第二个,这意味着它看起来像这样:当您处理文本时 !

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

现在假设您要拆分

那么你会留下 \ 但你看,这行不通!因为 会尝试转义之前的字符!这就是为什么您希望输出为 \,因此您需要有 \\.

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 中解析字符串使用拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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