Replace Last Occurrence of a character in a string Using Java [duplicate] - java

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

Related

how to replace a char within a string with "nothing" [duplicate]

This question already has answers here:
Java replace method, replacing with empty character [duplicate]
(2 answers)
Closed 2 years ago.
String mine = sc.next();
String corrected = mine.replace('.', '????');
System.out.println(corrected);
that's my code. let's assume that my input on String corrected is "<..><.<..>>" , and I want to replace every "." with a null space, so I get an output like "<><<>>". is there any way to do it?
If you want to replace . with an empty ("") string, you can just do:
mine.replace(".", "");
Alternatively, you can also check .replaceAll()
Try this to replace all occurrences of . with empty:
mine.replaceAll("\\.", "")
If you don't want any method, you can do it like this.
String str = "<<.>>.<>.<<.";
String [] parts = str.split("\\.");
for(String s:parts){
System.out.print(s);
}
Because I tried the method replaceAll(".", "") ;
But that method does not allow empty or null spaces in a string.
I don't know if it's the best way, but that's what I can think of.

Split a word by a char in Java [duplicate]

This question already has answers here:
How to split a string, but also keep the delimiters?
(24 answers)
How do I split a string in Java?
(39 answers)
Closed 3 years ago.
Consider the following example. I would like to divide the String into two parts by the char 'T'
// input
String toDivideStr = "RaT15544";
// output
first= "RaT";
second = "15544";
I've tried this:
String[] first = toDivideStr.split("T",0);
Output:
first = "Ra"
second = "15544"
How do I achieve this?
What you need to to, is locate the last "T", then split:
StringToD.substring(StringToD.lastIndexOf("T") + 1)
You could use a positive lookahead to assert a digit and a positive lookbehind to assert RaT.
(?<=RaT)(?=\\d)
For example:
String str = "RaT15544";
for (String element : str.split("(?<=RaT)(?=\\d)"))
System.out.println(element);
Regex demo | Java demo
You can use positive look-ahead with split limit parameter for this. (?=\\d)
With only T in the split method parameter, what happens is the regex engine consumes this T. Hence the two string split that occurs doesn't have T. To avoid consuming the characters, we can use non-consumeing look-ahead.
(?=\\d) - This will match the first number that is encountered but it will not consume this number
public static void main(String[] args) {
String s = "RaT15544";
String[] ss = s.split("(?=\\d)", 2);
System.out.println(ss[0] + " " + ss[1]);
}
The below regex can be used to split the alphabets and numbers separately.
String StringToD = "RaT15544";
String[] parts = StringToD.split("(?<=\\d)(?=\\D)|(?<=\\D)(?=\\d)");
System.out.println(parts[0]);
System.out.println(parts[1]);

Remove all occurrences of char "." from string in Java [duplicate]

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("\\.", "");

Using regex to trim a specific word in String [duplicate]

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.

How to replace first occurance a character in string android [duplicate]

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("\","");

Categories