|
-
Jan 27th, 2012, 08:47 PM
#1
[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
-
Jan 27th, 2012, 09:21 PM
#2
Hyperactive Member
Re: Get length of string minus the line feeds
-
Jan 27th, 2012, 10:12 PM
#3
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
-
Jan 27th, 2012, 11:10 PM
#4
Re: Get length of string minus the line feeds
this works, but only ignores vblf + not vbcr:
vb Code:
Dim TestString As String = _
"One" & Environment.NewLine & _
"Second Line" & Environment.NewLine & _
"Third and last"
MsgBox(TestString.Count(Function(c) c.ToString <> vbLf))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 27th, 2012, 11:12 PM
#5
Re: Get length of string minus the line feeds
Here's another alternative, not necessarily better or worse:
vb.net Code:
Private Function GetStringLengthWithoutLineBreaks(text As String) As Integer Return text.Split(New String() {ControlChars.NewLine, ControlChars.Cr, ControlChars.Lf}, StringSplitOptions.None).Sum(Function(s) s.Length) End Function
-
Jan 28th, 2012, 12:14 AM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|