Reputation: 297
If I have a string variable like this:
string f = "ABC";
I want to make it like this:
f="CDE"
This means that I want to take every char in this string and increase it to the next 2 values, if I have 'a' I want to change it to 'c' and so on.
Upvotes: 3
Views: 3701
Reputation: 217293
You can convert the string to a char[]
, modify each char
as needed, and convert the result back to a string as follows:
char[] chars = "ABC".ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
chars[i] += (char)2;
}
string result = new string(chars);
// result == "CDE"
Upvotes: 1
Reputation: 1327
All you need to do is get the individual char from the string e.g.
string a = "aba";
char b = a[0]; //the value is equal to 'a'
Console.WriteLine((char)((int)b + 1));
then convert the char into an int and increament it then convert it back to a char
Upvotes: 1
Reputation: 223247
Following will increment the character to + 2, Not sure what you want when then characters are ending character in the alphabets.
string f = "ABC";
string result = new string(f.Select(r =>(char) (r + 2)).ToArray());
For string ABC
result will be CDE
, but for string XYZ
result will be Z[\
Upvotes: 9