This is the fifth day of my participation in the August Wen Challenge.More challenges in August
string
In any language, strings are the most basic type, such as String in Java. Strings are also supported in AviatorScript, as long as consecutive characters enclosed in single or double quotation marks are a complete string object, for example:
"hello world"
'hello world'
"a"
or'a'
The string can be printed directly through the println function
The length of a string can be obtained using the string.length function:
## examples/string.av
let a = "hello world";
println(a);
println(string.length(a));
Copy the code
Print:
hello world
11
Copy the code
String concatenation can be done with + signs (another operator overload) :
## examples/string.av
let a = "hello world";
let b = 'AviatorScript';
println(a);
println(string.length(a));
println(a + ',' + b + 5);
Copy the code
String concatenation a + ‘,’ + b + 5 contains the number 5 and the string ‘,’. Any type and string added together will be concatenated into a string, as Java rules allow. So the last line above will print Hello World,AviatorScript5.
Strings also contain other functions, such as intercepting the string substring, which are in the namespace string, as described in the library list.
escape
Also, like other languages, strings in AviatorScript support escaping characters when special characters are encountered. Like Java, a character can be escaped by ‘ ‘. For example, if we want to represent a string with single quotes, we need to use the escape character if we continue to use single quotes to represent a string:
## examples/escape_string.av println('Dennis's car'); println('AviatorScript is great.\r\nLet's try it! '); Special characters such as' \r ', '\n', '\t', etc. are also supported. In the example above we use the newline '\r\n', which will print:Copy the code
Dennis’s car AviatorScript is great. Let’s try it!
Of course, in the case of quotes, you can simply use double quotes to represent strings and avoid escaping:Copy the code
println(“Dennis ‘s car”);
String Interpolation can be added, for exampleCopy the code
let name = “aviator”; let s = “hello,” + name;
The concatenated string s is' Hello aviator '. The '+' addition is specially optimized for string concatenation, which is automatically converted internally to 'StringBuilder' for concatenation. But for more complex scenarios, the syntax for string concatenation is still too ugly and cumbersome, so since 5.1.0, AviatorScript has supported string interpolation, an example:Copy the code
examples/string_interpolation.av
let name = “aviator”; let a = 1; let b = 2; let s = “hello, #{name}, #{a} + #{b} = #{a + b}”; p(s);
Expressions enclosed in '#{}' will be evaluated automatically in the current context and then inserted into the final result string. The above example will print:Copy the code
hello, aviator, 1 + 2 = 3
AviatorScript has done a lot of internal optimization to make it faster to reuse Expression in compilation mode than to use additive concatenation strings. Boolean types are used to represent truth and false. they have only two values, 'true' and 'false', respectively. Comparison operations such as greater than and less than can produce booleans:Copy the code
examples/boolean.av
println(“3 > 1 is ” + (3 > 1)); println(“3 >= 1 is ” + (3 >= 1)); println(“3 >= 3 is ” + (3 >= 3)); println(“3 < 1 is ” + (3 < 1)); println(“3 <= 1 is ” + (3 <= 1)); println(“3 <= 3 is ” + (3 <= 3)); println(“3 == 1 is ” + (3 == 1)); println(“3 ! = 1 is ” + (3 ! = 1));
Output:Copy the code
3 > 1 is true 3 >= 1 is true 3 >= 3 is true 3 < 1 is false 3 <= 1 is false 3 <= 3 is true 3 == 1 is false 3 ! = 1 is true
All logical operators are demonstrated above: - '>' greater than - '>=' greater than or equal to <! ----> - '<' less than - '<=' less than or equal to <! ----> - == == == -! Boolean values can take logical and, logical or, and logical no operations, assuming that 'x' and 'y' return Boolean values: - 'x && y' represents the relationship between ** and **, true if x is true and y is true, false otherwise. - ` x | | y ` said * * or * * relations, x is true, or is true, y is the result is true, when both are false value to false results. <! -- -- -- -- > - `! X '** negates the ** operator, yielding false if x is true and true otherwise. ` && ` and ` | | ` support short-circuit rules * * * *, and - if ` x ` false values, ` x && y ` directly returns false, y is evaluated. - if ` ` x is true value, ` x | | y ` directly returns true, y is evaluated.Copy the code
examples/boolean.av
let result = false && println(‘not reachable’); println(result);
let result = true || println(‘not reachable’); println(result);
Println returns a nonboolean value. If a logical operation is performed, an error is reported. But because of the short circuit rule, println does not even execute, and therefore does not print 'not reachable'. The Boolean value of the ternary operator can be used as a judgment in ternary expressions and conditional statements to determine which branch code to execute. The ternary expression 'test? Stmt1: stmt2 ', if the result of 'test' is true, 'stmt1' is executed, otherwise 'stmt2' is executed, and the result is the result of 'stmt1' or 'stmt2' :Copy the code
examples/ternary.av
let a = 3; let b = 1;
let c = a > b ? println(“a > b”) : println(“a <= b”);
println(c);
Will print:Copy the code
a > b null “
As you can see, only println(“a > b”) is executed, and the false branch really isn’t. The return result is null for println.
Unlike Java, AviatorScript allows two branches to return incompatible result types:
## examples/ternary.av
let a = 3;
let b = 1;
let c = a > b ? "a > b" : 999;
println(c);
Copy the code
a > b ? “A > b” : 999 The two branches result in a string and a number, which is perfectly fine in AviatorScript, where the string A > b is returned.
Regular expression
Regular expressions in AviatorScript are first-class citizens and are supported as basic types. A regular expression enclosed in/is an instance of java.util.Pattern, such as /\d+/ for one or more numbers. If we want to match files ending in.av, the re can be written as /^.*.av$/, where. To escape the. Symbol in the suffix.
The only operations that regular expressions can participate in are the comparison operator and the regular match operator =~ :
## examples/regexp.av
let p = /^(.*).av$/
println(p == p); ## print true
println("regexp.av" =~ p); ##print true
println("$0=" + $0);
println("$1=" + $1);
Copy the code
We define a regular expression p that matches filenames ending in.av using the =~ operator, which has a string to the left and a regular expression to the right, and returns true if the match is successful, false otherwise.
If the match is successful, AviatorScript puts the matching groups in the re into $0, $1, $2… Where $0 represents the entire string to match, and $1 represents the first grouping, and so on. Here is the file name, the first grouping (.*) of the re enclosed in parentheses.
So it will print:
true
true
$0=regexp.av
$1=regexp
Copy the code
PUT_CAPTURING_GROUPS_INTO_ENV If you don’t want to automatically capture grouped variables that start with a dollar sign, you can disable this behavior by setting options. PUT_CAPTURING_GROUPS_INTO_ENV to false.