String.Contains("\n") returns false

Ok, I have a string that has
in it, but string.Contains("
") always returns false.

			string temp = lp.levelName;
			Debug.Log(temp);
			Debug.Log("

" + temp.Contains("
“));
temp = temp.Replace(”
", “”);
Debug.Log("Replace
" + temp);
nameText.text = temp;

Log output (after a lot of useless info)

MATCH

3
False
Replace
MATCH
3

I use

t.levelName = EditorGUILayout.TextField("Level Name", t.levelName);

to enter the level name, so maybe it’s doing some strange modifications to the string there.

Edit:

Ok seconds after posting this I find it’s a bug with EditorGUILayout.TextField

http://forum.unity3d.com/threads/177932-BUG-EditorGUILayout-TextField-escapes-the-string

2 Answers

2

You need to escape the backslash or use a here-string.

Either of these should work:

     Debug.Log(temp.Contains("\

“));
Debug.Log(temp.Contains(@”
"));

And it’s not a bug - it’s how programming languages work -
means “newline” so you have to tell it in some way that you really want

  • not “newline”

That's not how they work, if I type \\n I want a newline. So if I write \\n in the editor I should expect the same thing as if I wrote that directly in the code. Otherwise you don't have a way to create newlines in the editor, enter just accepts.

I can't agree with you. The editor is a single line field. What's there is the equivalent of an in-string value. If you want to be able to write newlines in the editor there's a generic way you could hack it in - write a placeholder, like .nl. and then when you start the application, loop through each of the objects and rename them - replacing the .nl. with a newline character.

That is what I had to do, but because I had strings coming from different sources it was quite a bit of work. But they should give us a toggle or something, or maybe just a EditorGUILayout.String();

is an escaped character, not a string. You can either escape it by using the @ symbol before using a string containing the character…(which is pointless), OR use it as the character it is:

Debug.Log(temp.Contains('

'));

Notices the single quote, indicating a character type instead of string type.

Does not even compile, Contains expects a string, not a char, and Replace, that is overloaded with char, does not replace it. @ works, but then that's what I was doing.