0

How do i remove a new line from the start of a string?

what's happening is that i've been controlling and debugging someone else's php code, and converting it into asp. what he did was put html tags in a db table, and simply echoing them. ex, a field contains html table tags like <thead>,<tbody>,<tr>, etc. i didn't want to continue the wrong doing so what i did was to control them by first turning <tr>s into <br />s, and removing everything else. but problem is that the first <tr> makes a new line in the very start of the string. i want to remove it. another problem is that not all fields has htmlt tags inside, so i have to put something like if text.substring(0,1)="(idk what to put here)" then (maybe the replace or trim functions here). any help please?

here's a sample field content. pretty nasty indeed:

    <table width="705" height="323" id="gradient-style" summary="Meeting Results">
      <thead>
        <tr>
          <td width="238">Product</td>
          <td width="610">TS2360 Tape<br />
          Drive Express</td>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Machine/model, HVEC</td>
          <td>3580<br />
          S63, 3580S6X</td>
        </tr>
        <tr>
          <td>Product strengths</td>
          <td>Multi O/S<br />
            Encryption &amp; media<br />
            partition capable<br />
            LTFS support</td>
        </tr>
       </tbody>
    </table>

so after making <tr>s into <br>s and removing other html tags, output beacame:

Product TS2360 Tape Drive Express 
Give background color to the table cells to achieve seamless transition 
Machine/model, HVEC 3580 S63, 3580S6X 
Product strengths Multi O/S Encryption &amp; media partition capable LTFS support

(supposedly skipping a line before "Product" because it has <tr> before it, but didn't show in the block quote)

Thanks in advance.

Thomas
  • 63,911
  • 12
  • 95
  • 141
Lendl Leyba
  • 2,287
  • 3
  • 34
  • 49

1 Answers1

4
var cleanedFieldValue = someValueWithLineBreaks.TrimStart( '\n' );

The VB.NET version might look like:

Dim cleanedFieldValue = someValueWithLineBreaks.TrimStart(ControlChars.Lf)

Edit

It sounds as if you are trying to parse some Html and then do work on it. I would recommend using the Html Agility Pack for that and read about the evils of attempting to use RegEx to parse your Html.

Community
  • 1
  • 1
Thomas
  • 63,911
  • 12
  • 95
  • 141
  • but won't it remove every new line? – Lendl Leyba May 31 '13 at 01:32
  • My apologies. Yes. The fix is easy. – Thomas May 31 '13 at 01:35
  • Could you possibly use Environment.NewLine instead of '\n' ? – m.t.bennett May 31 '13 at 01:45
  • @m.t.bennett This would depend on environment. In this case to keep the code portable, it's better to avoid using Environment.NewLine. – Andrew Savinykh May 31 '13 at 01:50
  • html agility pack? whoo. any other easier solution please? – Lendl Leyba May 31 '13 at 02:58
  • @LeiLeyba - First, the agility pack isn't that difficult. Second, any other solution will end up answering to Cthulhu. You'll spend all your time trying to find patterns to the exceptions and going mad. The agility pack was specifically designed to parse Html and in the end should make your life easier. – Thomas May 31 '13 at 05:16
  • this did it: If .Text.Contains("
    ") Then .Text = .Text.Remove(.Text.IndexOf("
    "), 6) thanks anyway. :)
    – Lendl Leyba May 31 '13 at 05:27
  • @LeiLeyba - So what happens if the BR tag is in the middle of the text or there is an inline style on the preceding tag or the spacing isn't just right or they didn't use a `tbody` or `thead` tag? All of these are valid Html. If you can solve it with a simple replace great. But the dark side of that solution is that the number of valid exceptions is mind boggling. – Thomas May 31 '13 at 05:31
  • @LeiLeyba - Btw, a simpler solution is `.Text = .Text.Replace("
    ", string.Empty)`. No need to check for existence first. Of course, then there is capitalization...
    – Thomas May 31 '13 at 05:32
  • You are thinking far too away from my question, sir. What I did with this problem is to filter the tags I needed to convert and remove everything else. Doing that, the tags became
    tags, and removed everything else. So with that, a single
    tag remains in the start. and that was my problem that was solved by the simple remove. :)
    – Lendl Leyba Jun 05 '13 at 02:34