Special chars in strings
Special chars in strings
I know it has already be asked, and that it adds a bit of complexity to a beginner language, but I've had numerous times this problem. The last time was with the form designer : I wanted to have a default string with double quotes (") chr(34). The form designer accepted it, but complained when changing tooltip (!)
So, wouldn't it be possible to internally parse the strings so that we can use these special chars. Maybe with a \, or a special code, "&34", I don't know, I just say that it would be really valuable !
Thank you !
So, wouldn't it be possible to internally parse the strings so that we can use these special chars. Maybe with a \, or a special code, "&34", I don't know, I just say that it would be really valuable !
Thank you !
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Special chars in strings
'Escaped' characters is a commonplace solution. I think the (15) escapes from C are handy..........
\a Alarm (Beep/Bell)
\b Backspace
\f Formfeed
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\' Single quote
\" Double quote
\? Question mark
\0 Null
\ooo Octal. There can be 1, 2 or 3 octal digits, \o, \oo and \ooo are all valid
\xhh Hex
\uhhhh Unicode character
\a Alarm (Beep/Bell)
\b Backspace
\f Formfeed
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\' Single quote
\" Double quote
\? Question mark
\0 Null
\ooo Octal. There can be 1, 2 or 3 octal digits, \o, \oo and \ooo are all valid
\xhh Hex
\uhhhh Unicode character
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Special chars in strings
+1 totally agree.IdeasVacuum wrote:'Escaped' characters is a commonplace solution. I think the (15) escapes from C are handy..........
\a Alarm (Beep/Bell)
\b Backspace
\f Formfeed
\n Newline (Line Feed)
\r Carriage Return
\t Horizontal Tab
\v Vertical Tab
\\ Backslash
\' Single quote
\" Double quote
\? Question mark
\0 Null
\ooo Octal. There can be 1, 2 or 3 octal digits, \o, \oo and \ooo are all valid
\xhh Hex
\uhhhh Unicode character
The bad is that for storing windows path we would need to \\ a lot, and it would be really ugly. I'd rather prefer an escape character rarely used, like the old ESC character '^', but I know the balance will be in favor of '\'...
Re: Special chars in strings
@djes
I think you can also use / in windows path.
I think you can also use / in windows path.
Re: Special chars in strings
Yes, for internal commands, but if we design a windows program with hardcoded paths in strings, it will be a pain to write \\ everywhere :/infratec wrote:@djes
I think you can also use / in windows path.
Re: Escape characters in strings
If I had to guess, it is unlikely Fred will implement this system-wide. If he did, every bit of code containing a string with a backslash from the beginning of PB time would require a rewrite...and it could be a difficult debug, too. Especially with strings that are rarely used.
With this routine you can replace escape chars at your leisure, rather than be forced to use it everywhere (massive rewrites), or add a flag to every string routine (every string call becomes slower).
With this routine you can replace escape chars at your leisure, rather than be forced to use it everywhere (massive rewrites), or add a flag to every string routine (every string call becomes slower).
Code: Select all
Procedure.s SwapEscapeCharacters(s.s)
s = ReplaceString(s, "\r", #CR$) ; 13 ; NOTE: Windows (CRLF)
s = ReplaceString(s, "\t", #TAB$) ; 9
s = ReplaceString(s, "\n", #LF$) ; 10
s = ReplaceString(s, "\\", "\") ; 92
ProcedureReturn s
EndProcedure
Re: Escape characters in strings
Yes, but it wouldn't be a step backwardTenaja wrote:If I had to guess, it is unlikely Fred will implement this system-wide. If he did, every bit of code containing a string with a backslash from the beginning of PB time would require a rewrite...and it could be a difficult debug, too. Especially with strings that are rarely used.

Of course, but the goal of this thread is precisely to have it natively.Tenaja wrote:With this routine you can replace escape chars at your leisure, rather than be forced to use it everywhere (massive rewrites), or add a flag to every string routine (every string call becomes slower).
Code: Select all
Procedure.s SwapEscapeCharacters(s.s) s = ReplaceString(s, "\r", #CR$) ; 13 ; NOTE: Windows (CRLF) s = ReplaceString(s, "\t", #TAB$) ; 9 s = ReplaceString(s, "\n", #LF$) ; 10 s = ReplaceString(s, "\\", "\") ; 92 ProcedureReturn s EndProcedure
Re: Escape characters in strings
Then my vote is...djes wrote: Of course, but the goal of this thread is precisely to have it natively.
-1...NOOOOooooo....
I chose PB over C because of its native string handling. I far have too much string code to support this request. For the rare times you need it, it is very simple to call a proc.
Re: Escape characters in strings
Tenaja wrote:Then my vote is...djes wrote: Of course, but the goal of this thread is precisely to have it natively.
-1...NOOOOooooo....
I chose PB over C because of its native string handling. I far have too much string code to support this request. For the rare times you need it, it is very simple to call a proc.

Re: Special chars in strings
I would like support for escaped strings.
To not cause any problems, using a prefix could be a solution.
For example
A.s = %"A string\r\nwith escaped characters\r\ninside it"
for an escaped string and
A.s = "Normal string"
for a normal string.
To not cause any problems, using a prefix could be a solution.
For example
A.s = %"A string\r\nwith escaped characters\r\ninside it"
for an escaped string and
A.s = "Normal string"
for a normal string.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
-
- Always Here
- Posts: 6426
- Joined: Fri Oct 23, 2009 2:33 am
- Location: Wales, UK
- Contact:
Re: Special chars in strings
Good suggestion Wilbert
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
If it sounds simple, you have not grasped the complexity.
Re: Special chars in strings
Yes, good idea, or ala PHP syntax : strings with simples quotes ('\n\p\\') are parsed, and strings with double quotes ("\my damn path"), not parsed.
Re: Special chars in strings
The single quote in PB is reserved for Characters:
Code: Select all
Debug 'A' ; gives 65
Debug 'φ' ; gives 966
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Re: Special chars in strings
Oh yes, I've almost forgotten that ! Big jump in the past ! Thank you for this precision.STARGÅTE wrote:The single quote in PB is reserved for Characters:Code: Select all
Debug 'A' ; gives 65 Debug 'φ' ; gives 966
Re: Special chars in strings
There is another language that uses the tilde to prevent the ugly, but on Windows common, path problems with '\':
Code: Select all
Procedure.s Esc(s.s)
s = ReplaceString(s, "~q", Chr( 34)) ; 34 (quotation mark ")
s = ReplaceString(s, "~n", Chr( 10)) ; 10 (newline)
s = ReplaceString(s, "~r", Chr( 13)) ; 13 (return)
s = ReplaceString(s, "~t", Chr( 9)) ; 9 (tab)
s = ReplaceString(s, "~~", Chr(126)) ; 126 (tilde ~)
ProcedureReturn s
EndProcedure
Debug Esc("some ~qText~q ~~ ~t~t~t~t and a~r~nnew line")