String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i++;", "");
// Desired output :: newCode = "helloworld";
But this is not replacing i++ with blank.
just use replace() instead of replaceAll()
String preCode = "helloi++;world";
String newCode = preCode.replace("i++;", "");
or if you want replaceAll(), apply following regex
String preCode = "helloi++;world";
String newCode = preCode.replaceAll("i\\+\\+;", "");
Note : in the case of replace() the first argument is a character sequence, but in the case of replaceAll the first argument is regex
try this one
public class Practice {
public static void main(String...args) {
String preCode = "Helloi++;world";
String newCode = preCode.replace(String.valueOf("i++;"),"");
System.out.println(newCode);
}
}
The problem is the string that you are using to replace , that is cnsidered as regex pattern to skip the meaning you will have to use escape sequence like below.
String newCode = preCode.replaceAll("i\\+\\+;", "");
Related
I want to change all letters from a string to "-" char except space using Java.
I tried:
String out = secretWord.replaceAll("^ " , "-");
and
String out = secretWord.replaceAll("\\s" , "-");
They didn't work.
I tried:
String newWord = secretWord.replaceAll("[A-Z]" , "-");
It worked but i didn't change Turkish characters I use in that string.
Original Code:
public class ChangeToLine {
public static void main(String[] args) {
String originalWord = "ABİDİKUŞ GUBİDİKUŞ";
String secretWord = originalWord;
}
}
You can use the \\S regex:
String s = "Sonra görüşürüz";
String replaced = s.replaceAll("\\S", "-");
System.out.println(replaced); // outputs ----- ---------
Use a character class
String out = secretWord.replaceAll("[^ ]" , "-");
or a capital S, instead of a lower s to replace all non space chars
String out2 = secretWord.replaceAll("\\S" , "-");
NOT needs to be expressed in square brackets in java.util.regex.Pattern:
String out = secretWord.replaceAll("[^\\s]", "-")
I'm just trying to remove (replace with "") \r and \n from my JSON. Here is the method I'm currently testing which doesn't work.
public static void testing(){
String string = "\r\r\r\n\n\n";
string.replace("\r", "");
string.replace("\n", "");
}
Try this regex (\\r\\n|\\n|\\r) and String#replaceAll, like:
string = string.replaceAll("(\\r\\n|\\n|\\r)", "");
After replacing you need to assign back to the original string. Because the string is immutable you cannot change the value of a string.
You need to use
String string = "\r\r\r\n\n\n";
string = string.replace("\r", "");
string = string.replace("\n", "");
Or you can use any libraries like Apache StringUtils.If you are using these utils , no needs to assign back the value to String
try this:
public static void testing(){
String string = "\r\r\r\n\n\n";
string = string.replace("\r", "");
string = string.replace("\n", "");
}
because replace return another string(new String) because String is immutable so unable to modified directly
String.replace will return a string. It doesn't change its value.
public static void testing(){
String str = "\r\r\r\n\n\n";
str = str.replace("\r", "");
str = str.replace("\n", "");
}
String string = "\r\r\r\n\n\n";
String newStr = string.replace("\r", "");
newStr = newStr.replace("\n", "");
System.out.println(newStr);
String will return new String Object.
try this:
String string = "\rte\r\rs\nti\nn\ng";
String newString = string.replaceAll("[^\\w]", "");
String string = "\r\r\r\n\n\n";
string = string.replaceAll("\r", "");
string = string.replaceAll("\n", "");
Try this,
string = string.replaceAll("[\r\n]", "");
Here is what I found:
data_json = data_json.replaceAll("\\\\r\\\\n", "");
Copied from OP's comment.
I have a string String a = "(3e4+2e2)sin(30)"; and i want to show it as a = "(3e4+2e2)*sin(30)";
I am not able to write a regular expression for this.
Try this replaceAll:
a = a.replaceAll("\) *(\\w+)", ")*$1");
You can go with this
String func = "sin";// or any function you want like cos.
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)]" + func, ")*siz");
System.out.println(a);
this should work
a = a.replaceAll("\\)(\\s)*([^*+/-])", ") * $2");
String input = "(3e4+2e2)sin(30)".replaceAll("(\\(.+?\\))(.+)", "$1*$2"); //(3e4+2e2)*sin(30)
Assuming the characters within the first parenthesis will always be in similar pattern, you can split this string into two at the position where you would like to insert the character and then form the final string by appending the first half of the string, new character and second half of the string.
string a = "(3e4+2e2)sin(30)";
string[] splitArray1 = Regex.Split(a, #"^\(\w+[+]\w+\)");
string[] splitArray2 = Regex.Split(a, #"\w+\([0-9]+\)$");
string updatedInput = splitArray2[0] + "*" + splitArray1[1];
Console.WriteLine("Input = {0} Output = {1}", a, updatedInput);
I did not try but the following should work
String a = "(3e4+2e2)sin(30)";
a = a.replaceAll("[)](\\w+)", ")*$1");
System.out.println(a);
Example:
Input
Str = P.O.Box
Output
Str= PO BOX
I can able to convert the string to uppercase and replace all dot(.) with a space.
public static void main(String args[]){
String s = "P.O.Box 1836";
String uppercase = s.toUpperCase();
System.out.println("uppercase "+uppercase);
String replace = uppercase.replace("."," ");
System.out.println("replace "+replace);
}
System.out.print(s.toUpperCase().replaceFirst("[.]", "").replaceAll("[.]"," "));
If you look the String API carefully, you would notice that there's a methods that goes by:-
replaceFirst(String regex, String replacement)
Hope it helps.
You have to use the replaceFirst method twice. First for replacing the . with <nothing>. Second for replacing the second . with a <space>.
String str = "P.O.Box";
str = str.replaceFirst("[.]", "");
System.out.println(str.replaceFirst("[.]", " "));
This one liner should do the job:
String s = "P.O.Box";
String replace = s.toUpperCase().replaceAll("\\.(?=[^.]*\\.)", "").replace('.', ' ');
//=> PO BOX
String resultValue = "";
String[] result = uppercase.split("[.]");
for (String value : result)
{
if (value.toCharArray().length > 1)
{
resultValue = resultValue + " " + value;
}
else
{
resultValue = resultValue + value;
}
}
Try this
System.out.println("P.O.Box".toUpperCase().replaceFirst("\\.","").replaceAll("\\."," "));
Out put
PO BOX
NOTE: \\ is needed here if you just use . only your out put will blank.
Live demo.
You should use replaceFirst method twice.
String replace = uppercase.replace("\\.", "").replaceFirst("\\.", "");
As you want to remove the first dot and replace the second one with a space, you need replace the whole P.O. section
Use
replace("P\\.O\\.", "PO ");
What kind of method would I use to make this:
http://www.site.net/files/file1.zip
To
file1.zip?
String yourString = "http://www.site.net/files/file1.zip";
int index = yourString.lastIndexOf('/');
String targetString = yourString.substring(index + 1);
System.out.println(targetString);// file1.zip
String str = "http://www.site.net/files/file1.zip";
str = str.substring(str.lastIndexOf("/")+1);
You could use regex to extract the last part:
#Test
public void extractFileNameFromUrl() {
final Matcher matcher = Pattern.compile("[\\w+.]*$").matcher("http://www.site.net/files/file1.zip");
Assert.assertEquals("file1.zip", matcher.find() ? matcher.group(0) : null);
}
It'll return only "file1.zip". Included here as a test as I used it to validate the code.
Use split:
String[] arr = "http://www.site.net/files/file1.zip".split("/");
Then:
String lastPart = arr[arr.length-1];
Update: Another simpler way to get this:
File file = new File("http://www.site.net/files/file1.zip");
System.out.printf("Path: [%s]%n", file.getName()); // file1.zip