简体   繁体   English

从String中删除项目

[英]Removing items from String

I am trying to replace all occurrences of a substring from a String. 我试图从String中替换所有出现的子字符串。

I want to replace "\\t\\t\\t" with "<3tabs>" 我想用"<3tabs>"替换"\\t\\t\\t" "<3tabs>"
I want to replace "\\t\\t\\t\\t\\t\\t" with "<6tabs>" 我想用"<6tabs>"替换"\\t\\t\\t\\t\\t\\t" "<6tabs>"
I want to replace "\\t\\t\\t\\t" with "< >" 我想用"< >"替换"\\t\\t\\t\\t" "< >"

I am using 我在用

s = s.replace("\t\t\t\t", "<    >");
s = s.replace("\t\t\t", "<3tabs>");
s = s.replace("\t\t\t\t\t\t", "<6tabs>");

But no use, it does not replace anything, then i tried using 但没有用,它不会取代任何东西,然后我尝试使用

s = s.replaceAll("\t\t\t\t", "<    >");
s = s.replaceAll("\t\t\t", "<3tabs>");
s = s.replaceAll("\t\t\t\t\t\t", "<6tabs>");

Again, no use, it does not replace anything. 再一次,没有用,它不会取代任何东西。 after trying these two methods i tried StringBuilder 尝试这两种方法后我尝试了StringBuilder

I was able to replace the items through StringBuilder , My Question is, why am i unable to replace the items directly through String from the above two commands? 我能够通过StringBuilder替换项目,我的问题是,为什么我无法直接通过上述两个命令中的String替换项目? Is there any method from which i can directly replace items from String? 有什么方法可以直接替换String中的项目吗?

try in this order 按此顺序尝试

String s = "This\t\t\t\t\t\tis\t\t\texample\t\t\t\t";
s = s.replace("\t\t\t\t\t\t", "<6tabs>");
s = s.replace("\t\t\t\t", "<    >");
s = s.replace("\t\t\t", "<3tabs>");
System.out.print(s);

output: 输出:

This<6tabs>is<3tabs>example<    >

6tabs is never going to find a match as the check before it will have already replaced them with two 3tabs. 6tabs永远不会找到匹配,因为它之前的检查已经用两个3tabs替换它们。

You need to start with largest match first. 你需要先从最大的匹配开始。

Strings are immutable so you can't directly modify them, s.replace() returns a new String with the modifications present in it. 字符串是不可变的,所以你不能直接修改它们,s.replace()返回一个新的String,其中包含修改。 You then assign that back to s though so it should work fine. 然后你将它分配回s,所以它应该工作正常。

Put things in the correct order and step through it with a debugger to see what is happening. 按正确顺序排列并使用调试器逐步完成,以查看发生的情况。

Take a look at this 看看这个

Go through your text, divide it into a char[] array, then use a for loop to go through the individual characters. 浏览文本,将其分成char []数组,然后使用for循环遍历各个字符。 Don't print them out straight, but print them using a %x tag (or %d if you like decimal numbers). 不要直接打印出来,而是使用%x标签打印它们(如果你喜欢十进制数字,则打印%d)。

char[] characters = myString.tocharArray();  
for (char c : characters)  
{  
   System.out.printf("%x%n", c);  
}  

Get an ASCII table and look up all the numbers for the characters, and see whether there are any \\n or \\f or \\r. 获取ASCII表并查找字符的所有数字,并查看是否有\\ n或\\ f或\\ r \\ n。 Do this before or after. 在此之前或之后这样做。

Different operating systems use different line terminating characters; 不同的操作系统使用不同的行终止字符; this is the first reference I found from Google with "line terminator Linux Windows." 这是我从谷歌发现的第一个“行终结者Linux Windows”。 It says Windows uses \\r\\f and Linux \\f. 它说Windows使用\\ r \\ f和Linux \\ f。 You should find that out from your example. 你应该从你的例子中找到它。 Obviously if you strip \\n and leave \\r you will still have the text break into separate lines. 显然,如果你去掉\\ n并离开\\ r \\ n,你仍然会将文本分成不同的行。

You might be more successful if you write a regular expression (see this part of the Java Tutorial, etc) which includes whitespace and line terminators, and use it as a delimiter with the String.split() method, then print the individual tokens in order. 如果你编写一个包含空格和行终止符的正则表达式(请参阅Java教程的这一部分等),并使用它作为String.split()方法的分隔符,然后打印单个标记,则可能会更成功。订购。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM