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
2
Question by AndreasX12 · Aug 26, 2014 at 04:19 PM · runtimeremovenewline

Remove "new lines" from string variable? C#

Hi.

I have a simple string variable with multiple lines, so I'm trying to make all the lines into one single line.

I have tried this code, but it doesn't work:

transform.guiText.text = transform.guiText.text.Replace(System.Environment.NewLine, "");

How is this possible?

Best regards, Andreas.

Comment
Add comment · Show 1
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 robertbu · Aug 26, 2014 at 04:40 PM 0
Share

It appears that System.Envronment.NewLine is system specific. Are you sure the text you are processing has the same definition of System.Envronment.NewLine? Print out the length and the ASCII values of the characters in the System.Environment.NewLine string and compare it to he characters used for newlines in the text.

2 Replies

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

Answer by digibawb · Aug 26, 2014 at 05:22 PM

The simplest way would be to do multiple calls to replace to remove both the carriage return and line feed characters individually, as you probably don't really care which order they were in, and just want rid of them.

newString = oldString.Replace("\r", "").Replace("\n", "");

I'm not going to suggest that this is the best way of doing it, but it is at least straightforward.

Comment
Add comment · Show 2 · 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 AndreasX12 · Aug 26, 2014 at 05:25 PM 1
Share

Thank you so much! It worked perfectly :D

avatar image Yonal · Apr 25, 2019 at 04:02 AM 1
Share

thanks man this saved the day

avatar image
1

Answer by ephraimdov · Jul 23, 2015 at 08:46 AM

Remove new line from a string

myString.Replace(System.Environment.NewLine, "replacement text")

or

Regex.Replace(str, @"\t\n\r", "");

Or

str.Replace("\n", String.Empty); str.Replace("\r", String.Empty); str.Replace("\t", String.Empty);

Remove space from a string

xyz.Replace(" ", string.empty);

Full Source.....Remove new line from a string

Dov

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 Bunny83 · Jul 23, 2015 at 08:57 AM 0
Share

Well, first of all this question is a year old and has already a suitable answer. Of course if you have a better answer there's no problem posting one. However you said:

You can remove spaces and new line from a string in the following ways..

But that's not true:

  • No spaces will be removed with any of those 3 approaches. That wasn't even required

  • The first and third approach removes tab (\t) characters as well, the second doesn't.

  • The second approach doesn't remove newline characters but replace them with commas(,).

  • The third code example is in VB.NET and not C#. You copied the wrong example.

So it's questionable if this answer is an improvement ^^. It looks like a quick copy & paste without any checking.

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

26 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 avatar image avatar image 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

Export objects to a .3DS file at runtime 1 Answer

Checking if a component has been removed or not at runtime 1 Answer

How to edit 3D Text at runtime? 1 Answer

Is there a tool to measure a distance in runtime? 4 Answers

change the mesh of object during runtime using javascript 1 Answer


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