0

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
user692942
  • 16,398
  • 7
  • 76
  • 175
Samuel
  • 39
  • 1
  • 7

2 Answers2

1

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
Adirmola
  • 783
  • 5
  • 15
  • Hi, I would like to use vbCrLf. if instr(1,trim(strProjDetails), "vbCrLf") > 0 (will the code look like this?) – Samuel Sep 04 '19 at 06:49
  • "vbCrLf" is to search this string in your main string. You need to use it without "". I think it's better for you to search all the 3 options. – Adirmola Sep 04 '19 at 07:34
  • Please update us if the answer was helpful. Also, if it's please mark the answer as correct. – Adirmola Sep 04 '19 at 08:30
  • Hi Adirmola, I've tried without the "" but it didnt work either. I've tried another way of validating linebreaks, but it is unique to codes that I receive from the client. – Samuel Sep 05 '19 at 02:59
0

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.

Ralpharama
  • 441
  • 4
  • 20