I’m writing a simple algorithm which ahould write in the console everytime a certain condition is met. Here is my code:
public static void StringTest()
{
string stringToUse = "Ala BalaB JiBBerish Ala Jibberish Ala BalaB";
int strLength = stringToUse.Length;
int i = 0;
while(i < strLength-4)
{
if (stringToUse[i] == stringToUse[(i + 4)] && stringToUse[(i + 1)] == stringToUse[(i + 3)])
System.Console.WriteLine(stringToUse[i] + stringToUse[(i + 1)] + stringToUse[(i + 2)] + stringToUse[(i + 3)] + stringToUse[(i + 4)]);
i++;
}
}
But instead a literal string the output is numbers 434 . How should I format the ouput to get the actual letters if maybe someone could expain me why I don’t get an error and those numbers instead?
You’re adding characters together – which will give you an integer value. It looks like you should probably just be using Substring:
Alternatively you could format the characters:
(Note the removal of extraneous parentheses, by the way. Sometimes extra brackets are useful – here they’re just distracting.)