2011-04-27 70 views
1

说我有一个java源文件保存到像这样的字符串变量。在java问题中替换字符串

String contents = Utils.getTextFromFile(new File(fileName)); 

说,目前仍然在源文件中的文字,像这样

String x = "Hello World\n"; 

通知末尾的换行符行。

在我的代码,我知道的Hello World的存在,但不Hello World\n 等于是一个电话

String search = "Hello World"; 
contents = contents.replaceAll(search, "Something Else"); 

将失败,因为该换行符。我怎样才能让它匹配一个或多个换行符?难道这是一个正则表达式我添加到结尾search变量?

编辑:

我用变量替换字符串文字。我知道文字,但我不知道他们是否有换行符。这是我替换之前的代码示例。对于replacment,我知道application running at a time.存在,但不是application running at a time.\n\n

int option = JOptionPane.showConfirmDialog(null,"There is another application running. There can only be one application\n" + 
"application running at a time.\n\n" + 
"Press OK to close the other application\n" + 
"Press Cancel to close this application", 
"Multiple Instances of weh detected", 
JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE); 

这里是我的更换

int option = JOptionPane.showConfirmDialog(null,"There is another application running. There can only be one application\n" + 
"application running at a time.\n\n" + 
"Press OK to close the other application\n" + 
"PRESS_CANCEL_TO_CLOSE", 
"MULTIPLE_INSTANCES_OF", 
JOptionPane.OK_CANCEL_OPTION, 
JOptionPane.ERROR_MESSAGE); 

请注意,所有不带换行的文字中被替换后的例子,如"Multiple Instances of weh Detected"是现在"MULTIPLE_INSTANCES_OF",但所有的新行都没有。我在考虑有一些正则表达式可以添加到它处理一个或多个换行符时尝试全部替换。

+0

应该不存在'\ n'存在。你是否也想要删除'\ n'? – Jeremy 2011-04-27 17:14:04

+0

出于某种原因,所有没有换行符的'Hello World's都被替换了,但所有那些都没有。 – user663321 2011-04-27 17:14:48

+0

'contents = contents.replaceAll(“Hello World”,“Something Else”);'应该让您的示例文本为“Something Else \ n”。你想它只是“别的东西”吗? – 2011-04-27 17:15:19

回答

0

正如评论者所说,\ n不应该搞砸了。但是,如果你可以删除新行,你可以试试这个:

contents = contents.replaceAll(“search”+“\\ n *”,“Something Else”);

1

很好,因为它实际上是一个正则表达式,如你可以尝试这样的事情作为第一个参数

String search = "[^]?[H|h]ello [W|w]orld[\n|$]?" 

这会为你好世界各地开始搜索的第一个参数和的年底通过它有一个\ n或不是。

有点多余和说,它不应该的问题,但... apparantly它

尝试它(使它漂亮,使其符合资本以及普通字母:P ...只是overdoin吧)

+0

[\ n | $]?如果放置为[$]​​,甚至会更好?因为\ n被识别为行($)的结尾。 – youri 2011-04-27 17:40:38

1

如果您只替换字符串文字,并且您可以更换每个出现的文字(不只是第一个字符),那么您应该使用replace method而不是replaceAll。

你的第一个例子应该改成这样:

String search = "Hello World"; 
contents = contents.replace(search, "Something Else"); 

该做的replaceAll正则表达式替换,而不是一个字符串替换。这通常比较慢,对于您的用例来说不是必需的。

请注意,这个答案假设尾随的换行符可以留在字符串中(你说你在评论中可以使用)。

1
String search = "Hello World\n\n\n";       
search.replaceAll ("Hello World(\\n*)", "Guten Morgen\1"); 

\ 1捕获第一组,由(......)标记,从左括号开始计数。 \ n +对于换行符是\ n,但反斜线需要在Java中被屏蔽,导致出现两个反斜杠。 *表示0到n,所以它会捕获0,1,2,...新行。

0

根据你的问题,你需要通过以下charator

公共字符串的replaceAll(字符串的正则表达式,字符串替换)

这两个参数 -

regex – the regular expression to match 
replacement – the string to be substituted for every match 

其他一些方法: -

replace(char oldChar, char newChar) 
replace(CharSequence target, CharSequence replacement) 
replaceFirst(String regex, String replacement) 

示例为

import java.lang.String; 

public class StringReplaceAllExample { 

public static void main(String[] args) { 

String str = "Introduction 1231 to 124 basic 1243 programming 34563 concepts 5455"; 

String Str1 = str.replaceAll("[0-9]+", ""); 

System.out.println(Str1); 

Str1 = str.replaceAll("[a-zA-Z]+", "Java"); 

System.out.println(Str1); 

} 

}