Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ishlilith · Apr 11, 2013 at 05:01 PM · string

String.Contains("\n") returns false

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

             string temp = lp.levelName;
             Debug.Log(temp);
             Debug.Log("\n" + temp.Contains("\n"));
             temp = temp.Replace("\n", "");
             Debug.Log("Replace \n " + temp);
             nameText.text = temp;

Log output (after a lot of useless info)

 MATCH\n3
 False
 Replace 
  MATCH\n3

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Dracorat · Apr 11, 2013 at 05:09 PM

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

Either of these should work:

      Debug.Log(temp.Contains("\\n"));
      Debug.Log(temp.Contains(@"\n"));

And it's not a bug - it's how programming languages work - \n means "newline" so you have to tell it in some way that you really want \n - not "newline"

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ishlilith · Apr 11, 2013 at 05:22 PM 0
Share

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.

avatar image Dracorat · Apr 11, 2013 at 05:26 PM 0
Share

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.

avatar image ishlilith · Apr 11, 2013 at 05:37 PM 0
Share

That is what I had to do, but because I had strings co$$anonymous$$g 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();

avatar image
0

Answer by Yokimato · Apr 11, 2013 at 05:55 PM

\n 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('\n'));

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ishlilith · Apr 11, 2013 at 06:13 PM 1
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Extract number from string? 3 Answers

How do I use a Space between variables in a string? 1 Answer

How would I cast "someString" in the following code in order to get it to work? 1 Answer

iOS Not Converting String to Integer 0 Answers

converting string from a textfield to float 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges