分割字符串时,多个空格上分割,不要在单个空格上分割?

huangapple go评论77阅读模式
英文:

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
&quot;Data1  Data2      Data3   Data4   Data5a  Data5b  Data5c  Data6 Data7     Data8&quot;, 
&quot;Data1  Data2      Data3   Data4   Data5a  Data5b  Data5c  Data6 Data7          &quot;, 
&quot;Data1  Data2      Data3                                   Data6 Data7     Data8&quot;, 
&quot;Data1  Data2              Data4   Data5a  Data5b  Data5c  Data6 Data7     Data8&quot;,
};
// 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 &lt; columnStarts.length - 1; i++) {
System.out.printf(&quot;%3d : %3d  -- &#39;%s&#39;%n&quot;,
(columnNumber + 1),
columnStarts[columnNumber],
line.substring(columnStarts[i],
columnStarts[i + 1]).trim());
columnNumber++;
}
System.out.println();
}

Prints

  1 :   0  -- &#39;Data1&#39;
2 :   7  -- &#39;Data2&#39;
3 :  18  -- &#39;Data3&#39;
4 :  26  -- &#39;Data4&#39;
5 :  34  -- &#39;Data5a  Data5b  Data5c&#39;
6 :  58  -- &#39;Data6&#39;
7 :  64  -- &#39;Data7&#39;
8 :  74  -- &#39;Data8&#39;
1 :   0  -- &#39;Data1&#39;
2 :   7  -- &#39;Data2&#39;
3 :  18  -- &#39;Data3&#39;
4 :  26  -- &#39;Data4&#39;
5 :  34  -- &#39;Data5a  Data5b  Data5c&#39;
6 :  58  -- &#39;Data6&#39;
7 :  64  -- &#39;Data7&#39;
8 :  74  -- &#39;&#39;
1 :   0  -- &#39;Data1&#39;
2 :   7  -- &#39;Data2&#39;
3 :  18  -- &#39;Data3&#39;
4 :  26  -- &#39;&#39;
5 :  34  -- &#39;&#39;
6 :  58  -- &#39;Data6&#39;
7 :  64  -- &#39;Data7&#39;
8 :  74  -- &#39;Data8&#39;
1 :   0  -- &#39;Data1&#39;
2 :   7  -- &#39;Data2&#39;
3 :  18  -- &#39;&#39;
4 :  26  -- &#39;Data4&#39;
5 :  34  -- &#39;Data5a  Data5b  Data5c&#39;
6 :  58  -- &#39;Data6&#39;
7 :  64  -- &#39;Data7&#39;
8 :  74  -- &#39;Data8&#39;

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.

huangapple
  • 本文由 发表于 2020年8月6日 01:38:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63270655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定