Results 1 to 6 of 6

Thread: [RESOLVED] Get length of string minus the line feeds

  1. #1

    Thread Starter
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Resolved [RESOLVED] Get length of string minus the line feeds

    Need to get length of a string minus the line feed chars and came up with this. Is there a better/more efficient way?

    Code:
    Function GetLenMinus_LF(s As String) As Integer
        s = s.Replace(ControlChars.Lf, String.Empty)
        Return s.Length
    End Function
    Last edited by Edgemeal; Jan 28th, 2012 at 12:35 AM. Reason: resolved

  2. #2
    Hyperactive Member
    Join Date
    Jan 2012
    Location
    Salt Lake City
    Posts
    313

    Re: Get length of string minus the line feeds

    what about cr and crlf?

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Get length of string minus the line feeds

    Perhaps the following (tweak [\n\r] as needed)

    Code:
    Dim TestString As String = _
    "One" & Environment.NewLine & _
    "Second Line" & Environment.NewLine & _
    "Third and last"
    
    Console.WriteLine("[{0}]", TestString.Length)
    Console.WriteLine("[{0}]", TestString.CleanLength)
    Place in code modules
    Code:
    <System.Runtime.CompilerServices.Extension()> _
    Public Function CleanLength(ByVal sender As String) As Integer
        Return System.Text.RegularExpressions.Regex.Replace(sender, "[\n\r]", "").Length
    End Function

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Get length of string minus the line feeds

    this works, but only ignores vblf + not vbcr:

    vb Code:
    1. Dim TestString As String = _
    2.     "One" & Environment.NewLine & _
    3.     "Second Line" & Environment.NewLine & _
    4.     "Third and last"
    5.  
    6. MsgBox(TestString.Count(Function(c) c.ToString <> vbLf))

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Get length of string minus the line feeds

    Here's another alternative, not necessarily better or worse:
    vb.net Code:
    1. Private Function GetStringLengthWithoutLineBreaks(text As String) As Integer
    2.     Return text.Split(New String() {ControlChars.NewLine,
    3.                                     ControlChars.Cr,
    4.                                     ControlChars.Lf},
    5.                       StringSplitOptions.None).Sum(Function(s) s.Length)
    6. End Function
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Get length of string minus the line feeds

    Thanks for the suggestions,
    I did some quick tests and using .Replace(Lf, "") was actually the fastest for my use, I wasn't expecting that,... i just remember Replace being sort of slow in VB6.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width