From WikiChip
Editing mirc/string manipulation
Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.
The edit can be undone.
Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
This page supports semantic in-text annotations (e.g. "[[Is specified as::World Heritage Site]]") to build structured and queryable content provided by Semantic MediaWiki. For a comprehensive description on how to use annotations or the #ask parser function, please have a look at the getting started, in-text annotation, or inline queries help pages.
Latest revision | Your text | ||
Line 3: | Line 3: | ||
== Basic String Operations == | == Basic String Operations == | ||
+ | |||
Since everything is a string in mSL, just assigning it to a variable is enough. | Since everything is a string in mSL, just assigning it to a variable is enough. | ||
<syntaxhighlight lang="mIRC">var %x = This is an example string.</syntaxhighlight> | <syntaxhighlight lang="mIRC">var %x = This is an example string.</syntaxhighlight> | ||
− | We often want to get the length of such string. The | + | We often want to get the length of such string. The $len identifier can be used to get that. |
− | <syntaxhighlight lang="mIRC"> | + | <syntaxhighlight lang="mIRC">echo -a $len(apples and oranges) |
− | 18</syntaxhighlight> | + | ;18</syntaxhighlight> |
− | It is often desired to join two strings together. Such operation is called a '''string concatenation'''. The | + | It is often desired to join two strings together. Such operation is called a '''string concatenation'''. The $+ operator can be used to concatenate two string together. For example: |
− | <syntaxhighlight lang="mIRC"> | + | <syntaxhighlight lang="mIRC">echo -a A $+ B |
− | AB</syntaxhighlight> | + | ;AB</syntaxhighlight> |
It's important to note that both identifiers and variables can be substituted instead of A and B. For example: | It's important to note that both identifiers and variables can be substituted instead of A and B. For example: | ||
<syntaxhighlight lang="mIRC">alias hello { | <syntaxhighlight lang="mIRC">alias hello { | ||
− | var %x = Hello World | + | var %x = Hello, World |
var %x = %x $+ ! | var %x = %x $+ ! | ||
echo -a %x | echo -a %x | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
− | The output after executing the above code (/hello) is "Hello World!" | + | The output after executing the above code (/hello) is "Hello, World!" |
== Substrings == | == Substrings == | ||
+ | |||
A substring is a string that is part of a longer string. There are a number of built-in identifiers that can be used to retrieve a smaller portion of the original string. | A substring is a string that is part of a longer string. There are a number of built-in identifiers that can be used to retrieve a smaller portion of the original string. | ||
=== $left() and $right() === | === $left() and $right() === | ||
+ | |||
The first two identifiers you should be familiar with are the {{mIRC|$left}} and {{mIRC|$right}} identifiers which can be used to return the left or right most part of the original string respectably. Their syntax is: | The first two identifiers you should be familiar with are the {{mIRC|$left}} and {{mIRC|$right}} identifiers which can be used to return the left or right most part of the original string respectably. Their syntax is: | ||
Line 44: | Line 47: | ||
;Right: abc|defg|</syntaxhighlight> | ;Right: abc|defg|</syntaxhighlight> | ||
− | If the length specified is a negative number, | + | If the length specified is a negative number, $left and $right return the entire text minus that many characters from their respective sides. For example: |
<syntaxhighlight lang="mIRC">//echo -a $left(abcdefg, -4) $right(abcdefg, -4) | <syntaxhighlight lang="mIRC">//echo -a $left(abcdefg, -4) $right(abcdefg, -4) | ||
Line 59: | Line 62: | ||
=== $mid() === | === $mid() === | ||
− | + | ||
+ | [[$left identifier - mIRC|$left()]] and [[$right identifier - mIRC|$right()]] are great but they can get a little complicated if you want to get a substring from the middle of the string. For such cases, the $mid() identifier is a more powerful alternative. [[$mid identifier - mIRC|$mid()]] has the following syntax: | ||
<syntaxhighlight lang="mIRC">$mid(<string>, <start>) | <syntaxhighlight lang="mIRC">$mid(<string>, <start>) | ||
Line 74: | Line 78: | ||
== Case Transformation == | == Case Transformation == | ||
− | The | + | |
+ | The $islower and $isupper identifiers can be used to determine if a string is entirely made up of uppercase or lowercase letters ({{mIRC|$true}}), or else they return {{mIRC|$false}}. The same functionality is also built into an if statement using the isupper and islower operators. The $upper and $lower identifiers perform case conversion on an entire string or a string character. | ||
<syntaxhighlight lang="mIRC">alias case_example { | <syntaxhighlight lang="mIRC">alias case_example { | ||
Line 92: | Line 97: | ||
=== $lower() and $upper() === | === $lower() and $upper() === | ||
− | The | + | |
+ | The $lower() and $upper() identifiers can be used to transform the entire string into uppercase or lowercase letters. For example: | ||
<syntaxhighlight lang="mIRC">//echo -a $upper(HeLlO tHeRe) | <syntaxhighlight lang="mIRC">//echo -a $upper(HeLlO tHeRe) | ||
Line 103: | Line 109: | ||
== Searching == | == Searching == | ||
+ | |||
There are a number of identifiers that can be used to search for a substring within a string. The first one is the $pos identifier which has the following syntax: | There are a number of identifiers that can be used to search for a substring within a string. The first one is the $pos identifier which has the following syntax: | ||
Line 108: | Line 115: | ||
$pos(<string>, <substring>, <occurrence>)</syntaxhighlight> | $pos(<string>, <substring>, <occurrence>)</syntaxhighlight> | ||
− | The first variation returns the position of the first instance of the substring. If the substring is found multiple times within the string, you can specify the Nth occurrence you want. If you specify 0 for the occurrence, | + | The first variation returns the position of the first instance of the substring. If the substring is found multiple times within the string, you can specify the Nth occurrence you want. If you specify 0 for the occurrence, $pos will return the total number of substring found within the string. |
− | '''Note:''' | + | '''Note:''' $poscs is a case-sensitive version of $pos; it has the same syntax. |
If you simply want to count the number of occurrences a list of substring is found in the string, you can use the $count identifier instead. It's syntax is as follows: | If you simply want to count the number of occurrences a list of substring is found in the string, you can use the $count identifier instead. It's syntax is as follows: | ||
Line 122: | Line 129: | ||
Which will print "2". | Which will print "2". | ||
− | '''Note:''' | + | '''Note:''' $countcs is a case-sensitive version of $count; it has the same syntax. |
== Substring Replacement and Removal == | == Substring Replacement and Removal == | ||
=== Replacement === | === Replacement === | ||
− | |||
− | |||
− | |||
− | The syntax for both of them is: | + | There are two built-in string replacement identifiers, $replace and $replacex. The major difference between the two is that the later once will not apply replacement to any of the replaced strings. The syntax for both of them is: |
<syntaxhighlight lang="mIRC">$replace(<string>, <substring>, <replacement>[, <substring2>, <replacement2>, ...]) | <syntaxhighlight lang="mIRC">$replace(<string>, <substring>, <replacement>[, <substring2>, <replacement2>, ...]) | ||
Line 155: | Line 159: | ||
2 3 4 5</pre> | 2 3 4 5</pre> | ||
− | + | '''Note:''' $replacecs/$replacexcs are case-sensitive versions of $replace/$replacex; it has the same syntax. | |
− | + | === Substring Removal === | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
$remove is an identifier that can remove all occurrences of the substrings from the string. The syntax is: | $remove is an identifier that can remove all occurrences of the substrings from the string. The syntax is: | ||
Line 181: | Line 177: | ||
== Miscellaneous Identifiers == | == Miscellaneous Identifiers == | ||
− | Two more identifiers you should be aware of are | + | Two more identifiers you should be aware of are $str() and $strip(). |
=== $str() === | === $str() === | ||
+ | |||
$str returns the same exact string repeated N amount of times. The syntax is: | $str returns the same exact string repeated N amount of times. The syntax is: | ||
Line 197: | Line 194: | ||
=== $strip() === | === $strip() === | ||
+ | |||
The $strip identifier can remove control codes from a string. The syntax for it is: | The $strip identifier can remove control codes from a string. The syntax for it is: | ||
Line 211: | Line 209: | ||
* m = use messages option settings | * m = use messages option settings | ||
− | + | [[Category:mIRC]] | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | [[Category:mIRC |