Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

Dani AI

Generated

The goal in the thread is to increment only the last two characters of a string (example: "abcdef" -> "abcdgh"). ended up using Substring + a small loop and offered an in-place char[] approach; both are fine. Below are a terser modern option and a small note about edge cases you should watch for.

A concise, readable implementation using C# index/range syntax (C# 8+) — no explicit loop or StringBuilder needed:

static string IncLastTwo(string s)
{
    if (string.IsNullOrEmpty(s) || s.Length < 2) return s;
    return s[..^2] + new string(new[] { (char)(s[^2] + 2), (char)(s[^1] + 2) });
}

If the intent is to shift alphabetic letters and wrap inside a-z / A-Z rather than change raw UTF-16 code units, use a small helper that wraps within the alphabet:

static char ShiftAlpha(char c, int shift)
{
    if (c >= 'a' && c <= 'z') return (char)('a' + (c - 'a' + shift + 26) % 26);
    if (c >= 'A' && c <= 'Z') return (char)('A' + (c - 'A' + shift + 26) % 26);
    return (char)(c + shift);
}

static string IncLastTwoAlphaWrap(string s) =>
    string.IsNullOrEmpty(s) || s.Length < 2 ? s : s[..^2] + ShiftAlpha(s[^2], 2) + ShiftAlpha(s[^1], 2);

Troubleshooting / caveats:

  • Always guard for null or strings shorter than 2.
  • Incrementing a char changes the UTF-16 code unit; this is not the same as altering a user-visible grapheme. For emoji or combined characters use System.Globalization.StringInfo to work on text elements: StringInfo docs.
  • For a one-off change of two characters, simple concatenation is cheap. For many edits prefer an in-place char[] or Span<char> to reduce allocations.
  • Adding to a char can wrap the 16-bit value; validate if that behavior is acceptable.

These points complement the solutions in the thread and present a compact, modern alternative while calling out important Unicode and performance considerations.

Recommended Answers

All 4 Replies

Sorry, seems I didn't understand you well, you need to get last two characters and increment their value (means if input string xxxxAB so the result should be xxxxBC)?
if yes get last 2 characters get each of which increment thier ascii value by 1. that's my solution, if you need code reply again.

I've done done that my problem is that I cant preppend the previous characters in the buffer this is my solution:
--------------------
string input = "abcdef";
char[] mychar = input.ToCharArray();
int length = input.Length;
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++ )
sb.Append((char)((int)mychar + 2));
Console.WriteLine(sb.ToString());
Console.ReadLine();
--------------------
However this souliton iterates each character twice and I just want the last two

actually I figuree out but is there another shorter way to doi it :
string input = "mystring";
char[] mychar = input.ToCharArray();
int length = input.Length ;
string newinput = input.Substring(0, input.Length -2);
StringBuilder sb = new StringBuilder(length);
for (int i = length - 2; i < length; i++)
sb.Append((char)((int)mychar + 2));
Console.WriteLine(newinput + sb.ToString());

Hi,
Try following code.

class Program
{
    static void Main(string[] args)
    {
        string temp = "abcdef";
        char[] temp1 = new char[temp.Length];
        temp1 = temp.ToCharArray();
        for (int i = temp.Length-2;i<temp.Length;i++)
        {
            temp1[i] = Convert.ToChar((Convert.ToInt32(temp1[i]) + 2));
        }
        temp = new string(temp1);
    }
}

Finally the string " temp " contains the result you want.

Please feel free to ask me if you have any queries and let me know the code is working correctly or not.

Regards,
Vijay Bhaskar

Hi I'm now into data structures:
this is the problem:
string s = "abcdef"
char[] mychar = string.TocharArray();
int Length = s.length;
StringBuilder sb = new StringBuilder(Length);
-------Basically I need to construt a new string with the last two character iterated twice if the string = the same string.
does anybody now how?
the result would be "abcdgh"---> note the last two character are iterated twice

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.