Hello, everybody!
The script language Kinx is published with the concept of Looks like JavaScript, Feels like Ruby, Stable like AC/DC(?).
This time it is String.
- Reference
- First motivation ... The post of introduction
- Kinx, I wanted a scripting language with a syntax of C family.
- Repository ... https://github.com/Kray-G/kinx
- I am waiting for pull requests.
- First motivation ... The post of introduction
Basic interfaces for string operations are being defined in a special object named as String
. The method bound to the String
object is treated as a special method and can be directly applied to the string object.
String
special object
An object with special methods are called a special object, and there are String
, Array
, Integer
, Double
, Binary
. Special methods can be applied directly to the object (character string for String
) targeted by the special object.
For example, let's define the function as follows.
String.greeting = function(name) {
System.println("Hello, I am %{name}.");
};
Then you can write as below.
"John".greeting();
Let's run it.
Hello, I am John.
It is convenient depending on the usage. However, it is being also used by a standard library (adding a built-in special method), so it is better not to abuse it too much.
String
Built-in special methods
Method | Meaning |
---|---|
String.startsWith(str) | true if the string starts with str . |
String.endsWith(str) | true if the string ends with str . |
String.find(str) | Returns the position (0 ~) where str was found in the string. Returns -1 if not found. |
String.subString(str, start[, len]) | Returns the substring of str . |
String.replace(str, cond, repl) | Replaces all parts of str that match cond with repl . cond can be a string or regular expression object. |
String.toInt(str) | Converts str into an integer value. |
String.toDouble(str) | Converts str into a real number. |
String.parentPath(str) | Recognizes str as a path and returns the substring that is the parent path. Example) "ab/cd/ef.x".parentPath() -> "ab/cd.x"
|
String.filename(str) | Recognizes str as a path and returns the file name part string with the parent path part deleted. Example) "ab/cd/ef.x".filename() -> "ef.x"
|
String.stem(str) | Recognizes str as a path and returns the stem substring of the file name. Example) "ab/cd/ef.x".stem() -> "ef"
|
String.extnsion(str) | Recognizes str as a path and returns the extension substring of the file name. Example) "ab/cd/ef.x".extnsion() -> ".x"
|
String.split(str, sep) | Splits str with sep as the delimiter and returns it as an array. sep can be a character string or a regular expression object can be specified. |
String.each(str, callback) | Split str character by character and call the callback function with it as an argument. The index (0~) is also passed as the second argument of the callback function. |
Special operator
/
operator
When the /
operator is applied to a character string, the character string concatenated by /
is returned. In that case, there will be only one duplicate separator.
var a = "aa/bb" / "ccdd"; // => "aa/bb/ccdd"
var b = "aa/bb/" / "ccdd"; // => "aa/bb/ccdd"
var c = "aa/bb" / "/ccdd"; // => "aa/bb/ccdd"
=~
operator
When you apply =~
to a string, expect a regular expression object for the rvalue. If it is not a regular expression object, an exception is thrown.
!~
operator
If you apply !~
to a string, expect a regular expression object for the rvalue. If it is not a regular expression object, an exception is thrown.
Postfix []
operator
When a character string is accessed by index, the character code at that position is returned as an integer value. Therefore, to judge that "the 5th character is 'a'
", write as follows.
if (str[5] =='a'[0]) {
/* ... */
}
Note that it is a little different from C. The character'a' is not a character but a string literal, so we need [0]
to indicate the first character.
unary *
operator
If you apply the unary *
operator to a string, convert the string to an array.
var a = *"abc"; // => [97, 98, 99]
As a side note, applying the unary *
operator to an array returns it to a string.
Conclusion
It's a common sense to use a scripting language to make string operations easier. In the first place, Perl, which is the original of this type of script, was used because it is easy to process text. In that sense, string manipulation is a primitive but important function. Text processing is one of the programs I don't want to write in C.
See you next time.
Top comments (0)