Literals/String: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(5 intermediate revisions by 3 users not shown)
Line 652:
? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }
# value: ["abc", "def"]</syntaxhighlight>
 
=={{header|EasyLang}}==
Strings are always enclosed in double quotes ("). Unicode is also supported.
<syntaxhighlight lang="easylang">
print "EasyLang"
print "简"
</syntaxhighlight>
 
=={{header|Ela}}==
Line 2,155 ⟶ 2,162:
'101100'b /* a bit string, stored as one bit per digit. */
</syntaxhighlight>
 
=={{header|Plain English}}==
A string literal is surrounded by double quotes. Plain English does not make a distinction between string and character literals.
 
To escape a double quote inside a string literal, use two double quotes.
<syntaxhighlight lang="text">"a ""string"" literal"</syntaxhighlight>
 
=={{header|plainTeX}}==
Line 2,533 ⟶ 2,546:
This will not interpolate: #{a}
NON_INTERPOLATING</syntaxhighlight>
 
=={{header|Rust}}==
 
A <code>char</code> in Rust is a Unicode scalar value. A char type in Rust is always four bytes in size and can be denoted by single quotes:
 
<syntaxhighlight lang="rust">
let char01: char = 'a';
let char02: char = '\u{25A0}'; // Black square
let char03: char = '❤'; // Heart
</syntaxhighlight>
 
Rust has two common string types: <code>&str</code> and <code>String</code>. These different string types are used depending if it's a fixed string literal that is saved into the executable and lives throughout the program execution (usually borrowed as a <code>&str</code>), a string slice that references a contiguous sequence of elements (borrowed as a <code>&str</code>), or a <code>String</code> which allows for a growable heap allocated valid UTF-8 string. Only the <code>String</code> type is fully owned by its variable, and the other two are only interacted through the reference <code>&str</code>. The <code>&str</code> string slice type may point to a string literal or a heap-allocated string.
 
The String type in Rust is intended to always contain a valid UTF-8 string. UTF-8 is a "variable width" encoding, and therefore Strings are going to be typically smaller than an array of the same Rust <code>char</code>'s. For example, the String "hi" is all within ASCII, and therefore a UTF-8 String of "hi" represents each character as one byte each. On the other hand, a char array ['h', 'i'] would take up 8 bytes in total. A string is denoted by double quotes:
 
<syntaxhighlight lang="rust">
const string_literal_str1: &str = "Hi Rust!";
 
let string_slice_str1: &str = string_literal_str1; // Creating a string slice from a string literal
let string_slice_str2: &str = "hello str"; // String slice pointing to string literal "hello str"
 
let string1: String = String::new(); // Empty String
let string2: String = String::from("hello"); // Creating String from string literal "hello"
let string3: String = "hi".to_string();
let string4: String = "bye".to_owned();
let string5: String = "see you soon".into();
// The "to_string()", "to_owned" or "into" are all equivalent in the code above.
// The "to_string()", "to_owned" or "into" methods are needed so that a string slice (&str) or a string literal (&str) is explicitly converted into a heap-allocated fully-owned String type. Otherwise the compiler's type checker will complain "expected struct `String`, found `&str` (string slice)"
 
let string6: String = string_slice_str2.to_owned(); // Explictly converting the string_slice_str2 into a heap-allocated fully-owned String. This can be done with "to_string()", "to_owned" or "into".
 
// String slices can also point to heap allocated strings:
let string_slice_str3: &str = &string2; // Creating a string slice to a heap-allocated String.
let string7: String = string_slice_str3.to_string(); // Converting string_slice_str3 into a heap-allocated fully-owned String copy, resulting in a new independent owned string copy of the original String. This can be done with "to_string()", "to_owned" or "into".
</syntaxhighlight>
 
Rust supports verbatim strings and here-strings by putting <code>r#</code> before the first double quotes, and the end is marked by adding a <code>#</code> after the final double quotes. If there is a <code>"#</code> inside the contents of your here-string or your verbatim string then you can just add more <code>#</code>'s as required in both the beggining and the end:
<syntaxhighlight lang="rust">
let verbatim_here_string01: &str = r#"A \verbatim string\, line breaks in programming use \n and tabs \t"#;
let verbatim_here_string02: &str = r#"
A \multi-line\ string, in programming
line breaks use the characters \n
and for tabs we use the characters \t
"#;
let verbatim_here_string03: &str = r##"
Part number "#001": 1
Part number "#002": 2
Part number "#003": 3
"##;
</syntaxhighlight>
 
To expand variables in Rust we have 3 options: we can use the "format!" macro, the "print!" macro or the "println!" macro.
<syntaxhighlight lang="rust">
let number: i32 = 42;
let number_string: String = format!("Number: {}", number);
println!("The result in string form is '{}'.", number_string);
print!("The number is {}. ", number); // Print without line break
println!("Again, it's {}", number); // Print with line break
// The above prints:
// The result in string form is 'Number: 42'.
// The number is 42. Again, it's 42
</syntaxhighlight>
 
In Rust, there are other string types that are specialized to specific string requirements, such as working with system strings (OsString and OsStr), working with C strings (CString and CStr), and working with system paths (Path and PathBuf).
 
=={{header|S-lang}}==
Line 3,042 ⟶ 3,119:
<syntaxhighlight lang="vbnet">Dim s = "Tom said, ""The fox ran away."""
Result: Tom said, "The fox ran away."</syntaxhighlight>
 
=={{header|V (Vlang)}}==
 
Character literals for Unicode characters, "rune literals", are an alias for u32.
 
Character literals for UTF-8 characters, "string literals", are an alias for u8.
 
To denote rune, Unicode characters, ` (backticks) are used :
 
A rune can be converted to a UTF-8 string by using the .str() method.
 
rocket := `🚀`
 
rocket.str() == '🚀' // uses single quotes, not the backtick, after conversion
 
A string can be converted back to runes by the .runes() method.
 
hello := 'Hello World'
 
hello_runes := hello.runes() // [`H`, `e`, `l`, `l`, `o`, ` `, `W`, `o`, `r`, `l`, `d`]
 
In V, a string is an immutable array of read-only bytes. All Unicode characters are encoded using UTF-8:
 
mut s := 'hello 🌎'
 
s[0] = `H` // not allowed as immutable
 
// convert `string` to `[]u8`
 
s := 'hello 🌎'
 
arr := s.bytes()
 
assert arr.len == 10
 
// convert `[]u8` to `string`
 
s2 := arr.bytestr()
 
assert s2 == s
 
// indexing gives a byte, u8(66) == `B`
 
name := 'Bob'
 
println(name.len == 3) // will print 3
 
if name[0] == u8(66) {println(name[0].ascii_str())} // will print`B`
 
String literals are contained in quotes:
 
str:= "Hello, world!"
 
=={{header|WEB}}==
Line 3,094 ⟶ 3,223:
 
From v0.4.0 Wren also supports ''raw'' string literals. These are any text surrounded by triple double quotes, """, and are interpreted verbatim i.e. any control codes and/or interpolations are not processed as such. They can include single or double double quotes without problem.
<syntaxhighlight lang="ecmascriptwren">var s = "abc123"
var t = "abc\t123\%"
var u = "\U0001F64A\U0001F680"
9,476

edits