0
String text= "2011-11-19T00:00:00.000-05:00";

I have the above String .

As the date above is not in RFC format , i want to remove the colons in the string at 05:00 So taht the actual string will look like

String text= "2011-11-19T00:00:00.000-0500";
1
  • 3
    You can use either regexp or date functions. Date functions would be a wiser choice though.
    – lc2817
    Nov 16, 2011 at 15:37

5 Answers 5

2

Since it's the last occurrence of the colon in your string, you could do something like this:

String newText = text.substring(0, text.lastIndexOf(":")) + text.substring(text.lastIndexOf(":") + 1);

This will create a new string without the last occurrence of the colon.

2

Regex is overkill for such an operation. Learning basic string manipulation is an important skill, and usually leads to more efficient code.

My example is cleaner (and even more efficient, though it's a marginal benefit) than the highest rated answer.

int remove = text.lastIndexOf(':');
text = text.substring(0,remove) + text.substring(remove+1);

lastIndexOf()

substring(int)

substring(int,int)

4
  • 1
    This is not an answer, it is just code. Without explanations it doesn't contribute enough to the Wiki. Please improve or delete it.
    – markus
    Nov 16, 2011 at 17:14
  • For such questions, the code is more than explanatory. Looking at other answers given, the accompanying text adds little to no value.
    – Kylos
    Nov 16, 2011 at 19:43
  • But perhaps I should explain why my example is superior to other answers. First, regex is overkill for such an operation. learning basic string manipulation is an important skill, and usually leads to more efficient code. Additionally, for someone who doesn't know about lastIndexOf, saying "use lastIndexOf" is probably not a sufficient answer to the question. And, finally, my example is cleaner (and even more efficient, though it's a marginal benefit) than the highest rated answer.
    – Kylos
    Nov 16, 2011 at 19:51
  • Why didn't you say that from the beginning?!?
    – markus
    Nov 16, 2011 at 20:49
1

You can also use regular expression with a lookahead and a lookbehind to replace the colon with an empty string. This way you're sure the right colon is removed.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

    ...
    String text= "2011-11-19T00:00:00.000-05:00";
    Pattern p = Pattern.compile("(?<=\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{3}-\\d{2}):(?=\\d{2})");
    Matcher m = p.matcher(text);
    String newtext = m.replaceAll("");
    ...

This may be a good option if you're changing the format of dates in a larger block of text.

0

One way is to use String API to get the last index of the character ':'

 int    lastIndexOf(int ch) 

Once you have that you can remove the element at that index.

There might be a easier way to remove it via regex for example. it is just one way of doing it.

0

Well in this particular case you can use String.lastIndexOf(int) and get the position of the last ":" in your string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.