Samuel
Samuel

Reputation: 39

How to check for line break in a string?

I am trying to check if a line break exists in a string. I have intentionally included a line break in the string to check if it returns an error. However, it does not return an error despite having a line break in the string.

A sample of the string:

<Project_Title> Proposed 
   Envelope Control </Project_Title>

The code for find the line breaks:

if instr(1,trim(strProjDetails),"</Project_Title>",vbtextcompare) then                      
   if instr(1,trim(strProjDetails), vbCrLf) > 0 Then
      Response.write "sam"  
      call logClass.setLogs(userid, menu, action, "Error! Line break exists in XML File.", f)
      Response.Write("Please remove the line break from the XML file.")
      Response.end
   End if
End if

Upvotes: 0

Views: 1282

Answers (2)

Ralpharama
Ralpharama

Reputation: 461

If this code doesn't work for your strings

    testString = "<Project_Title> Proposed "&VbCrLf&" Envelope Control </Project_Title>" 
    if instr(testString,vbCrLf)>0 or instr(testString,vbCr)>0 or instr(testString,vbLf)>0 then
        Response.Write "Line break"
    else
        Response.Write "No line break"
    end if

Then there's something else going on, and your string doesn't have a line break in it (Adirmola's code checks the same thing, his code should work fine too)

Try getting the Char/Chr codes of the breaks in your string here: http://asciivalue.com/ just to check what the actual thing you're trying to find is!

example line breaks

In this example it's 13 followed by 10 - a VbCrLf

Then you can substitute vbCrLf for chr(whatever).

Best of luck.

Upvotes: 0

Adirmola
Adirmola

Reputation: 873

There are 3 options for line break:

Constant   Value               Description
 ----------------------------------------------------------------
 vbCr       Chr(13)             Carriage return
 vbCrLf     Chr(13) & Chr(10)   Carriage return–linefeed combination
 vbLf       Chr(10)             Line feed

See here: Differences Between vbLf, vbCrLf & vbCr Constants

So if you not sure which one of them is in use in your text I would suggest to search for every one of them as following:

if instr(1,trim(strProjDetails),"</Project_Title>",vbtextcompare) then                      
   if instr(1,trim(strProjDetails), Chr(10)) > 0 or instr(1,trim(strProjDetails), Chr(13)) > 0 or instr(1,trim(strProjDetails), Chr(13) & Chr(10)) > 0 Then
      Response.write "sam"  
      call logClass.setLogs(userid, menu, action, "Error! Line break exists in XML File.", f)
      Response.Write("Please remove the line break from the XML file.")
      Response.end
   End if
End if

Upvotes: 1

Related Questions