[英]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"
Below is the code for reference 下面是参考代码
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());
}
}
Anyone please help me how can i split this string data. 任何人都请帮助我如何分割此字符串数据。 I can able to split first 11 values using
split("\\n",0)
. 我可以使用
split("\\n",0)
分割前11个值。 But the last two values, I don't know how to proceed. 但是最后两个值,我不知道如何进行。 I will be splitting the string and assign it to a string array and again i will split the string array using space("\\s") and will pass it to an object[] to display it as a table format in a dialog box.
我将拆分字符串并将其分配给字符串数组,然后再次使用space(“ \\ s”)拆分字符串数组,并将其传递给object []以在对话框中以表格格式显示。
Try this regex ^\\s+([\\w-\\/]+)+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+%\\s+(\\/[\\w-]*)+
试试这个正则表达式
^\\s+([\\w-\\/]+)+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+%\\s+(\\/[\\w-]*)+
It is a bit ugly but it will match each of your lines. 这有点丑陋,但是会与您的每行匹配。 You can easily correct it for any exceptional cases you have.
对于任何特殊情况,您都可以轻松地对其进行更正。
Use it in java like this: 在Java中像这样使用它:
String[] lines = str.split("^\s+([\w-\/]+)+\s+\d+\s+\d+\s+\d+\s+\d+%\s+(\/[\w-]*)+");
Now you should have each of your lines in the lines array. 现在,您应该在lines数组中包含每一行。
I tried this and its a bit verbose. 我试过了,有点冗长。 But you will have some control over how you can process.
但是您将可以控制处理方式。 For example, the first header line has seven tokens and the rest six.
例如,第一标题行具有七个令牌,其余六个令牌。 For testing I used the text from the post (as it is) and created a text file in a Windows Notepad.
为了进行测试,我使用了帖子中的文本(原样),并在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.