I am trying to break apart a very simple collection of strings that come in the forms of
我试图打破一个非常简单的字符串集合
0|0
10|15
30|55
etc etc. Essentially numbers that are seperated by pipes.
等等。基本上由管道分开的数字。
When I use java's string split function with .split("|"). I get somewhat unpredictable results. white space in the first slot, sometimes the number itself isn't where I thought it should be.
当我使用java的字符串拆分函数与.split(“|”)。我得到了一些不可预知的结果。第一个插槽中的空白区域,有时数字本身并不是我认为应该的位置。
Can anybody please help and give me advice on how I can use a reg exp to keep ONLY the integers?
任何人都可以帮助并给我建议如何使用reg exp来保持整数?
I was asked to give the code trying to do the actual split. So allow me to do that in hopes to clarify further my problem :)
我被要求提供试图进行实际拆分的代码。所以请允许我这样做,希望进一步澄清我的问题:)
String temp = "0|0";
String splitString = temp.split("|");
results
结果
\n
0
|
0
I am trying to get
我想要
0
0
only. Forever grateful for any help ahead of time :)
只要。永远感激任何帮助提前:)
8 个解决方案
#1
7
I still suggest to use split()
, it skips null tokens by default. you want to get rid of non numeric characters in the string and only keep pipes and numbers, then you can easily use split()
to get what you want. or you can pass multiple delimiters to split (in form of regex) and this should work:
我仍然建议使用split(),默认情况下它会跳过null标记。你想摆脱字符串中的非数字字符,只保留管道和数字,然后你可以轻松地使用split()来获得你想要的。或者您可以传递多个分隔符进行拆分(以正则表达式的形式),这应该工作:
String[] splited = yourString.split("[\\|\\s]+");
and the regex:
和正则表达式:
import java.util.regex.*;
Pattern pattern = Pattern.compile("\\d+(?=([\\|\\s\\r\\n]))");
Matcher matcher = pattern.matcher(yourString);
while (matcher.find()) {
System.out.println(matcher.group());
}
#2
6
The pipe symbol is special in a regexp (it marks alternatives), you need to escape it. Depending on the java version you are using this could well explain your unpredictable results.
管道符号在正则表达式中是特殊的(它标记替代),您需要将其转义。根据您使用的Java版本,这可以很好地解释您不可预测的结果。
class t {
public static void main(String[]_)
{
String temp = "0|0";
String[] splitString = temp.split("\\|");
for (int i=0; i<splitString.length; i++)
System.out.println("splitString["+i+"] is " + splitString[i]);
}
}
outputs
输出
splitString[0] is 0
splitString[1] is 0
Note that one backslash is the regexp escape character, but because a backslash is also the escape character in java source you need two of them to push the backslash into the regexp.
请注意,一个反斜杠是regexp转义字符,但由于反斜杠也是java源中的转义字符,因此需要其中两个将反斜杠推入正则表达式。
#3
4
You can do replace white space for pipes and split it.
您可以替换管道的空白区域并将其拆分。
String test = "0|0 10|15 30|55";
test = test.replace(" ", "|");
String[] result = test.split("|");
Hope this helps for you..
希望这对你有所帮助..
#4
3
You can use StringTokenizer.
您可以使用StringTokenizer。
String test = "0|0";
StringTokenizer st = new StringTokenizer(test);
int firstNumber = Integer.parseInt(st.nextToken()); //will parse out the first number
int secondNumber = Integer.parseInt(st.nextToken()); //will parse out the second number
Of course you can always nest this inside of a while loop if you have multiple strings.
当然,如果你有多个字符串,你总是可以将它嵌套在while循环中。
Also, you need to import java.util.* for this to work.
此外,您需要导入java.util。*才能生效。
#5
2
The pipe ('|') is a special character in regular expressions. It needs to be "escaped" with a '\' character if you want to use it as a regular character, unfortunately '\' is a special character in Java so you need to do a kind of double escape maneuver e.g.
管道(“|”)是正则表达式中的特殊字符。如果你想将它作为常规字符使用,它需要用'\'字符“转义”,不幸的是'\'是Java中的一个特殊字符所以你需要做一种双逃避操作,例如
String temp = "0|0";
String[] splitStrings = temp.split("\\|");
#6
2
The Guava library has a nice class Splitter
which is a much more convenient alternative to String.split()
. The advantages are that you can choose to split the string on specific characters (like '|'), or on specific strings, or with regexps, and you can choose what to do with the resulting parts (trim them, throw ayway empty parts etc.).
Guava库有一个很好的类Splitter,它是String.split()的一个更方便的替代品。优点是您可以选择将字符串拆分为特定字符(如“|”),特定字符串或正则表达式,您可以选择如何处理结果部分(修剪它们,抛出空的部分等) )。
For example you can call
例如你可以打电话
Iterable<String> parts = Spliter.on('|').trimResults().omitEmptyStrings().split("0|0")
#7
0
This should work for you:
这应该适合你:
([0-9]+)
#8
0
Considering a scenario where in we have read a line from csv or xls file in the form of string and need to separate the columns in array of string depending on delimiters.
考虑一种情况,我们以字符串的形式从csv或xls文件中读取一行,并且需要根据分隔符分隔字符串数组中的列。
Below is the code snippet to achieve this problem..
下面是实现此问题的代码段。
{ ...
....
String line = new BufferedReader(new FileReader("your file"));
String[] splittedString = StringSplitToArray(stringLine,"\"");
...
....
}
public static String[] StringSplitToArray(String stringToSplit, String delimiter)
{
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
char[] chars = stringToSplit.toCharArray();
for (int i=0; i 0) {
tokens.addElement(token.toString());
token.setLength(0);
i++;
}
} else {
token.append(chars[i]);
}
}
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] preparedArray = new String[tokens.size()];
for (int i=0; i < preparedArray.length; i++) {
preparedArray[i] = (String)tokens.elementAt(i);
}
return preparedArray;
}
Above code snippet contains method call to StringSplitToArray where in the method converts the stringline into string array splitting the line depending on the delimiter specified or passed to the method. Delimiter can be comma separator(,) or double code(").
上面的代码片段包含对StringSplitToArray的方法调用,其中在方法中将stringline转换为字符串数组,根据指定的分隔符或传递给方法来分割行。分隔符可以是逗号分隔符(,)或双重代码(“)。
For more on this, follow this link : http://scrapillars.blogspot.in
有关此问题的更多信息,请访问以下链接:http://scrapillars.blogspot.in
#1
7
I still suggest to use split()
, it skips null tokens by default. you want to get rid of non numeric characters in the string and only keep pipes and numbers, then you can easily use split()
to get what you want. or you can pass multiple delimiters to split (in form of regex) and this should work:
我仍然建议使用split(),默认情况下它会跳过null标记。你想摆脱字符串中的非数字字符,只保留管道和数字,然后你可以轻松地使用split()来获得你想要的。或者您可以传递多个分隔符进行拆分(以正则表达式的形式),这应该工作:
String[] splited = yourString.split("[\\|\\s]+");
and the regex:
和正则表达式:
import java.util.regex.*;
Pattern pattern = Pattern.compile("\\d+(?=([\\|\\s\\r\\n]))");
Matcher matcher = pattern.matcher(yourString);
while (matcher.find()) {
System.out.println(matcher.group());
}
#2
6
The pipe symbol is special in a regexp (it marks alternatives), you need to escape it. Depending on the java version you are using this could well explain your unpredictable results.
管道符号在正则表达式中是特殊的(它标记替代),您需要将其转义。根据您使用的Java版本,这可以很好地解释您不可预测的结果。
class t {
public static void main(String[]_)
{
String temp = "0|0";
String[] splitString = temp.split("\\|");
for (int i=0; i<splitString.length; i++)
System.out.println("splitString["+i+"] is " + splitString[i]);
}
}
outputs
输出
splitString[0] is 0
splitString[1] is 0
Note that one backslash is the regexp escape character, but because a backslash is also the escape character in java source you need two of them to push the backslash into the regexp.
请注意,一个反斜杠是regexp转义字符,但由于反斜杠也是java源中的转义字符,因此需要其中两个将反斜杠推入正则表达式。
#3
4
You can do replace white space for pipes and split it.
您可以替换管道的空白区域并将其拆分。
String test = "0|0 10|15 30|55";
test = test.replace(" ", "|");
String[] result = test.split("|");
Hope this helps for you..
希望这对你有所帮助..
#4
3
You can use StringTokenizer.
您可以使用StringTokenizer。
String test = "0|0";
StringTokenizer st = new StringTokenizer(test);
int firstNumber = Integer.parseInt(st.nextToken()); //will parse out the first number
int secondNumber = Integer.parseInt(st.nextToken()); //will parse out the second number
Of course you can always nest this inside of a while loop if you have multiple strings.
当然,如果你有多个字符串,你总是可以将它嵌套在while循环中。
Also, you need to import java.util.* for this to work.
此外,您需要导入java.util。*才能生效。
#5
2
The pipe ('|') is a special character in regular expressions. It needs to be "escaped" with a '\' character if you want to use it as a regular character, unfortunately '\' is a special character in Java so you need to do a kind of double escape maneuver e.g.
管道(“|”)是正则表达式中的特殊字符。如果你想将它作为常规字符使用,它需要用'\'字符“转义”,不幸的是'\'是Java中的一个特殊字符所以你需要做一种双逃避操作,例如
String temp = "0|0";
String[] splitStrings = temp.split("\\|");
#6
2
The Guava library has a nice class Splitter
which is a much more convenient alternative to String.split()
. The advantages are that you can choose to split the string on specific characters (like '|'), or on specific strings, or with regexps, and you can choose what to do with the resulting parts (trim them, throw ayway empty parts etc.).
Guava库有一个很好的类Splitter,它是String.split()的一个更方便的替代品。优点是您可以选择将字符串拆分为特定字符(如“|”),特定字符串或正则表达式,您可以选择如何处理结果部分(修剪它们,抛出空的部分等) )。
For example you can call
例如你可以打电话
Iterable<String> parts = Spliter.on('|').trimResults().omitEmptyStrings().split("0|0")
#7
0
This should work for you:
这应该适合你:
([0-9]+)
#8
0
Considering a scenario where in we have read a line from csv or xls file in the form of string and need to separate the columns in array of string depending on delimiters.
考虑一种情况,我们以字符串的形式从csv或xls文件中读取一行,并且需要根据分隔符分隔字符串数组中的列。
Below is the code snippet to achieve this problem..
下面是实现此问题的代码段。
{ ...
....
String line = new BufferedReader(new FileReader("your file"));
String[] splittedString = StringSplitToArray(stringLine,"\"");
...
....
}
public static String[] StringSplitToArray(String stringToSplit, String delimiter)
{
StringBuffer token = new StringBuffer();
Vector tokens = new Vector();
char[] chars = stringToSplit.toCharArray();
for (int i=0; i 0) {
tokens.addElement(token.toString());
token.setLength(0);
i++;
}
} else {
token.append(chars[i]);
}
}
if (token.length() > 0) {
tokens.addElement(token.toString());
}
// convert the vector into an array
String[] preparedArray = new String[tokens.size()];
for (int i=0; i < preparedArray.length; i++) {
preparedArray[i] = (String)tokens.elementAt(i);
}
return preparedArray;
}
Above code snippet contains method call to StringSplitToArray where in the method converts the stringline into string array splitting the line depending on the delimiter specified or passed to the method. Delimiter can be comma separator(,) or double code(").
上面的代码片段包含对StringSplitToArray的方法调用,其中在方法中将stringline转换为字符串数组,根据指定的分隔符或传递给方法来分割行。分隔符可以是逗号分隔符(,)或双重代码(“)。
For more on this, follow this link : http://scrapillars.blogspot.in
有关此问题的更多信息,请访问以下链接:http://scrapillars.blogspot.in