英文:
Split string on multiple white space not on single space?
问题
我正在从文本文件中读取文本并将其保存到数据库中。数据排列得像一个虚拟的表格,有多列。我需要使用多个空格来分割每一行,我尝试了这段代码
String[] arrayofStr = line.split("\\s+");
它会使用每个空格来分割字符串。还有一个问题需要解决,我将在文本文件中粘贴文本行。
在第一行中,我需要将文本“Pipe, Smls, be”作为单个字符串读取,而在第五行和第六行中,出现了空格,而前一行在该位置有数据,我需要在数据库中为该位置插入空数据。
英文:
I am reading text from a text file and saving it to the database. The data is sorted like a imaginary table with columns. I need to split each line with multiple white space, I tried this code
String[] arrayofStr = line.split("\\s+");
It splits the string with every white space, And one more issue to solve is, I will paste the lines of text in the text file.
P11570 24311VG10281-01 1 011441-X SPL-01 1.1 7430030711 FAB 2 0.4 M PIPE 5 938 2448 1465 2448 PIPE, SMLS, BE, 80, ASTM A106 GR.B,SOUR SERVICE LC1-N 2"-VG-10281-011441-X-N
P11570 24311VG10281-01 1 011441-X SPL-01 1.1 7430030711 FAB 2 0.4 M PIPE 2 938 2448 1465 2448 PIPE, SMLS, BE, 80, ASTM A106 GR.B,SOUR SERVICE LC1-N 2"-VG-10281-011441-X-N
P11570 24311VG10281-01 1 011441-X 1.1 8543603141 EREC 5/8 2 BOLTS PIPE, SMLS, BE, 80, ASTM A106 GR.B,SOUR SERVICE LC1-N 2"-VG-10281-011441-X-N
For more clarity i will add a screen shot too.
> In the first line I need to read the text Pipe,Smls, be etc as single string and in the 5th and 6th line there appear blank spaces where as the previous line have data in that position, I need to insert null data for that position in the database.
答案1
得分: 1
这是我所谈论的一个示例。
- 您需要确保将选项卡转换为空格,同时保留列位置。
- 由于选项卡和空格交错使用,最简单的解决方案是目测列起始位置,然后手动输入它们到一个数组中。如果您创建一个如下所示的指南,这将变得非常容易。
- 然后只需读取行并使用列位置拆分它们。
- “数据”后面跟着列号,或者如果在同一列中分组,跟着数字和一个字母。
String[] data = {
// 1111111111222222222233333333334444444444555555555566666666667777777777
// 01234567890123456789012345678901234567890123456789012345678901234567890123456789
"Data1 Data2 Data3 Data4 Data5a Data5b Data5c Data6 Data7 Data8",
"Data1 Data2 Data3 Data4 Data5a Data5b Data5c Data6 Data7 ",
"Data1 Data2 Data3 Data6 Data7 Data8",
"Data1 Data2 Data4 Data5a Data5b Data5c Data6 Data7 Data8",
};
// 最后一个条目是行的字符串长度
int[] columnStarts = { 0, 7, 18, 26, 34, 58, 64, 74, 79};
for (String line : data) {
int columnNumber = 0;
for (int i = 0; i < columnStarts.length - 1; i++) {
System.out.printf("%3d : %3d -- '%s'%n",
(columnNumber + 1),
columnStarts[columnNumber],
line.substring(columnStarts[i],
columnStarts[i + 1]).trim());
columnNumber++;
}
System.out.println();
}
打印输出
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- ''
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- ''
5 : 34 -- ''
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- ''
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
注意,数据已修剪并打印,以仅显示列的数据部分。如果不修剪空格,数据将显示每列的尾随空格。
以上内容足以让您将信息存储在数组或列表中,并根据列号进行修改。
英文:
Here is an example of what I was talking about.
- You need to ensure that your tabs are converted to spaces while preserving the column locations.
- Because tabs and spaces are intermixed, the easiest solution is to eye the column starts and manually enter them into an array. If you make a guide as shown below this is trivial to do.
- Then just read in the lines and split them using the column locations.
- The "data" is followed by the column number, or if grouped in the same column, the number and a letter.
String[] data = {
// 1111111111222222222233333333334444444444555555555566666666667777777777
// 01234567890123456789012345678901234567890123456789012345678901234567890123456789
"Data1 Data2 Data3 Data4 Data5a Data5b Data5c Data6 Data7 Data8",
"Data1 Data2 Data3 Data4 Data5a Data5b Data5c Data6 Data7 ",
"Data1 Data2 Data3 Data6 Data7 Data8",
"Data1 Data2 Data4 Data5a Data5b Data5c Data6 Data7 Data8",
};
// last entry is string length of the line
int[] columnStarts = { 0, 7, 18, 26, 34, 58, 64, 74, 79};
for (String line : data) {
int columnNumber = 0;
for (int i = 0; i < columnStarts.length - 1; i++) {
System.out.printf("%3d : %3d -- '%s'%n",
(columnNumber + 1),
columnStarts[columnNumber],
line.substring(columnStarts[i],
columnStarts[i + 1]).trim());
columnNumber++;
}
System.out.println();
}
Prints
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- ''
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- 'Data3'
4 : 26 -- ''
5 : 34 -- ''
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
1 : 0 -- 'Data1'
2 : 7 -- 'Data2'
3 : 18 -- ''
4 : 26 -- 'Data4'
5 : 34 -- 'Data5a Data5b Data5c'
6 : 58 -- 'Data6'
7 : 64 -- 'Data7'
8 : 74 -- 'Data8'
Note that the Data is trimmed and printed to show just the data portion of the column. Without the white space trimming, the data would show trailing white space for each column.
The above should be enough for you to store the information in an array or list and modify it based on column number.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论