Android - how to replace part of a string by another string?
It is working, but it wont modify the caller object, but returning a new String.
So you just need to assign it to a new String variable, or to itself:
string = string.replace("to", "xyz");
or
String newString = string.replace("to", "xyz");
API Docs
public String replace (CharSequence target, CharSequence replacement)
Since: API Level 1
Copies this string replacing
occurrences of the specified target
sequence with another sequence. The
string is processed from the beginning
to the end.
Parameters
target
the sequence to replace.replacement
the replacement
sequence.
Returns the resulting string.
Throws NullPointerException
if target or replacement is null.
how to replace some character's of String
You can use string replace method. see below
String number = +9231235410;
String newNumber = number.replace("+92","0");
EDIT:
this one is base on your code
private String modifyNumber(String num) {
if (num.startsWith("+92")) {
num = num.replaceFirst("\\+(92)", "0");}
return num;}
Note:
- In java, you need to have double backslash since the
\
is a unique
java character. - Java strings are immutable method. you need to assign it to a variable to have the result.
num = num.replaceFirst("\\+(\\d{2})", "0")
Replace part of String resource with format string
Let's suppose you have a resource
R.string.template_string
as
On your return trip from studying Saturn's rings, you hear a distress signal that seems to be coming from the surface of Mars. It's strange because there hasn't been a colony there in years. Even stranger, it's calling you by name: \"Help me, %s, you're my only hope.\"
In code you can format this as
String username = "Bob"
String result = String.format(getResources().getString(R.string.template_string), username);
Warn:
You have incorrect template. Replace %1$s
with %s 1$
Find and Replace string in Android
You can remove strings this way:
string.replace("=\"ppshein\"", "");
Replace string \ with / android
Escape back slash like:
myString.replace("\\","/");
Replace a substring at a particular position in a string in Java
I would not use any replace()
method if you know the indexes of the substring you want to replace. The problem is that this statement:
str = str.replace(str.substring(someIndex, someOtherIndex), replacement);
first computes the substring, and then replaces all occurrences of that substring in the original string. replace
doesn't know or care about the original indexes.
It's better to just break up the string using substring()
:
int i1 = str.indexOf("(");
int i2 = str.indexOf(")");
str = str.substring(0, i1+1) + "tom" + str.substring(i2);
android replace string by another string in file on sdcard
I will give my code, always worked for me :)
Hope thi can help you :DD
public void saveString(String text){
if(this.isExternalStorageAvailable()){
if(!this.isExternalStorageReadOnly()){
try {
FileOutputStream fos = new FileOutputStream(
new File(this.getExternalFilesDir("text"), "text.dat"));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeBytes(text);
oos.close();
fos.close();
} catch (FileNotFoundException e) {
//Toast.makeText(main, "Eror opening file", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
//Toast.makeText(main, "Eror saving String", Toast.LENGTH_SHORT).show();
}
}
}
}
private static boolean isExternalStorageAvailable(){
String estadoSD = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(estadoSD))
return true;
return false;
}
private static boolean isExternalStorageReadOnly(){
String estadoSD = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED_READ_ONLY.equals(estadoSD))
return true;
return false;
}
public String getString(){
FileInputStream fis = null;
ObjectInputStream ois = null;
if(this.isExternalStorageAvailable()) {
try {
fis = new FileInputStream(
new File(this.getExternalFilesDir("text"), "text.dat"));
ois = new ObjectInputStream(fis);
String text = (String)ois.readObject();
return familia;
} catch (FileNotFoundException e) {
//Toast.makeText(main, "The file text doesnt exist", Toast.LENGTH_SHORT).show();
} catch (StreamCorruptedException e) {
//Toast.makeText(main, "Eror opening file", Toast.LENGTH_SHORT).show();
} catch(EOFException e){
try {
if(ois != null)
ois.close();
if(fis != null)
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
} catch (IOException e) {
//Toast.makeText(main, "eror reading file", Toast.LENGTH_SHORT).show();
} catch (ClassNotFoundException e) {
//Toast.makeText(main, "String class doesnt exist", Toast.LENGTH_SHORT).show();
}
}
return null;
}
Android. Replace * character from String
Why not just use String#replace()
method, that does not take a regex
as parameter: -
text = text.replace("*","");
In contrary, String#replaceAll()
takes a regex as first parameter, and since *
is a meta-character in regex, so you need to escape it, or use it in a character class. So, your way of doing it would be: -
text = text.replaceAll("[*]",""); // OR
text = text.replaceAll("\\*","");
But, you really can use simple replace here.
Related Topics
How to Include Jar Dependency into an Aar Library
Problem Unmarshalling Parcelables
Fill Fields in Webview Automatically
Create an Android Jar Library for Distribution
What Exactly Is a Context in Java
How to Navigate from One Screen to Another Screen
How to Get Difference Between Two Dates in Android, Tried Every Thing and Post
Java.Lang.Nullpointerexception: Attempt to Invoke Virtual Method on a Null Object Reference
Fatal Error: Invalid Layout of Java.Lang.String at Value
Android: Prompt User to Save Changes When Back Button Is Pressed
Aes Gets Different Results in iOS (Obj-C) and Android (Java)
Cordova Could Not Reserve Enough Space for 2097152Kb Object Heap
How to Skip Initial Data and Trigger Only New Updates in Firestore Firebase