[英]Efficient way to process record separated by pipe?
I've a string which contains data concatenated by pipe (|) 我有一个字符串,其中包含由管道(|)连接的数据
Example: 5|aa@gmail.com|477|en|87477 示例:5 | aa@gmail.com | 477 | en | 87477
The string has ~15 fields concatenated by pipe. 该字符串具有〜15个由管道连接的字段。
It could be possible that some field may have empty data: Example: 5|aa@gmail.com||en|87477 某些字段可能有空数据:示例:5|aa@gmail.com||en|87477
How can I efficiently get all the individual fields in Java 1.6? 如何有效地获取Java 1.6中的所有单个字段?
Thanks! 谢谢!
Consider using the Split method offered by java 考虑使用java提供的Split方法
String str = "5|aa@gmail.com|477|en|87477";
String[] data = str.split("\\|");
for(String s : data){
System.out.println(s);
}
The output i get is 我得到的输出是
5
aa@gmail.com
477
en
87477
As it's just an array of string you access the data as you want, ie data[1] gives aa@gmail.com 因为它只是一个字符串数组,所以您可以根据需要访问数据,即data [1]给出aa@gmail.com
I upvoted the String.split
answer because it's simplest. 我赞成
String.split
答案,因为它是最简单的。 Its two downsides are: 它的两个缺点是:
To get a slight increase, if you need it, you can pre-compile the pattern and then use that to split: 要稍微增加一点,如果需要,可以预编译模式,然后使用它来拆分:
import java.util.regex.Pattern;
private static final Pattern BAR_REGEX = Pattern.compile( Pattern.quote("|") );
and then in your method: 然后在你的方法中:
String[] splits = BAR_REGEX.split( inputString );
You may also want to consider the StringUtils class from Apache Commons Lang . 您可能还需要考虑Apache Commons Lang中的StringUtils类。 Very fast and does indeed handle 'missing' tokens.
非常快,确实可以处理“丢失”令牌。
Alan's solution will work most times except if you have empty token(s) in the end. 艾伦(Alan)的解决方案将在大多数情况下有效,除非最后您有空令牌。 For example,
例如,
String str = "5|aa@gmail.com|477|en||||87477||x|||";
字符串str =“ 5 | aa@gmail.com | 477 | en ||||| 87477 || x |||”;
You could solve this by 你可以通过解决这个问题
String str = "5|aa@gmail.com|477|en||||87477||x|||";
字符串str =“ 5 | aa@gmail.com | 477 | en ||||| 87477 || x |||”;
str.split("\\\\|", -1);
str.split(“ \\\\ |”,-1);
考虑使用StringTokenizer
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.