Now, we're going to create an application that takes your name as a parameter, greets you, and displays the number of characters in your name.
Main.hx
class Main { public function new() { } public static function main() { var name = neko.Sys.args()[0]; trace("Hello " + name); trace("Your name is " + Std.string(name.length) + " characters long."); } }
Main.hx
and type the haxe main Main -neko characterCounter.n
command.neko characterCounter.n Benjamin
Main.hx:12: Hello Benjamin
Main.hx:13: Your name is 8 characters long.
The program read your name from the command line, greeted you by appending your name to "Hello", and displayed the number of characters that make up your name.
args
function of neko.Sys
. This function takes no parameters and returns an Array of String.As you can see, we can access an array's item by its index using the square-brackets notation. Here we are accessing the item at index 0 (arrays are 0-based, that means that the first item is always at index 0).
Then, we use the trace
function that you've already seen to display "Hello" followed by the name of the user. As you can see here, string concatenation is done by using the + operator. It is still quite important to note that there are classes such as StringBuf
that can be used to achieve the same behavior, if you are focused on performances.
You'll also notice that String
has a variable named length
that allows you to retrieve the number of characters in it.
By the way, haXe is typed, and here we are using Std.string
to convert the length of the string from Int
to String
. Std.string
can be used to convert any value to a String
. This is not mandatory here, as when using the +
operator, if one of the two values is not an Int
nor a Float
, the operator will return a String
. In this case, all operands will be considered as String
.
characterCounter.n
file by passing it an argument: Benjamin
.a. Windows
b. MacOSX
c. Linux
d. Android
e. iOS
a. haXe 1
b. haXe 2
c. haXe 3
d. haXe 4
a. static and named main
b. public and named first
c. public and static, and named first
a. println
b. print
c. trace
d. debug
18.225.254.192