Reputation: 782
I have an array of special characters like the following
String[] special = {
"\", ":", "/"
};
What I want to be able to do is loop through a given
string and if I encounter any of the characters in the special
array, I want to add an escape \\
in front of that character. Please see the following input below
Input: Hello : Good : Day
Output Hello \\: Good : Day
Expected: Hello\\:Good\\:Day
As you can see, after it sees the first instance of a special
character, it does not check for any other. Any help is appreciated. Code is below
String[] special = { "\", ":", "/" };
public String specialCheck(String replaceMe) {
String cleanMe = replaceMe.replace(" ", "");
StringBuilder stringBuilder = new StringBuilder(cleanMe);
for (String badChars : special) {
if (cleanMe.contains(badChars)) {
stringBuilder.insert(cleanMe.indexOf(badChars) , "\\\\");
}
}
return stringBuilder.toString();
}
Upvotes: 0
Views: 429
Reputation: 868
This is a possible answer to your problem.
I made it only using Strings and ArrayLists and it is as simple as possible (Even dumb lol, but it's working).
I don't use StringBuilders because they are mutable and for the case, they are difficult to handle.
Improve it as you wish (I am not using replace(" ", "") by the way), but I saw you may want to use it. Regards.
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
System.out.println(specialCheck("Hello : Good : Day / Test $ for : the @ problem"));
}
public static String specialCheck(String replaceMe) {
String[] special = { "\\", ":", "/", "$", "@"};
ArrayList<Integer> positions = new ArrayList<Integer>();
ArrayList<String> phrase = new ArrayList<String>();
for(int i = 0 ; i < replaceMe.length(); i ++) {
phrase.add(String.valueOf(replaceMe.charAt(i)));
for(int j = 0; j < special.length; j++) {
if(String.valueOf(replaceMe.charAt(i)).equals(special[j].toString())) {
positions.add(i);
}
}
}
for(int i = 0; i < phrase.size(); i++) {
for(int j = 0; j < special.length; j++) {
if(phrase.get(i).equals(special[j])) {
phrase.remove(i);
phrase.add(i, "\\\\" + special[j] );
}
}
}
String result = "";
for(int i = 0; i < phrase.size(); i++) {
result = result + phrase.get(i);
}
return result;
}
}
Result for the test I made: Hello \\: Good \\: Day \\/ Test \\$ for \\: the \\@ problem
Upvotes: 1
Reputation: 78985
Do it as follows:
public class Main {
public static void main(String[] args) {
String str="Hello : Good : Day";
System.out.println(str.replaceAll(":", "\\\\\\\\:"));
}
}
Output:
Hello \\: Good \\: Day
Note:
String:replaceAll
to replace all occurrences.\\
to escape \
[Update]
Posting the following update based on OP's comment:
public class Main {
public static void main(String[] args) {
// Test
System.out.println(getEscapedString("Hello : Good : Day"));
System.out.println(getEscapedString("Hello / Good / Day"));
System.out.println(getEscapedString("Hello \\ Good \\ Day"));
}
static String getEscapedString(String str) {
return str.replaceAll("[:/\\\\]+", "\\\\\\\\$0");
}
}
Output:
Hello \\: Good \\: Day
Hello \\/ Good \\/ Day
Hello \\\ Good \\\ Day
Upvotes: 2
Reputation: 3969
String result = replaceMe
for( i = 0 ; i < special.length ; i ++)
result = result.replaceAll(special[i], "////" + special[i]);
Upvotes: 0