简体   繁体   中英

Replacing strings in java question

Say I have a java source file saved into a String variable like so.

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

And say there is a line of text in the source file like so

String x = "Hello World\n";

Notice the newline character at the end.

In my code, I know of the existence of Hello World , but not Hello World\n so therefore a call to

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

will fail because of that newline character. How can I make it so it will match in the case of one or many newline characters? Could this be a regular expression I add to the end search variable?

EDIT:

I am replacing string literals with variables. I know the literals, but I dont know if they have a newline character or not. Here is an example of the code before my replacement. For the replacment, I know that application running at a time. exists, but not 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);

And here is an example after my replacement

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);

Notice that all of the literals without newlines get replaced, such as "Multiple Instances of weh Detected" is now "MULTIPLE_INSTANCES_OF" but all of the ones with new lines do not. I am thinking that there is some regular expression I can add on to handle one or many newline characters when it tries to the replace all.

well since its actually a regular expression that is passed as the first argument you could try something like this as the first argument

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

which will search for hello world everywhere in the start and the end of a line wether it has a \n or not.

a bit redundant and as stated it should not matter but... apparantly it does

try it (made it nifty so it matches capital as well as regular letters:P... just overdoin it)

If you're only replacing string literals, and you're ok with replacing every occurrence of the literal (not just the first one) then you should use the replace method instead of replaceAll.

Your first example should change to this:

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

The replaceAll does a regular expression replacement instead of a string literal replacement. This is generally slower, and is not strictly necessary for your use case.

Note that this answer assumes that the trailing newline characters can be left in the string (which you've said you are ok with in the comments).

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

\1 captures the first group, marked by (...), counting from the opening parenthesis. \n+ is \n for newline, but the backslash needs to be masked in Java, leading to two backslashes. * means 0 to n, so it would catch 0, 1, 2, ... newlines.

As per your question you need to pass following charator

public String replaceAll(String regex, String replacement)

The two parameters are –

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

Some other method is: -

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

example is

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);

}

}

As the commenters noted, the \n should not mess this up. However, if you are ok with removing the new lines, you can try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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