This question already has answers here:
Replace first occurrence of character in Android/Java?
(3 answers)
Closed 6 years ago.
In android how do I replace the first occurrence of a character in a string
with another? F
or example I want to replace first occurrence of character v
in vivu with / so that output has to be /ivu
I found we can replace all occurrence of string with a character by replace() as follows:-
EditText n1=(EditText)findViewById(R.id.editText);
TextView t1=(TextView)findViewById(R.id.textView);
String s1= n1.getText().toString();
s1=s1.replace(s1.charAt(i),'\')
But I don't want to change all occurences, just the first one
String s1= n1.getText().toString();
String output = s1.replaceFirst("\","");
Related
This question already has answers here:
Split string into array of character strings
(12 answers)
Closed 3 years ago.
For instance, I have the String variable abbaabbbba. I would like to use String split so each character would be separated, so it would be {a,b,b,a,a,b,b,b,b,a}. All the instructions I can find for string split say i need to have something, like a a space:
String mySplit = str.split("/");
Is there anyway to do this by character?
You can use the toCharArray method like this:
String myString = "abbaabbbba";
char[] myCharacters = myString.toCharArray();
This question already has answers here:
How to replace a String in java which contains dot?
(3 answers)
Closed 4 years ago.
I would like to remove the period/decimal character from a String using Java.
String originalString = "1.2345";
originalString = originalString.replaceAll(".", "");
Printing originalString returns empty.
How can I remove . from originalString?
The first argument of replaceAll is a regex pattern. Since . means "any character", all the characters are removed. In order to refer to the actual . character, you need to escape it:
originalString = originalString.replaceAll("\\.", "");
String originalString = "1.2345";
originalString = originalString.replaceAll("\\.", "");
This question already has answers here:
Replace the last part of a string
(11 answers)
Closed 5 years ago.
I am having a string like this
String s1 = "2999.1049.00_GRB.1";
String s2 = "my File.txt.txt";
I want to replace the last ".1" with "_1" and ".txt" with "_txt"
The result of the String should be
s1 = "2999.1049.00_GRB_1" and s2 = "my File.txt_txt"
How can I do this. I am aware of replacing the first occurrence of the string. but don't know how to replace the last occurrence of a string.
Simply use .replace with lastIndexOf method of string
System.out.println(s.replace(s.substring(s.lastIndexOf(".1"), s.length()), "_1"));
You can use regex :
s = s.replaceAll("(.*)\\.(\\d+)$","$1_$2");
// (everything)point(digits) -> (everything)underscore(digits)
It will capture all element before the . in a group (group1), the digit(s) after in another group (group2), and replace by : group1_group2
the first group can be whatever you want
the second group is just digits, even more than 1
Regex demo
This question already has answers here:
Delete everything after part of a string
(10 answers)
Closed 6 years ago.
I am not an expert of regex. Suppose I have this string:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
If I want to remove ,normal and replace it by dot so the string becomes like this:
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00."
How can I do that in regex?
Thank you very much.
You can use a regular expression like ,\\w+\\.$ which matches any String ending in , a word and then a . and String.replaceAll(String, String) like
String str = "0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00,normal."
.replaceAll(",\\w+\\.$", "\\.");
System.out.println(str);
Output is (as requested)
0,tcp,1.00,0.00,0.11,0.00,0.00,0.00,0.00,1.00.
This question already has answers here:
Java regular expression to match _all_ whitespace characters
(7 answers)
Closed 8 years ago.
I'm trying to replace all all spaces in string with one space. I'm trying this:
String src = "2. Test Sentence with spaces";
String out = src.replaceAll("\\s+", " ");
System.out.println(out);
And this is what I'm getting:
2. Test Sentence with spaces
Spaces after dot were not replaced... Why?
You can try with the Unicode category: separator, space, combined with whitespace:
String input = "\u0020\u00A0\u1680\u2000\u2001\t"; //etc. 17 characters
System.out.println(input.replaceAll("[\\p{Zs}\\s]+", " "));
Output
[1 space]
See here for the list of characters in category Zs.