- Home /
The question is answered, right answer was accepted
Unable to compare double quotation character specifically in Unity
I am trying to iterate through a string and compare a double quotation character. However, for some reason I can't figure out how to make the comparison work. The part that's confusing me most of all is that when I run this code in a basic c# editor it works perfectly, but as soon as I put it in my Unity c# script, the comparison just doesn't work for some reason.
Any help would be greatly appreciated, especially if you could explain why my code snippet works when it's not attached to my Unity script.
if (str[i-1] == '\"') {
print("Successful comparison");
}
I've also tried comparing (str[i-1] == '"'), and (str[i-1] == (char)34), all to no avail :/
I don't have any problem with if ( str[i-1] == '"' ) print( "Successful comparison" );
That's it, you probably don't need to escape double quote character if you define a char!
EDIT: actually, it works with `'\"` as well. @brooksiieeee, could you show more of the surrounding code around what you already posted?
string AddNewline(string str, int split)
{
string newString = "";
int i = 1;
int lineCounter = 1;
if (str.Length < split)
{
split = str.Length;
}
while (i < (str.Length + 1))
{
if (str[i-1].Equals('\"'))
{
print("Successful Comparison");
}
//if (str[i-1] == '\\' && str[i - 2] != '\"')
if (str[i-1].Equals('\\') && !(str[i-2].Equals('\"')))
{
//print(str[i - 2]);
//skip the n and space
i += 3;
newString += Environment.NewLine;
lineCounter = 1;
}
newString += str[i-1];
if (lineCounter % split == 0)
{
if(str[i-1] != ' ')
{
i++;
while(i < (str.Length + 1) && str[i-1] != ' ')
{
newString += str[i - 1];
i++;
lineCounter++;
}
}
newString += Environment.NewLine;
}
i++;
lineCounter++;
}
return newString;
}
This is direct copy-paste from my code.
I have also tried the comparisons without the escape character.
Answer by brooksiieeee · Apr 24, 2018 at 10:29 PM
Okay this worked!!
I'm guessing the problem is that I am reading my strings in from a file.
Given this, I'm now not sure how to parse the double quote out of the string I'm getting from a file. Do you have any ideas about this?
EDIT: Solved the problem, turns out it was the fact that I had exported my .txt file from a source that used different quotations than the ascii evaluations.
Answer by Priyanka-Rajwanshi · Apr 24, 2018 at 08:57 AM
@brooksiieeee If str is an array of string use:
if (str[i-1].Equals("\"") {
print("Successful comparison");
}
If str is an array of char use:
if (str[i-1].Equals('\"'){
print("Successful comparison");
}
Although your answer is technically correct, string has the array access operator overloaded, and it returns the char
at the given index. The OPs code should work as it is.
@Priyanka-Rajwanshi Unfortunately this made no difference with the specific double-quotes issue.
Both my original code and your recommendation work fine for comparing other characters, but as soon as I try to compare a double quote it fails.
I have actually tested the code and it works fine. Are you sure that str[i-1] = ""?
I just double checked and should at some point it should find a match.
The string in particular is
print(“Hello”, “You”, sep = “\n”)
But I'm not receiving any matches on any of the quotes in that string. I also put in the line below to see what it would print one of the quotes as:
if (str[i-1].Equals('\\') && !(str[i-2].Equals('\"')))
print(str[i-2])
}
It found the \, but was unable to find the quote.
However, it did print what appeared to be a double quote character " , which is confusing.