i would like to remove a character from java string using hex code:
i am trying following code but seems to not be correct as the character isn't replaced: ÿ
String str ="test ÿ";
str.replaceAll("\\x{9F}","")
is there any thing wrong with the syntax of the hex code? Thanks.
Could you please try this:
public class AsciiHexCode {
public static void main(String[] args) {
String str = "test ÿ";
String result = str.replaceAll("[^\\x00-\\x7F]", "");
System.out.println("result : "+ result);
}
}
To mach ÿ you need \u00ff instead, as Jon mentioned.
String replaced = str.replace("\u00ff", "");
in your case.
Related
I am trying to add prefix and suffix to a particular occurrence of the word in the string in java. Can anyone help me and tell me where am i going wrong? Below is my code.
public static void main(String[] args) {
String str = "Hello world. welcome world java.";
String arr[] = str.split("[. ]");
if(str.contains("world")) {
System.out.println("PREFIX_"+str+"_SUFFIX");
}
}
output expected :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java
output getting:
PREFIX_Hello world. welcome world java_SUFFIX
String replaced = str.replaceAll("world", "PREFIX_world_SUFFIX");
System.out.println(replaced);
Your code is wrong since you're not changing the str variable while calling the split() function.
Also, from what I can gather, you also want to add prefix and suffix to those words containing "world".
Like if your string is something like this: Hello worldJava! welcome to java world, you'd want to display something like this: Hello PREFIX_worldJava_SUFFIX! welcome to java PREFIX_world_SUFFIX. (Note, the previous answers wouldn't be able to do this kind of substitution).
String str = "Hello world. welcome world java.";
String[] wordArr = str.split("[. ]");
Set<String> words = new HashSet<>(Arrays.asList(wordArr));
for (String w: words) {
if(w.toLowerCase().contains("world")){
str = str.replace(w, "PREFIX_"+ w +"_SUFFIX");
}
}
System.out.println(str);
Note here that I am using java Set to parse unique words from the input string and then replacing them in the original string with the added prefix/suffix.
Just do this simply :
public class Example {
public static void main(String[] args){
String str = "Hello world. welcome world java.";
System.out.println(str.replace("world", "PREFIX_world_SUFFIX"));
}
}
Output :
Hello PREFIX_world_SUFFIX. welcome PREFIX_world_SUFFIX java.
You are doing it wrong on the print side. You can do this.
String stringToCheck = "world";
if(str.contains(stringToCheck)) {
str = str.replaceAll(stringToCheck , "PREFIX_"+stringToCheck+"_SUFFIX");
System.out.println(str);
}
I am using the wikimedia api to get content from wikipedia pages. The api returns a lot of "\n" as plain text. I want to remove them from a string
s = s.replaceAll("\\n", "");
s = s.replaceAll("\n", "");
Neither of these work, any ideas?
When your String contains a plaintext \n it is actually a \\n otherwise it would be displayed as a linebreak, which is why I found s = s.replaceAll("\\\\n","") to be working for me. An example snippet:
class Main{
public static void main(String[] args){
String s = "Hello\\nHello";
System.out.println(s);
s = s.replaceAll("\\\\n","");
System.out.println(s);
}
}
Remember that replaceAll takes a Regex: Since you want to replace 2 /s you have to escape both of them, therefore////
Hi Please to use below code format:
s= s.replace("\n", "").replace("\r", "");
Thanks
You can use the code below:
s = s.replace("\n", "");
but, the newline character can be different among the environments.
So, you can use this
s = s.replace(System.getProperty("line.separator"), "");
I am receiving a file path with "xyz" appended to it. name would look like D:/sdcard/filename.docxyz
i am using the below code to remove xyz but it is not working. what is missing here ?
String fileExtension = path.substring(path.lastIndexOf(".")+1);
String newExtension= fileExtension;
newExtension.replace("xyz", "");
path.replace(fileExtension, newExtension);
return path;
What is missing is that you need to save the result of your operations. Strings are immutable in Java, and the results of all String manipulations are therefore returned in the form of a new String:
newExtension = newExtension.replace("xyz", "");
path = path.replace(fileExtension, newExtension);
String in java are immutable, and changes upon it never occurs in place, but every time a new string is returned,
newExtension = newExtension.replace("xyz", "");
You could also use replaceAll() with a regex.
public static void main(String[] args) {
String s = "D:/sdcard/filename.docxyz";
System.out.println(s.replaceAll("xyz$", "")); // $ checks only the end
}
O/P :
input : s = "D:/sdcard/filename.docxyz";
D:/sdcard/filename.doc
input : String s = "D:/sdcard/filenamexyz.docxyz";
output : D:/sdcard/filenamexyz.doc
newExtension.replace("xyz", "");
Will only return string which has "xyz" removed but newExtension will remain as it is. Simple fix for your problem is use as below
String newExtension= fileExtension.replace("xyz", "");
How do I convert Æ and á into a regular English char with Java ? What I have is something like this : Local TV from Paraná. How to convert it to [Parana] ?
Look at icu4j or the JDK 1.6 Normalizer:
public String removeAccents(String text) {
return Normalizer.normalize(text, Normalizer.Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
As far as I know, there's no way to do this automatically -- you'd have to substitute manually using String.replaceAll.
String str = "Paraná";
str = str.replaceAll("á", "a");
str = str.replaceAll("Æ", "a");
Try something similar to the following code snippet:
import org.apache.commons.lang3.StringUtils;
public class Test {
public static void main(String[] args) {
String original = new String("Ramesh Öhrman");
try {
System.out.println(StringUtils.stripAccents(original));
} catch (Exception e) {
}
}
}
Output: Ramesh Ohrman
Suppose I have following string:
String asd = "this is test ass this is test"
and I want to split the string using "ass" character sequence.
I used:
asd.split("ass");
It doesn't work. What do I need to do?
It seems to work fine for me:
public class Test
{
public static void main(String[] args) {
String asd = "this is test ass this is test";
String[] bits = asd.split("ass");
for (String bit : bits) {
System.out.println("'" + bit + "'");
}
}
}
Result:
'this is test '
' this is test'
Is your real delimiter different perhaps? Don't forget that split uses its parameter as a regular expression...
String asd = "this is test foo this is test";
String[] parts = asd.split("foo");
Try this it will work
public class Splitter {
public static void main(final String[] args) {
final String asd = "this is test ass this is test";
final String[] parts = asd.split("ass");
for (final String part : parts) {
System.out.println(part);
}
}
}
Prints:
this is test
this is test
Under Java 6. What output were you expecting?