PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to 'Trim' a multi line string?

 September 05, 2022     c#, string, text, trim     No comments   

Issue

I am trying to use Trim() on a multi line string, however only the first line will Trim(). I can't seem to figure out how to remove all white space from the beginning of each line.

string temp1 = "   test   ";
string temp2 = @"   test
                    line 2 ";

MessageBox.Show(temp1.Trim());
//shows "test".

MessageBox.Show(temp2.Trim());
//shows "test"
        "       line2 ".

Can I use Trim/TrimStart/TrimEnd on a multi line string?


Solution

Can I use Trim/TrimStart/TrimEnd on a multi line string?

Yes, but it only Trims the string as a whole, and does not pay attention to each line within the string's content.

If you need to Trim each line, you could do something like:

string trimmedByLine = string.Join(
                             "\n", 
                             temp2.Split('\n').Select(s => s.Trim()));


Answered By - Reed Copsey
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing