Chopping off newlines

Results 1 to 6 of 6

Thread: Chopping off newlines

  1. #1
    Join Date
    Dec 1969
    Posts
    290

    Default Chopping off newlines

    Hi there,<BR><BR>I&#039;m having absolutely no luck trying to write lines of code that&#039;ll remove any and all carriage returns - newlines from the end of a user&#039;s input. <BR><BR>Unfortunately, we&#039;re running a version of ASP that does not support the RegExp object.<BR><BR>Can anyone please help me with the necessary string routine code to perform such a task?<BR><BR>Thanks!

  2. #2
    Join Date
    Dec 1969
    Posts
    18,177

    Default Now this is funny.

    Someone else wanted the exact same code.<BR><BR>http://www.27seconds.com/kb/article_view.asp?id=18<BR><BR>That should do it. Keep in mind that it&#039;ll do newlines, tabs, space and a bunch of other things. You may need to muck with the code a little.

  3. #3
    Join Date
    Dec 1969
    Posts
    96,118

    Default Can I suggest an improvement...

    ...to that code?<BR><BR>Instead of this:<BR><BR>Dim m_aBadChars(4)<BR> m_aBadChars(0) = vbCrLf<BR> m_aBadChars(1) = vbCr<BR> m_aBadChars(2) = vbLf<BR> m_aBadChars(3) = vbTab<BR> m_aBadChars(4) = " "<BR><BR>Do this:<BR><BR>Dim m_aBadChars<BR>m_aBadChars(0) = Array( vbCrLf, vbCr, vbLf, vbTab, " " )<BR><BR>ALSO:<BR><BR>If you will use this function more than once per page, move the array and its initialization *OUT* of the function. As it is, the array is destroyed every time the function finishes and has to be rebuilt on the next call. Since it&#039;s an array of constants, this is a waste of time.<BR><BR>And after that...<BR><BR>The code doesn&#039;t make sense.<BR><BR>You replace LF with nothing. You replace CR with CRLF.<BR><BR>And then you *STILL* have CR and LF in the list of BadChars!!!<BR><BR>If you just got through replacing them, why do you need to check for them in the loop???<BR><BR>

  4. #4
    Join Date
    Dec 1969
    Posts
    18,177

    Default RE: Can I suggest an improvement...

    Good point about the array creation. I will actually move that outside of the function and use the Array() syntax.<BR><BR>Need to look into your "And after that.." part.<BR><BR>It&#039;s been so long since I&#039;ve written that code.


  5. #5
    Join Date
    Dec 1969
    Posts
    96,118

    Default Well, if you'll take that suggestion...

    &nbsp;<BR>&#060;%<BR>Function RemoveTrailing( str, bad )<BR> Do Until InStr( bad, Right(str,1) ) = 0 <BR> str = Left( str, Len(str)-1 )<BR> Loop<BR> RemoveTrailing = str<BR>End Function<BR><BR>Dim whitespace<BR>whitepace = vbCR & vbLF & vbTab & " "<BR><BR>Function RemoveTrailingWhitespace( str )<BR> RemoveTrailingWhitespace = RemoveTrailing( str, whitespace )<BR>End Function<BR>%&#062;<BR><BR>Nice part is you can test the first function with things like<BR> RemoveTrailing( "this is a test 123", "321" )<BR>and see it working.<BR><BR><BR>

  6. #6
    Join Date
    Dec 1969
    Posts
    96,118

    Default And then this...

    ...would be more efficient if, on average, you expect 2 or more "bad" characters on the end of your strings:<BR><BR>Function RemoveTrailing( str, bad )<BR> Dim i<BR> For i = Len(str) To 1 Step -1<BR> If InStr( bad, Mid(str,i,1) ) = 0 Then Exit For<BR> Next<BR> RemoveTrailing = Left( str, i )<BR>End Function<BR>

Posting Permissions

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