how to write string literal with new lines in Pharo - smalltalk

How do you write a string literal with new line characters in Pharo 9? I tried the following but neither of them inserted the new line:
a := 'paragraph1\n\nparagraph2'.
a := 'paragraph1\\n\\nparagraph2'.
The only way I could see to do it was through concatenation like so:
a := 'paragraph' ,
(String with: Character cr with: Character cr),
'new paragraph' ,
(String with: Character cr with: Character cr)
Is there a simpler (and shorter) way to do this?

You just do your line:
multiLineString := 'paragraph1
paragraph2
paragraph3'.
Pharo (as any other Smalltalk AFAIK) has multiline strings, you do not need any special notation as in Python or others.
EDIT: Note that while my example will be a literal, yours will not (there will be 2 literals there, and the resulting string will not be a literal.
EDIT 2: There is also String cr.
EDIT 3: It can also be constructed with streams:
myMultiLineString := String streamContents: [ :stream |
stream
nextPutAll: 'paragraph1'; cr;
nextPutAll: 'paragraph2'; cr ]

You can use the <n> placeholder in your String and send it the expandMacros message - this will expand the placeholder to the platform line separator(s):
a := 'paragraph1<n>paragraph2' expandMacros.
expandMacros and its variants also accept placeholders for tabs, cr, lf and parameters. See the comment to String>>expandMacrosWithArguments: for more details.

Related

Escape hex like \u... in kotlin strings

I have a string "\ufffd\ufffd hello\n"
i have a code like this
fun main() {
val bs = "\ufffd\ufffd hello\n"
println(bs) // �� hello
}
and i want to see "\ufffd\ufffd hello", how can i escape \u for every hex values
UPD:
val s = """\uffcd"""
val req = """(?<!\\\\)(\\\\\\\\)*(\\u)([A-Fa-f\\d]{4})""".toRegex()
return s.replace(unicodeRegex, """$1\\\\u$3""")
(I'm interpreting the question as asking how to clearly display a string that contains non-printable characters.  The Kotlin compiler converts sequences of a \u followed by 4 hex digits in string literals into single characters, so the question is effectively asking how to convert them back again.)
Unfortunately, there's no built-in way of doing this.  It's fairly easy to write one, but it's a bit subjective, as there's no single definition of what's ‘printable‘…
Here's an extension function that probably does roughly what you want:
fun String.printable() = map {
when (Character.getType(it).toByte()) {
Character.CONTROL, Character.FORMAT, Character.PRIVATE_USE,
Character.SURROGATE, Character.UNASSIGNED, Character.OTHER_SYMBOL
-> "\\u%04x".format(it.toInt())
else -> it.toString()
}
}.joinToString("")
println("\ufffd\ufffd hello\n".printable()) // prints ‘\ufffd\ufffd hello\u000a’
The sample string in the question is a bad example, because \uFFFD is the replacement character — a black diamond with a question mark, usually shown in place of any non-displayable characters.  So the replacement character itself is displayable!
The code above treats it as non-displayable by excluding the Character.OTHER_SYMBOL type — but that will also exclude many other symbols.  So you'll probably want to remove it, leaving just the other 5 types.  (I got those from this answer.)
Because the trailing newline is non-displayable, that gets converted to a hex code too.  You could extend the code to handle the escape codes \t, \b, \n, \r and maybe \\ too if needed.  (You could also make it more efficient… this was done for brevity!)
Simply escape the \ in your strings by adding another backslash in front of it:
val bs = "\\ufffd\\ufffd hello\n"
You can also use raw strings with """ so you don't have to escape the backslashes (which is useful for regex):
val bs = """\ufffd\ufffd hello\n"""
Note that in that case the \n would also NOT be counted as an LF character, and will be literally printed as the 2 characters "\n".
You can add literal line breaks in your raw string if you want an actual line feed, though:
val bs = """\ufffd\ufffd hello
"""

How to add a newline to a variable?

I'm trying to append a trailing newline to a variable like:
data := "text"
Clipboard := data "\n"
But that leads to a data with literal \n. All other tries without quotation marks leads to errors.
Instead of using a backslash \ AHK uses a backtick `.
So you'd do a line feed character with `n, or a carriage return with `r, or a horizontal tab with `t, and so on.
data := "text"
Clipboard := data "`n"

Printing Unnecessary escape character [duplicate]

I tried many ways to get a single backslash from an executed (I don't mean an input from html).
I can get special characters as tab, new line and many others then escape them to \\t or \\n or \\(someother character) but I cannot get a single backslash when a non-special character is next to it.
I don't want something like:
str = "\apple"; // I want this, to return:
console.log(str); // \apple
and if I try to get character at 0 then I get a instead of \.
(See ES2015 update at the end of the answer.)
You've tagged your question both string and regex.
In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\.
The following string starts with one backslash, the first one you see in the literal is an escape character starting an escape sequence. The \\ escape sequence tells the parser to put a single backslash in the string:
var str = "\\I have one backslash";
The following regular expression will match a single backslash (not two); again, the first one you see in the literal is an escape character starting an escape sequence. The \\ escape sequence tells the parser to put a single backslash character in the regular expression pattern:
var rex = /\\/;
If you're using a string to create a regular expression (rather than using a regular expression literal as I did above), note that you're dealing with two levels: The string level, and the regular expression level. So to create a regular expression using a string that matches a single backslash, you end up using four:
// Matches *one* backslash
var rex = new RegExp("\\\\");
That's because first, you're writing a string literal, but you want to actually put backslashes in the resulting string, so you do that with \\ for each one backslash you want. But your regex also requires two \\ for every one real backslash you want, and so it needs to see two backslashes in the string. Hence, a total of four. This is one of the reasons I avoid using new RegExp(string) whenver I can; I get confused easily. :-)
ES2015 and ES2018 update
Fast-forward to 2015, and as Dolphin_Wood points out the new ES2015 standard gives us template literals, tag functions, and the String.raw function:
// Yes, this unlikely-looking syntax is actually valid ES2015
let str = String.raw`\apple`;
str ends up having the characters \, a, p, p, l, and e in it. Just be careful there are no ${ in your template literal, since ${ starts a substitution in a template literal. E.g.:
let foo = "bar";
let str = String.raw`\apple${foo}`;
...ends up being \applebar.
Try String.raw method:
str = String.raw`\apple` // "\apple"
Reference here: String.raw()
\ is an escape character, when followed by a non-special character it doesn't become a literal \. Instead, you have to double it \\.
console.log("\apple"); //-> "apple"
console.log("\\apple"); //-> "\apple"
There is no way to get the original, raw string definition or create a literal string without escape characters.
please try the below one it works for me and I'm getting the output with backslash
String sss="dfsdf\\dfds";
System.out.println(sss);

Smalltalk, newline character

Does anybody know what's the newline delimiter for a string in smalltalk?
I'm trying to split a string in separate lines, but I cannot figure out what's the newline character in smalltalk.
ie.
string := 'smalltalk is
a lot of fun.
ok, it's not.'
I need to split it in:
line1: smalltalk is
line2: a lot of fun.
line3: ok, it's not.
I can split a line based on any letter or symbol, but I can't figure out what the newline delimter is.
OK here is how I'm splitting the string based on commas, but I cannot do it based on a new line.
The newline delimiter is typically the carriage return, i.e., Character cr, or as others mentioned, in a string, String cr. If you wanted to support all standard newline formats, just include both standard delimiters, for example:
string := 'smalltalk is
a lot of fun.'.
string findTokens: String cr, String lf.
Since you now mention you're using VisualWorks, the above won't work unless you have the "squeak-accessing" category loaded (which you probably won't unless you're using Seaside). You could use a regular expression match instead:
'foo
bar' allRegexMatches: '[^', (String with: Character cr), ']+'
A quick solution (I don't know if it is the better) is:
|array |
array := mystring findTokens: String cr
Where String cr is the carriage return character
As noted in this question: Character cr.
You can send the String>>withCRs message then delimit the carriage returns with backslashes, thus--
string := 'smalltalk is\
a lot of fun.\
ok, it's not.' withCRs.
It is of course depending on the encoding. Could be cr, lf or crlf. For unicode there are a few extra possibilities. See: pharo linesDo:

what characters should be escaped in sql string parameters

I need a complete list of characters that should be escaped in sql string parameters to prevent exceptions. I assume that I need to replace all the offending characters with the escaped version before I pass it to my ObjectDataSource filter parameter.
No, the ObjectDataSource will handle all the escaping for you. Any parametrized query will also require no escaping.
As others have pointed out, in 99% of the cases where someone thinks they need to ask this question, they are doing it wrong. Parameterization is the way to go. If you really need to escape yourself, try to find out if your DB access library offers a function for this (for example, MySQL has mysql_real_escape_string).
SQL Books online:
Search for String Literals:
String Literals
A string literal consists of zero or more characters surrounded by quotation marks. If a string contains quotation marks, these must be escaped in order for the expression to parse. Any two-byte character except \x0000 is permitted in a string, because the \x0000 character is the null terminator of a string.
Strings can include other characters that require an escape sequence. The following table lists escape sequences for string literals.
\a
Alert
\b
Backspace
\f
Form feed
\n
New line
\r
Carriage return
\t
Horizontal tab
\v
Vertical tab
\"
Quotation mark
\
Backslash
\xhhhh
Unicode character in hexadecimal notation
Here's a way I used to get rid of apostrophes. You could do the same thing with other offending characters that you run into. (example in VB.Net)
Dim companyFilter = Trim(Me.ddCompany.SelectedValue)
If (Me.ddCompany.SelectedIndex > 0) Then
filterString += String.Format("LegalName like '{0}'", companyFilter.Replace("'", "''"))
End If
Me.objectDataSource.FilterExpression = filterString
Me.displayGrid.DataBind()