如何使用java split()方法分割以下字符串?

时间:2018-07-23 05:15:39

标签: java

 String data = 
    "Filesystem           1K-blocks      Used Available Use% Mounted on
    /dev/sda8              2064208    407212   1552140  21% /
    tmpfs                  4058672         0   4058672   0% /dev/shm
    /dev/sda1              1034096     62636    918932   7% /boot
    /dev/sda11             1032088    117684    861976  13% /home
    /dev/sda6              6551480   5514296    704384  89% /opt
    /dev/sda2            203145268 165930964  26728680  87% /t24bmifs
    /dev/sda7              5160576    141484   4756948   3% /tmp
    /dev/sda3             15239564  13005132   1460288  90% /usr
    /dev/sda9              2064208     68760   1890592   4% /usr/local
    /dev/sda10             2064208   1811884    147468  93% /var
    /dev/mapper/t24linfs-t24linlv
                         2113783728 1622849504 383560248  81% /t24linfs
    /dev/mapper/oracfsvg-oracfsvl
                         1909423812 1372203712 440227028  76% /oraclefs"

下面是参考代码

   public void setData(String procText)
{
    try
    {
        DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(procText));
        Document xDoc = docBuilder.parse(is);
        XPath xPath = XPathFactory.newInstance().newXPath();
        String status = (String) xPath.evaluate("//status", xDoc, XPathConstants.STRING);
        if(status.compareTo("OK")!=0)
        {
            //show error                
        }
        else
        {
            String data = (String) xPath.evaluate("//data", xDoc, XPathConstants.STRING);
            String[] lines = data.split("\n", 0);
            String line = "";                        

                        model.addRow(new Object[]{ procData[0], procData[1], procData[2], procData[3], procData[4], procData[5] } );                                                

                }

            jTable1.setAutoCreateRowSorter(true);
            jTable1.setModel(model);

    }
    catch(Exception ex)
    {
        System.out.println("Exception - " + ex.getClass().getName()+":"+ ex.getMessage());
    }
    }

任何人都请帮助我如何分割此字符串数据。我可以  使用split("\n",0)分割前11个值。但是最后两个值,我  不知道如何进行。我将分割字符串并分配  将其转换为字符串数组,然后再次使用  space(“ \ s”)并将其传递给object []以将其显示为表格  对话框中的格式。

2 个答案:

答案 0 :(得分:0)

尝试使用此正则表达式^\s+([\w-\/]+)+\s+\d+\s+\d+\s+\d+\s+\d+%\s+(\/[\w-]*)+

这有点难看,但它会匹配您的每行。对于任何特殊情况,您都可以轻松地对其进行纠正。

像这样在Java中使用它:

String[] lines = str.split("^\s+([\w-\/]+)+\s+\d+\s+\d+\s+\d+\s+\d+%\s+(\/[\w-]*)+");

现在您应该在lines数组中放置每一行。

答案 1 :(得分:0)

我试过了,有点冗长。但是您将可以控制处理方式。例如,第一标题行具有七个令牌,其余六个令牌。为了进行测试,我使用了帖子中的文本(原样),并在Windows记事本中创建了一个文本文件。

import java.util.*;
import java.io.*;
public class StringSplitTest {
    private static List<String> words = new ArrayList<>();
    private static String word = "";
    private static boolean wordFlag = true;
    public static void main(String [] args) throws IOException {
        // Read file and create word tokens
        FileReader reader = new FileReader("test.txt");
        int c;
        while ((c = reader.read()) != -1) {
            makeWords((char) c);
        }
        reader.close();
        // Process tokens to get result
        int n = 0; // tracks count of words on a line
        List<String> line = new ArrayList<>();
        for (int i = 0; i < words.size(); i++) {
            if (i < 7) {
                // The first header line has 7 tokens
                // ignore for now
                continue;
            }
            // Process remaining lines (6 tokens for each line)
            if (++n == 7) {
                System.out.println(line); // prints a line
                n = 1;
                line = new ArrayList<>();
            }
            line.add(words.get(i));
        }
        System.out.println(line); // prints last line
    }
    /*
     * Processes all text (a character at a time and stores them as
     * word tokens in a List<String>. Uses whitespaces as delimiter.
     * NOTE: The whitespace as defined in the Character.isWhitespace()
     * https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html.
     */
    private static void makeWords(char c) {
        if (! Character.isWhitespace(c)) {
            if (! wordFlag) {
                wordFlag = true;
                word = "";
            }
            word = word + String.valueOf(c);
        }   
        else {
            if (wordFlag) {
                wordFlag = false;
                words.add(word);
            }
        }
    }
}