How to remove character after specific character? - java

I have for example this string:
hello.name-2.txt
And I need to remove only character after "-".
So my output should look:
hello.name-.txt
How can I do it?

You can do
s = s.replaceAll("-.", "-");
if you want to replace a number even "hello.name-1234.txt" you can use
s = s.replaceAll("-\\d+", "-");
If you only want to do this once, you can use replaceFirst instead.

int dashIndex = yourString.indexOf("-");
String result = yourString.substring(0, dashIndex + 1)
+ yourString.substring(dashIndex + 2);

Related

JAVA - How to get text after a particular character?

I have a String Chocolate:30:2 in a variable and I want to extract the number after the second colon i.e. 2. So, How can I extract that number?
For example:
String s = "Chocolate:30:2";
String number = s.split(":")[2];
If the second colon is actually the last colon, you can use:
String after = str.substring(1 + str.lastIndexOf(':'));
You can use String lastIndexOf method.
String result = str.substring(str.lastIndexOf(':') + 1);

Wrong Answer with Java

I'm a student that is learning Java, and I have this code:
lletres = lletres.replace(lletres.charAt(2), codi.charAt(codi.indexOf(lletres.charAt(2)) + 1));
lletres is a string, and it's like this
lletres = "BBB"
The result is "CCC" and I only want to change the last B, so the result can be like this: "BBC".
Reading the documentation for String.replace should explain what happened here (I marked the relevant part in bold):
Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.
One way to solve it is to break the string up to the parts you want and then put it back together again. E.g.:
lletres = lletres.substring(0, 2) + (char)(lletres.charAt(2) + 1);
As others pointed replace() will replace all the occurrences which matched.
So, instead you can make use of replaceFirst() which will accept the regx
lletres = lletres.replaceFirst( lletres.charAt( 2 ) + "$", (char) ( lletres.charAt( 2 ) + 1 ) + "" )
You could use StringBuilder for your purpose:
String lletres = "BBB";
String codi = "CCC";
StringBuilder sb = new StringBuilder(lletres);
sb.setCharAt(2, codi.charAt(codi.indexOf(lletres.charAt(2)) + 1));
lletres = sb.toString();
If you need to change only the last occurrence in the string, you need to split the string into parts first. I hope following snippet will be helpful to you.
String lletres = "BBB";
int lastIndex = lletres.lastIndexOf('B');
lletres = lletres.substring(0, lastIndex) + 'C' + lletres.substring(lastIndex+1);
This code will find index of last letter B and stores it in lastIndex. Then it splits the string and replaces that B letter with C letter.
Please keep in mind that this snippet doesn't check whether or not the letter B is present in the string.
With slight modification you can get it to replace whole parts of the string, not only letters. :)
Try this one.
class Rplce
{
public static void main(String[] args)
{
String codi = "CCC";
String lletres = "BBB";
int char_no_to_be_replaced = 2;
lletres = lletres.substring(0,char_no_to_be_replaced ) + codi.charAt(codi.indexOf(lletres.charAt(char_no_to_be_replaced )) + 1) + lletres.substring(char_no_to_be_replaced + 1);
System.out.println(lletres);
}
}
use this to replace the last character
lletres = lletres.replaceAll(".{1}$", String.valueOf((char) (lletres.charAt(2) + 1)));
suppose you have dynamic value at last index and you want to replace that value will increasing one then use this code
String lletres = "BBB";
int atIndex = lletres.lastIndexOf('B');
char ReplacementChar = (char)(lletres.charAt(lletres.lastIndexOf('B'))+1);
lletres= lletres.substring(0, atIndex)+ReplacementChar;
System.out.println(lletres);
output
BBC

Replacing width and height in url

What's the simplest way of changing the w= number and h= number?
Example of url:
https://test.com/photos/226109/test-photo-226109.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb&fit=crop
I have to dynamically change bolded parts.
I can extract the value of w like this:
s = s.substring(s.indexOf("=") + 1, s.indexOf("&"));
But how could I change it?
I tried searching Stackoverflow, but couldn't find anything.
Thanks.
If i understood correctly you are trying to replace the values after the = sign for h and w.
You can simply do that with RegEx as follows:
"https://test.com/photos/226109/test-photo-226109.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb&fit=crop"
.replaceAll("w=\\d+", "w=NEW_VALUE").replaceAll("&h=\\d+", "&h=NEW_VALUE")
What is happening above is that we first find the patern that matches w=AnyNumberHere and we replace the entire section with w=NEW_VALUE. Likewise we replace &h=AnyNumberHere with &h=NEW_VALUE
This solution is not length depended, therefore if the URL has a variable length this will still work, and will even work if the value h=123 or w=1234 for example do not exist ;)
In your case you can use String replaceAll method. Example of use:
String string =
"https://test.com/photos/226109/test-photo-226109." +
"jpeg?w=1260&h=750&auto=compress&cs=tinysrgb&fit=crop";
String replacedString = string
.replaceAll(string.substring(string.indexOf("=") + 1,
string.indexOf("&")), "1000");
When you get your numbers from string you can convert it to StringBuffer.
Like this
String s = new String("https://test.com/photos/226109/test-photo-226109.jpeg?w=1260&h=750&auto=compress&cs=tinysrgb&fit=crop");
StringBuffer sb = new StringBuffer(s);
sb.replace(s.indexOf("w=") + 2, s.indexOf("&"), "2000");
sb.replace(s.indexOf("h=") + 2, s.indexOf("&"), "2000");
s = sb.toString();

How can I modify just the first letter of a String?

I have got a String which reads "You cannot sit on that " + entity.getEntityType().toLowerCase() + "!";. Which returns an upper case, which is why I convert it to lower case.
However, how can I get the first letter ONLY, and turn it into an upper case?
A one-liner solution would be preferred, however beggars cant be choosers.
In java, if you do any manipulation to String, it will create a new string in the memory.
Here's, how to do it:
String output = input.substring(0, 1).toUpperCase() + input.substring(1);
It's more efficient to treat the first character what it is - a character and not a String:
String output = Character.toUpperCase(input.charAt(0)) + input.substring(1);
The first thing it came to my mind was this
int x = a.codePointAt(0)-32;
a=a.replace(a.charAt(0), (char) x);
You can just use something like this:
String output = input.substring(0, 1).toUpperCase() + input.substring(1);
On the other hand is isn't bad to take a look at the guava libraries.
String output = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, entity.getEntityType()));
For this case only it will be overkill but it has a lot of other usefull utilities (Not only for String manipulation)

Getting the last part of the string

I have a string :
"id=40114662&mode=Edit&reminderId=44195234"
All i want from this string is the final number 44195234. I can't use :
String reminderIdFin = reminderId.substring(reminderId.lastIndexOf("reminderId=")+1);
as i cant have the = sign as the point it splits the string. Is there any other way ?
Try String.split(),
reminderIdFin.split("=")[3];
You can use indexOf() method to get where this part starts:
int index = reminderIdFin.indexOf("Id=") + 3;
the plus 3 will make it so that it jumps over these characters. Then you can use substring to pull out your wanted string:
String newString = reminderIdFin.substring(index);
Remove everything else and you'll be left with your target content:
String reminderIdFin = reminderId.replaceAll(".*=", "");
The regex matches everything up to the last = (the .* is "greedy").

Categories

Resources