Separator-based string separation

I am trying to break up a very simple set of lines that go into forms

0|0
10|15
30|55

etc. Essentially numbers separated by pipes.

When I use java string split function with .split ("|"). I get some unpredictable results. a space in the first slot, sometimes the number itself is not where I thought it should be.

Can someone help and give me advice, how can I use reg exp to store ONLY integers?

I was asked to give a code trying to make the actual split. So let me do this to further clarify my problem :)

String temp = "0|0";
String splitString = temp.split("|");

results

\n
0
| 
0

I'm trying to get

0
0

only. Thanks forever for any help in advance :)

+5
source share
8

split(), . , split(), , . ( ), :

String[] splited = yourString.split("[\\|\\s]+");

:

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());
}
+7

( ), . 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]);
    }       
}

splitString[0] is 0
splitString[1] is 0

, escape , escape- java, , .

+6

.

String test = "0|0 10|15 30|55";
test = test.replace(" ", "|");
String[] result = test.split("|");

, .

+4

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

, while, .

, java.util. * .

+3

('|') . "" "\", , , "\" Java, - , ,

String temp = "0|0";
String[] splitStrings = temp.split("\\|");
+2

Guava Splitter, String.split(). , (, "|" ) , , ( , ..)..).

,

Iterable<String> parts = Spliter.on('|').trimResults().omitEmptyStrings().split("0|0")
+2

:

([0-9]+)
0

, csv xls .

.

         
    { ...
    ....
    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 the code fragment is a call to the StringSplitToArray method, where the string is converted into a string array in the method, dividing the string depending on the specified separator or passed to the method. The delimiter can be a comma delimiter (,) or a double code (").

For more on this follow the link : http://scrapillars.blogspot.in

0
source

All Articles