Posted: 11th Jun 2007 5:28
Hello. I'm attempting to create a message box function and I'm having trouble with one part of it.

function messagebox(text$)

I want it to find if I typed a / or /n into text$ and if so do a return and remove the characters and continue with the rest of it.

Any help would be great.

P.S> I tried a for next loop but it caused the program to hang...
Posted: 11th Jun 2007 7:09
Use a For..Next loop, but start at the end of the line and work backwards to the start of the line.

Use Mid$() to check if the character is a '\' and if so, note the position.

Grab the whole line from the start to that position -1 into a string.

Grab the line from that position +1 to the end of the line in another string.

Add Chr$(13) + Chr$(10) to the end of the first string and add the second string on the end of the result.

It's slightly different if you are using a string array though...

TDK_Man
Posted: 11th Jun 2007 22:36
so go from len(text$) to 1?
Posted: 11th Jun 2007 23:43
I'm not sure there's a need to run through the string backwards - It's just as easy to amend it running forwards:
+ Code Snippet
InputLine as string = "Hello\nWorld"

print ReplaceNewline( InputLine )
wait key
end

function ReplaceNewline(InputLine as string)
   local Result as string
   local i
   local l

   l = len(InputLine)
   i = 0
   while i < l
      inc i
      if mid$(InputLine, i) = "\" and mid$(InputLine, i+1) = "n"
         InputLine = left$(InputLine, i-1) + chr$(13) + chr$(10) + right$(InputLine, l - i - 1)
         inc i, 1
      endif
   endwhile
endfunction InputLine


You can also use one of my plug-ins (isn't that a surprise ) to do the same thing:
+ Code Snippet
InputLine as string = "Hello\nWorld"

print replace all$( InputLine, "\n", chr$(13) + chr$(10) )

wait key
end

... which in most cases makes things a lot easier.
Posted: 12th Jun 2007 3:08
I had something specific in mind when I said start at the end of the line, but I can't for the life of me remember what it was!

I must have been thinking more about when you split lines to create completely new lines - if you start at the beginning of the line, once it's split, the For..Next loop breaks because the rest of the line is gone. So you start at the end instead...

But for what you're doing it works exactly the same whichever end you start at!

TDK_Man
Posted: 12th Jun 2007 4:05
thanks everyone, It's been solved