Can't figure out how to replace a string with a character in Java. I wanted a function like this:
replace(string, char)
but there's not a function like this in Java.
String str = str.replace("word", '\u0082');
String str = str.replace("word", (char)130);
How do I go about this?
Use a string as the replacement that happens to be only a single character in length:
String original = "words";
String replaced = original.replace("word", "\u0130");
The replaced instance will be equivalent to "İs".
Note also that, from your question, '\u0130' and (char)130 are not the same characters. The \u syntax uses hexadecimal and your cast is using decimal notation.
Very simply:
String orginal = "asdf";
char replacement = 'z';
orginal = original.replace(original, replacement+"");
You asked for a "function" in Java, you can allways create a method like this
public static String replace (String text, char old, char n){
return text.replace(old, n);
}
Then you can call this method as you want
String a = replace("ae", 'e', '3');
In this case the method will return a String with a3 as value, but you can replace not only a char by another, you can replace a String with multiple characters in the same way
public static String replace (String text, String old, String n){
return text.replace(old, n);
}
Then you call this method like this
String a = replace("aes", "es", "rmy");
The result will be a String with army as value
the simplest way:
"value_".replace("_",String.valueOf((char)58 )); //return "value:"
EDIT:
(char)58 //return: ':'
(int)':' //return: 58
Since we want to work with codes and no character we have to pass code to character
another problem to solve is that method "replace" does not take "(String x,char y)"
So we pass our character to String this way:
String.valueOf((char)58) //this is a String like this ":"
Finally we have String from int code, to be replaced.
"String is a sequence of characters"
String s="hello";
char c='Y';
System.out.println(s.replace(s, Character.toString(c)));
Output:
Y
Related
I try to replace a character and all it's following characters within a string with another character.
This is my code so far.
String name = "Peter Pan";
name = name.replace("er", "abc");
Log.d("Name", name)
the result should be: "Petabc"
I would highly appreciate any help on this matter!
A way to achive your goal:
search the string for the first appearance of the sequnce you want to replace
use that index and cut the string using String#substring
add your replace sequence to the end of the substring you just created
fin.
Good luck.
EDIT
In code it might look like this (not tested)
public static String customReplace(String input, String replace)
{
int index = input.indexOf(replace);
if(index >= 0)
{
return input.substring(index) + replace; //cutting string down to the required part and adding the replace
}
else
return null; //String 'input' doesn't contain String 'replace'
}
You could use a Regular Expression here with String's built-in replaceAll method to very easily do what you want:
original.replaceFirst(toReplace + ".*", replaceWith);
For example:
String original = "testing 123";
String toReplace = "ing";
String replaceWith = "er";
String replaced = original.replaceFirst(toReplace + ".*", replaceWith);
After the above, replaced will be set to "tester".
Input string is "ABC\1067546161"
I want to remove "\" backslash and get digits only from the string but we are getting string with ascii value.Result String is ABCF7546161 after print.
Please suggest some solution.
Input String is ABC\1067546161
Expected result is 1067546161
May be something like this
"ABC\1067546161".replaceAll("[a-zA-Z\\]", "")
I think this could work, but the code is ugly as hell..
String word = "ABC\1067546161";
char badChar = word.charAt(3);
String[] arr = word.split(Character.toString(badChar));
System.out.println(Integer.toOctalString(badChar) + arr[1]);
You only mentioned one string in the question, but on several cases, this would most likely not work.
As #TheLostMind pointed out in a comment, you can't replace the backslash directly because the String is created with that value.
The only way to do that is manipulate the input itself and convert it into a byte array instead of a String. Then you can call the String constructor that takes a byte[] as argument and it won't be converted.
Once you have that, you can use a regex to remove the part you don't want as others suggested. Here's the code I've used to test this:
public static void main(String[] args) {
// Input manipulation.
byte[] input = {'A','B','C','\\','1','0','6','7','5','4','6','1','6','1'};
String string = new String(input);
System.out.println(string);
// Splitting.
String[] result = string.split("\\\\");
System.out.println(result[1]);
}
Please note that this question is not a duplicate.
I have a String like this:
// My String
String myString = "U+1F600";
// A method to convert the String to a real character
String unicodeCharacter = convertStringToUnicode(myString);
// Then this should print: 😀
System.out.println(unicodeCharacter);
How can I convert this String to the unicode character 😀? I then want to show this in a TextView.
What you are trying to do is to print the unicode when you know the code but as String...
the normal way to do this is using the method
Character.toChars(int)
like:
System.out.print(Character.toChars(0x1f600));
now in you case, you have
String myString = "U+1F600";
so you can truncate the string removing the 1st 2 chars, and then parsing the rest as an integer with the method Integer.parseInt(valAsString, radix)
Example:
String myString = "U+1F600";
System.out.print(Character.toChars(Integer.parseInt(myString.substring(2), 16)));
Try
yourTextVIew.setText(new String(Character.toChars(0x1F60B)));
I want to search a particular char from string without any loop and then i want to insert new char after that.
String a = "my%name%is%";
I want to find "%" and then i want to insert "?" char.
Output result:
a = "my%?name%?is%?";
Use replace(char a,char b).
void replaceString(){
String a = "my%name%is%";
System.out.printlnt(a.replace("%","%?"));
}
You can also use replaceAll(String regex, String replacement)
String a = "my%name%is%";
System.out.println(a.replaceAll("([.*^%])", "%?"));//prints my%?name%?is%?
How do i use replace(char, char) to replace all instances of character "b" with nothing.
For example:
Hambbburger to Hamurger
EDIT: Constraint is only JDK 1.4.2, meaning no overloaded version of replace!
There's also a replaceAll function that uses strings, note however that it evals them as regexes, but for replacing a single char will do just fine.
Here's an example:
String meal = "Hambbburger";
String replaced = meal.replaceAll("b","");
Note that the replaced variable is necessary since replaceAll doesn't change the string in place but creates a new one with the replacement (String is immutable in java).
If the character you want to replace has a different meaning in a regex (e.g. the . char will match any char, not a dot) you'll need to quote the first parameter like this:
String meal = "Ham.bur.ger";
String replaced = meal.replaceAll(Pattern.quote("."),"");
Strings are immutable, so make sure you assign the result to a string.
String str = "Hambbburger";
str = str.replace("b", "");
You don't need replaceAll if you use Java 6. See here: replace
Try this code....
public class main {
public static void main(String args[]){
String g="Hambbburger.i want to eat Hambbburger. ";
System.out.print(g);
g=g.replaceAll("b", "");
System.out.print("---------After Replacement-----\n");
System.out.print(g);
}
}
output
Hambbburger.i want to eat Hambbburger. ---------After Replacement-----
Hamurger.i want to eat Hamurger.
String text = "Hambbburger";
text = text.replace('b', '\0');
The '\0' represents NUL in ASCII code.
replaceAll in String doesnot work properly .It's Always recomend to use replace()
Ex:-
String s="abcdefabcdef";
s=s.replace("a","");
String str="aabbcc";
int n=str.length();
char ch[]=str.toCharArray();
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(ch[i]==ch[j])
{
ch[j]='*';
}
}
}
String temp=new String(ch);
for(int i=0;i<temp.length();i++)
{
if(temp.charAt(i)!='*')
System.out.print(temp.charAt(i));
}