user2956514
user2956514

Reputation: 39

For looping and string

So I have a string, and in it, I want to replace last 3 chars with a dot.
I did something but my result is not what I wanted it to be.
Here is my code:

string word = "To je";
        for (int k = word.Length; k > (word.Length) - 3; k--)
        {
            string newWord = word.Replace(word[k - 1], '.');
            Console.WriteLine(newWord);
        }


The output I get is:
To j.
To .e
To.je

But the output I want is:
To...

How do I get there?
So the program is doing something similar to what I actually want it to do, but not quite.
I've really been struggling with this and any help would be appreciated.

Upvotes: 0

Views: 1104

Answers (7)

Jon Skeet
Jon Skeet

Reputation: 1500504

Look at this:

string newWord = word.Replace(word[k - 1], '.');

You're always replacing a single character from word... but word itself doesn't change, so on the next iteration the replacement has "gone".

You could use:

word = word.Replace(word[k - 1], '.');

(And then move the output to the end, just writing out word.)

However, note that this will replace all occurrences of any of the last three characters with a ..

The simplest way to fix all of this is to use Substring of course, but if you really want to loop, you could use a StringBuilder:

StringBuilder builder = new StringBuilder(word);
for (int k = word.Length; k > (word.Length) - 3; k--)
{
    builder[k - 1] = '.';
}
word = builder.ToString();

Upvotes: 2

mhawke
mhawke

Reputation: 87074

I'm a bit late to the party but all of the other solutions posted so far do not elegantly handle the case that the string is shorter than the requested number of replacements, or an arbitrary number of replacements. Here is a general function for replacing the last n characters at the end of a string with a user specified value:

static String replace_last_n(String s, int nchars, char replacement='.')
{
    nchars = Math.Min(s.Length, nchars > 0 ? nchars : 0);
    return s.Substring(0, s.Length - nchars) + new String(replacement, nchars);
}

Console.WriteLine(replace_last_n("wow wow wow", 3));
Console.WriteLine(replace_last_n("wow", 3, 'x'));
Console.WriteLine(replace_last_n("", 3));
Console.WriteLine(replace_last_n("w", 3));
Console.WriteLine(replace_last_n("wow", 0));
Console.WriteLine(replace_last_n("wow", -2));
Console.WriteLine(replace_last_n("wow", 33, '-'));

Output:

wow wow ...
xxx

.
wow
wow
---

Upvotes: 0

Mauricio Trajano
Mauricio Trajano

Reputation: 2927

If you don't need the original word afterwards
Using Jon Skeets method

string word = "To je";
word = word.Substring(0,word.Length - 3);
word += "...";

Console.WriteLine(word);

Upvotes: 0

M Akin
M Akin

Reputation: 507

as @jon-skeet said, you could do this with substrings. Here are 3 ways that you could do this with substring.

You could use String.Concat

string word = "To je";
word = String.Concat(word.Substring(0,word.Length-3),"...");
Console.WriteLine(word);

You could use the + operator

string word2 = "To je";
word2 = word2.Substring(0,word.Length-3) + "...";
Console.WriteLine(word2);

You could use String.Format

string word3 = "To je";
word3 = String.Format("{0}...",word3.Substring(0,word.Length-3));
Console.WriteLine(word3);

Upvotes: 0

Wilson
Wilson

Reputation: 251

Assuming the string is always at least 3 characters, you could substring everything but the last three characters and then append the three dots (periods) to the end of that string.

string word    = "To je";
string newWord = word.Substring(0, word.Length - 3); // Grabs everything but the last three chars
newWord       += "..."; // Appends three dots at the end of the new string

Console.WriteLine(newWord);

Note: this assumes that the input string word is at least three characters. If you were to supply a shorter string, you would need to supply an extra check on the string's length.

Upvotes: 1

mcmonkey4eva
mcmonkey4eva

Reputation: 1377

if (word.Length > 3)
    Console.WriteLine(word.substring(0, word.Length - 3) + "...");

or something along those lines, no need for a loop!

Upvotes: -1

Servy
Servy

Reputation: 203820

  1. You're replacing all instances of the character at each of those three last positions with a period. You only want to replace that one character at the end. "aaaaa" shouldn't become "....." but rather "aa...".
  2. You're printing out newWord after calculating an intermediate value and then never doing anything with it, leaving word unchanged. You'll want to assign it back to word, after correctly adjusting the character in question.

Of course the far easier solution (both for you, and for the computer) is to simply concat a substring of the string you have that excludes the last three characters with three periods.

Upvotes: 1

Related Questions