I want to replace the last String which is a , with ).
Suppose the string is:
Insert into dual (name,date,
It is to be converted to:
Insert into dual (name,date)
I want to replace the last String which is a , with ).
Suppose the string is:
Insert into dual (name,date,
It is to be converted to:
Insert into dual (name,date)
The following code should replace the last occurrence of a ',' with a ')'.
StringBuilder b = new StringBuilder(yourString);
b.replace(yourString.lastIndexOf(","), yourString.lastIndexOf(",") + 1, ")" );
yourString = b.toString();
Note This will throw Exceptions if the String doesn't contain a ','.
You can use a regular expression:
String aResult = "Insert into dual (name,date,".replaceAll(",$", ")");
replaceAll(...) will match the string with the given regular expression (parameter 1) (in this case we match the last character if it is a comma). Then replace it with a replacement (parameter 2) (in this case is ')').
Plus! If you want to ensure that trailing spaces and tabs are taken care of, you can just change the regular expression to ',\[ \t\]*$'. Note: '\[' and '\]' is without backslash (I don't know how to properly escape it).
This is a custom method to replace only the last substring of a given string. It would be useful for you:
private String replaceLast(String string, String from, String to) {
int lastIndex = string.lastIndexOf(from);
if (lastIndex < 0)
return string;
String tail = string.substring(lastIndex).replaceFirst(from, to);
return string.substring(0, lastIndex) + tail;
}
str = str.substring(0, str.lastIndexOf(",")) + ")";
substring if lastIndexOf returns -1.Use Apache Commons' StringUtils function removeEnd():
StringUtils.removeEnd("www.domain.com", ".com") = "www.domain"
The more readable way ... Which you can use to learn about String and its functions
String myString = "Insert into dual (name,date,";
String newString = "";
int length = myString.length();
String lastChar = myString.substring(length-1);
if (lastChar.contains(",")) {
newString = myString.substring(0,length-1) + ")";
}
System.out.println(newString);
On a similar search I found this answer:
Replace Last Occurrence of a character in a string
I think it is the best, because it uses the Java methods as intended rather than trying to reinvent the wheel.
It essentially reads the string backwards and uses the String object's replaceFirst method, this is exactly what I was looking for.
Here is the documentation on replaceFirst String method and the StringBuffer's reverse function:
Here is how I implemented it to simply remove some HTML 'pre' tags from a code snippet that I wanted to interpret. Remember to reverse your search string as well, and then reverse everything back to normal afterwards.
private String stripHtmlPreTagsFromCodeSnippet(String snippet) {
String halfCleanSnippet = snippet.replaceFirst("<pre>", "");
String reverseSnippet = new StringBuffer(halfCleanSnippet).reverse().toString();
String reverseSearch = new StringBuffer("</pre>").reverse().toString();
String reverseCleanSnippet = reverseSnippet.replaceFirst(reverseSearch, "");
return new StringBuffer(reverseCleanSnippet).reverse().toString();
}
Check the length of the string, check the last character (if you have the length it is easy), and replace it - when necessary.
This solution is not language-specific - just use common sense.
Here's how you can replace the last occurrence of a string in another string:
String yourString = "foo, foo, foo, bar!";
yourString = yourString.replaceAll("foo, (?!foo, )", "bar!, ");
The (?!foo, ) ensures that there is no foo, afterwards.
See helpful article and documentation on regex for more info.