删除字符串中的空格字符

时间:2021-03-17 20:14:26

I am reading each line in the text file, do some with the file and write back to same file using Java. And, position(index) of each value in the line is important. So I need to preserve the location of the each value.

我正在读取文本文件中的每一行,对文件执行一些操作并使用Java写回同一文件。并且,行中每个值的位置(索引)很重要。所以我需要保留每个值的位置。

How do I remove a "space" character at specific location(index)?

如何删除特定位置(索引)处的“空格”字符?

Say, below is the line that I read,

比如,下面是我读过的那一行,

.... ABC   123....  --There are 3 spaces between ABC and 123

Basically, I want to make the above line written as 2 spaces between ABC and 123.

基本上,我想将上面的行写成ABC和123之间的2个空格。

At first, I was just using replaceAll of String but that just shift the the values to right by one and still 3 spaces. So, I figure I need more than just replaceAll.

起初,我只是使用了String的replaceAll,但只是将值向右移动了一个仍然是3个空格。所以,我认为我需要的不仅仅是replaceAll。

Now, I am getting a position of where ABC is found and just trying to remove that 1 space.

现在,我正在找到ABC的位置,并试图删除该1个空格。

4 个解决方案

#1


0  

If you want to remove a single character, just use StringBuilder.deleteCharAt.

如果要删除单个字符,只需使用StringBuilder.deleteCharAt。

final int pos = ...;
str = new StringBuilder(str).deleteCharAt(pos).toString();

I explicitly advise you not to do the substring approach.

我明确建议你不要做子串方法。

#2


0  

Can you not do a search for the whole string? The first parameter has three spaces and the second has two spaces

你能不搜索整个字符串吗?第一个参数有三个空格,第二个参数有两个空格

replaceAll("ABC   123", "ABC  123");

or if ABC is your key input

或者如果ABC是您的关键输入

replaceAll("ABC   ", "ABC  ");

and the first parameter has 3 spaces and the second two after the ABC

第一个参数有3个空格,第二个参数有ABC

#3


0  

Since you know the position n (zero based) of string s that you want to remove:

由于您知道要删除的字符串s的位置n(从零开始):

s = s.substring(0, n) + s.substring(n + 1);

#4


0  

You could just convert your string to a char array and loop it, but it's not a nice way to do it.

您可以将您的字符串转换为char数组并循环它,但这不是一个很好的方法。

Or you could do:

或者你可以这样做:

myString = myString.substring(0,(position of first space))+
                myString.substring((position of first space)+1, myString.length);

or something like that

或类似的东西

#1


0  

If you want to remove a single character, just use StringBuilder.deleteCharAt.

如果要删除单个字符,只需使用StringBuilder.deleteCharAt。

final int pos = ...;
str = new StringBuilder(str).deleteCharAt(pos).toString();

I explicitly advise you not to do the substring approach.

我明确建议你不要做子串方法。

#2


0  

Can you not do a search for the whole string? The first parameter has three spaces and the second has two spaces

你能不搜索整个字符串吗?第一个参数有三个空格,第二个参数有两个空格

replaceAll("ABC   123", "ABC  123");

or if ABC is your key input

或者如果ABC是您的关键输入

replaceAll("ABC   ", "ABC  ");

and the first parameter has 3 spaces and the second two after the ABC

第一个参数有3个空格,第二个参数有ABC

#3


0  

Since you know the position n (zero based) of string s that you want to remove:

由于您知道要删除的字符串s的位置n(从零开始):

s = s.substring(0, n) + s.substring(n + 1);

#4


0  

You could just convert your string to a char array and loop it, but it's not a nice way to do it.

您可以将您的字符串转换为char数组并循环它,但这不是一个很好的方法。

Or you could do:

或者你可以这样做:

myString = myString.substring(0,(position of first space))+
                myString.substring((position of first space)+1, myString.length);

or something like that

或类似的东西