views:

582

answers:

5

Hi guys,

is there any possiblity to get multiline string in classic asp (I think vbscript is the language)?

I want a multiline string like in python or groovy:

def str = """hello I am a multiline string"""

I searched a lot but didn't find a solution.

Workarounds are welcome too.

BTW: I had in javascript the same problem and solved it back in time with a function saved in a variable. This function had a multiline comment in it so I could through everything away except the comment using regex.

Something like this:

var multilinestr = function() {
/*
hello
I am a multiline
string
*/
}

And after Regex I got a String which contains:

hello
I am a multiline
string

Thank you.

Edit:

I think I missed a very important point. My client is you using something like a "pre processor" for his scripts. It looks like this:

Dim str 
str = "<%std_text%>"

The "pre processor" exchanges "<%std_text%>" with a text which comes from a Database. But this text have breaks in it so I can't just put a '" & vbNewline ' to the end of line. This means after "pre processing" it looks like this:

Dim str 
str = "hello 
I am a multiline
string"

Is there anyway to get this "text" in a string?

If I could write something like this (groovy):

def multistr = """<%std_text%>"""

after "pre processing":

def multistr = """hello
I am a multiline
string"""

It would be great!

+3  A: 

Could you not add carriage returns to the string?

Dim myString : myString = "Line 1" & vbCrLf & "Line 2"... etc

Mick Walker
Yes, pretty sure this is your only recourse.
Paul
Just don't forget that Classic ASP/VBScript won't let you set the variable in the same line you declare it.
AnonJr
My bad - its been a while :)
Mick Walker
A: 

There's only two ways that I know of and I've been doing this for 10+ years.

Dim x: x = "Hello" & vbNewline & "World"

or

Dim x: x = "Hello" & vbNewline & _
        "World"

I guess there's the hack way, too:

Dim x: x = Replace("Hello\nWorld", "\n", vbNewline)
Chris Haas
Just don't forget that Classic ASP/VBScript won't let you set the variable in the same line you declare it.
AnonJr
Ha, ha, ha, should have actually tested it, been doing more VB.Net than VBS for so long now! Thanks @AnonJr!
Chris Haas
Not a problem. I have been mired in a VBScript problem here lately so I've got all the fun little quirks at the forefront of my mind.
AnonJr
+2  A: 

Do you want multi-line as in the plain text sent to the browser is multi-line? Or do you want the rendered text to be multi-line?

If its the former, a solution like Mick's works. You can use either vbCrLf or vbNewLine to create a new line in the text sent to the browser. IIRC the latter is preferred as it provides either the carriage return or the carriage return/line feed as appropriate for the client.

Dim myString : myString = "hello" & vbNewLine & "I am a multi-line" & vbNewLine & "string"

If its the latter, you just need to put a <br /> where you want the browser to create a new line. In this case, you may want to think about why you want it to display the way you want it to display as there may (or may not) be a better way of doing it.

AnonJr
@AnonJr:the breaks are in the Text coming from the database. Hopefully this will answer your question.
OemerA
+1  A: 

python:

text = """"
hello world
this is some text
"""

vbscript:

text = "" & vbcrlf &_
"hello world" & vbcrlf &_
"this is some text" & vbcrlf

You can also write a custom stringbuffer class etc.

with new StringBuffer
    .writeline "hello world"
    .writeline "this is some text"
    result = .as_string
end with

Just KISS... I mean a 'preprocessor' for a scripting language? That doesn't sound good....

If you really need to use the preprocessor (i18n?) than you will need to modify it such to replace all line breaks by " & vbcrlf & ".

Joost Moesker
+1  A: 

Unfortunately you will not be able to use this as-is as far as I can tell. You would need to modify the pre-processor to replace the vbNewLine's with an actual vbNewLine variable rather then true line breaks. There is no way in VB Script to concatenate a string on multiple lines without using & _ which requires you to close off the string before doing so on each line, which doesn't seem possible with this setup.

Shawn Steward
Thank you! I think it really is impossible to create a multiline string with VB Script.
OemerA