4

I have a string where the third last character is sometimes a , If this is the case I want to replace it with a . The string could also have other ,'s throughout. Is there an elegant solution to this?

EDIT: Thanks everyone for your answers. Just to clarify, yes by third last I mean a string of the form xxxxxx,xx (it's a european currency thing)

4
  • Is the comma that is to be replaced always appearing on the third from last position (if it appears at all)? Commented Aug 8, 2011 at 13:46
  • 1
    Can you post an example string? Commented Aug 8, 2011 at 13:47
  • @Fredrik - yes, it will always be appearing in the third last position. Commented Aug 8, 2011 at 13:59
  • If you have the decimal value (since you talk about currency in your edit) wouldn't it be cleaner to do a string.Format("{0:0.00}", 275.95, System.Globalization.CultureInfo.CreateSpecificCulture("nl-BE")); with the correct culture? This example would return 275,95 Commented Aug 18, 2011 at 14:53

4 Answers 4

7

How about:

if (text[text.Length - 3] == ',')
{
    StringBuilder builder = new StringBuilder(text);
    builder[text.Length - 3] = '.';
    text = builder.ToString();
}

EDIT: I hope the above is just about the most efficient approach. You could try using a char array instead:

if (text[text.Length - 3] == ',')
{
    char[] chars = text.ToCharArray();
    chars[text.Length - 3] = '.';
    text = new string(chars);
}

Using Substring will work as well, but I don't think it's any more readable:

if (text[text.Length - 3] == ',')
{
    text = text.Substring(0, text.Length - 3) + "."
           + text.Substring(text.Length - 2);
}

EDIT: I've been assuming that in this situation you already know that text will be at least three characters length. If that's not the case, you'd obviously want a test for that as well.

Sign up to request clarification or add additional context in comments.

8 Comments

@Code: Heavy compared with what? I can't think of a more efficient approach - can you?
+1 - if the op means third from the last this is so far the only solution that would seem to work.
@Jon: Simple string concatenation?
@Code Monkey: Concatentation of substrings, each of which needs to be created as a separate object? How would that be more efficient? (I've added an example of it in my answer, but I would expect that to be less efficient.)
@Code Monkey - I don't see anything wrong with it. For one its a lot more readable.
|
4
string text = "Hello, World,__";

if (text.Length >= 3 && text[text.Length - 3] == ',')
{
    text = text.Substring(0, text.Length - 3) + "." + text.Substring(text.Length - 2);
}

// text == "Hello, World.__"

2 Comments

+1 but he says "third last character" so I don't know if this means third to last character.
+1 @dtb - great answer as well. Sorry I can't give out two correct responses.
2

A more proper method would probably be to use the cultures

string input = "12345,67";
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("nl-NL");
decimal value = System.Convert.ToDecimal(input);
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
string converted = string.Format("{0:C}", value);

1 Comment

I know the question is 'finished' now but +1. I did think this after the OP edited the question to include what he was actually after.
1

Try this

System.Text.RegularExpressions.Regex.Replace([the_string], "(,)(.{2})$", ".$2")

It should do it if by 'third last character' you literally mean the third-last character in the whole string.

That said - you might need to tweak if there are new lines - e.g. add the RegexOptions.Singleline enum as an extra parameter.

For better performance - probably - you could pre-declare the regex inside a class body:

static readonly Regex _rxReplace = new Regex("(,)(.{2})$", RegexOptions.Compiled);

Then when you want to use it it's just:

var fixed = _rxReplace.Replace([the_string], ".$2");

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.