[英]How to split the below string using java split() method?
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 []以在對話框中以表格格式顯示。
試試這個正則表達式^\\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數組中包含每一行。
我試過了,有點冗長。 但是您將可以控制處理方式。 例如,第一標題行具有七個令牌,其余六個令牌。 為了進行測試,我使用了帖子中的文本(原樣),並在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);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.