splitString: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
m (added note how to split string at linebreak by adding endl to the delimiters)
m (Text replacement - "<sqf>([^↵][^<]*↵[^<]*)<\/sqf>" to "<sqf> $1 </sqf>")
 
(65 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{Command|= Comments
{{RV|type=command
____________________________________________________________________________________________


| arma3|= Game name
|game1= arma3
|version1= 1.50


|1.50|= Game version
|gr1= Strings
____________________________________________________________________________________________


| An SQF version of C++ [http://www.cplusplus.com/reference/cstring/strtok/ strtok]. Splits given string ''str'' into an array of tokens according to given ''delimiters''. In addition, if empty string "" is used for ''delimiters'', ''str'' is split by each character.|DESCRIPTION=
|descr= Splits the provided '''ANSI''' string into an array of tokens according to given delimiters.
____________________________________________________________________________________________
{{Feature|informative|For Unicode support, see [[forceUnicode]].}}


| str '''splitString''' delimiters |SYNTAX=
|s1= str [[splitString]] delimiters


|p1= str: [[String]] - A string to split |PARAMETER1=
|p1= str: [[String]] - the string to split
|p2= delimiters: [[String]] - one or more characters, each used as a separate delimiter. |PARAMETER2=
| [[Array]] |RETURNVALUE=
____________________________________________________________________________________________


|x1= <code>_str = "- This, is a sample string." [[splitString]] "-,. "; // ["This","is","a","sample","string"]
|p2= delimiters: [[String]] - one or more characters, each used as a separate delimiter; if empty, ''str'' is split by each character
_str [[joinString]] " "; // "This is a sample string"</code>|EXAMPLE1=


|x2= <code>"\A3\ui_f\data\map\vehicleicons\iconLogic_ca.paa" [[splitString]] "\.";
|r1= [[Array]]
// ["A3","ui_f","data","map","vehicleicons","iconLogic_ca","paa"]</code>|EXAMPLE2=


|x3= <code>"1:2:3" [[splitString]] ":"; // ["1","2","3"]</code>|EXAMPLE3=
|x1= <sqf>
_str = "- This, is a sample string." splitString "-,. "; // ["This","is","a","sample","string"]
_str joinString "#"; // "This#is#a#sample#string"
</sqf>


|x4= <code>["test","test"] [[joinString]] [[toString]] [12345] [[splitString]] [[toString]] [12345]; // ["test","test"]</code>|EXAMPLE4=
|x2= <sqf>
"\A3\ui_f\data\map\vehicleicons\iconLogic_ca.paa" splitString "\."; // ["A3","ui_f","data","map","vehicleicons","iconLogic_ca","paa"]
</sqf>


|x5= <code>"Japa is the best!" [[splitString]] "" [[joinString]] " "; // "J a p a  i s  t h e  b e s t !"</code>|EXAMPLE5=
|x3= <sqf>"1:2:3" splitString ":"; // ["1","2","3"]</sqf>


|x6= <code>_cmd = [[currentMagazineDetail]] [[player]]; //"9mm 16Rnd Mag(13/16)[id/cr:10000011/0]"
|x4= <sqf>"Japa is the best!" splitString "" joinString " "; // "J a p a  i s  t h e  b e s t !"</sqf>
_cmd [[splitString]] "([ ]/:)"; //["9mm","16Rnd","Mag","13","16","id","cr","10000011","0"]</code>|EXAMPLE6=


|x7= Remove all \r\n from file:<code>[[loadFile]] "somefile.txt" [[splitString]] [[toString]] [13,10] [[joinString]] " "</code>|EXAMPLE7=
|x5= <sqf>
____________________________________________________________________________________________
_cmd = currentMagazineDetail player; // "9mm 16Rnd Mag(13/16)[id/cr:10000011/0]"
_cmd splitString "([ ]/:)"; // ["9mm","16Rnd","Mag","13","16","id","cr","10000011","0"]
</sqf>


| [[joinString]], [[set]], [[resize]], [[reverse]], [[select]], [[in]], [[find]], [[toArray]], [[forEach]], [[count]], [[deleteAt]], [[deleteRange]], [[append]], [[sort]], [[param]], [[params]], [[arrayIntersect]], [[toString]], [[endl]], [http://www.utf8-chartable.de/ Unicode Character Table] |SEEALSO=
|x7= Remove all \r\n from file:
<sqf>loadFile "somefile.txt" splitString toString [13,10] joinString " ";</sqf>


|seealso= [[joinString]] [[set]] [[resize]] [[reverse]] [[select]] [[in]] [[find]] [[toArray]] [[forEach]] [[count]] [[deleteAt]] [[deleteRange]] [[append]] [[sort]] [[param]] [[params]] [[arrayIntersect]] [[toString]] [[endl]] [[forceUnicode]], {{Link|http://www.utf8-chartable.de/|Unicode Character Table}} {{Link|http://www.cplusplus.com/reference/cstring/strtok/|strtok (C++)}}
}}
}}


<h3 style="display:none">Notes</h3>
{{Note
<dl class="command_description">
|user= BaerMitUmlaut
|timestamp= 20160517141800
|text= This command is unreliable/broken when it comes to some non-ASCII characters (as of {{arma3}} v1.58):
<sqf>
"Mörser" splitString "" // -> ["M"," "," ","r","s","e","r"]
"Mörser" splitString "ö" // -> ["M","rser"]
"Mörser" splitString "ä" // -> ["M"," rser"]
</sqf>
}}


</dl>
{{Note
 
|user= Killzone_Kid
<h3 style="display:none">Bottom Section</h3>
|timestamp= 20161017140300
[[Category:Scripting Commands|{{uc:{{PAGENAME}}}}]]
|text= This commands works only with ASCII chars, any chars with codes > 127, also used in different languages, will be converted to [" ", " "]. Use [[toArray]] to preserve special characters:
[[Category:Scripting Commands Arma 3|{{uc:{{PAGENAME}}}}]]
<sqf>_spaced = toArray "Mörser" apply {toString [_x]} joinString " "; // M ö r s e r</sqf>
 
}}
<!-- CONTINUE Notes -->
<dl class="command_description">
<dd class="notedate">Posted on May 17, 2016 - 14:18 (UTC)</dd>
<dt class="note">[[User:BaerMitUmlaut|BaerMitUmlaut]]</dt>
<dd class="note">
This command is unreliable/broken when it comes to some non-ASCII characters (as of Arma 3 1.58):
<code>"Mörser" splitString ""
-> ["M"," "," ","r","s","e","r"]
"Mörser" splitString "ö"
-> ["M","rser"]
"Mörser" splitString "ä"
-> ["M"," rser"]</code>
</dd>
</dl>
<!-- DISCONTINUE Notes -->


<!-- CONTINUE Notes -->
{{Note
<dl class="command_description">
|user= R3vo
<dd class="notedate">Posted on October 17, 2016 - 14:03 (UTC)</dd>
|timestamp= 20190719111700
<dt class="note">[[User:Killzone Kid|Killzone Kid]]</dt>
|text= When working with string which contain linebreaks...
<dd class="note"> This commands works only with ASCII chars, any chars with codes > 127, also used in different languages, will be converted to [" ", " "]. Use [[toArray]] to preserve special characters:
<sqf>
<code>_spaced = [[toArray]] "Mörser" [[apply]] {[[toString]] [_x]} [[joinString]] " "; // M ö r s e r</code>
"stringWith" + endl + "Linebreak" splitString ",; ";
</dd>
/*
</dl>
result is ["someStringWith
<!-- DISCONTINUE Notes -->
Linebreak"]
 
*/
<!-- CONTINUE Notes -->
</sqf>
<dl class="command_description">
<dd class="notedate">Posted on July 19, 2019 - 11:17 (UTC)</dd>
<dt class="note">[[User:R3vo|R3vo]]</dt>
<dd class="note">
When working with string which contain linebreaks...
<code>"stringWith" + [[endl]] + "Linebreak" [[splitString]] ",; "; //Result ["someStringWith
Linebreak"]</code>


...one can remove those by adding [[endl]] as delimiter
...one can remove those by adding [[endl]] as delimiter
<code>"stringWith" + [[endl]] + "Linebreak" [[splitString]] ",; " + [[endl]] //Result ["someStringWith","Linebreak"]</code>
<sqf>"stringWith" + endl + "Linebreak" splitString ",; " + endl; // result is ["someStringWith","Linebreak"]</sqf>
</dd>
}}
</dl>
<!-- DISCONTINUE Notes -->

Latest revision as of 19:42, 3 September 2024

Hover & click on the images for description

Description

Description:
Splits the provided ANSI string into an array of tokens according to given delimiters.
For Unicode support, see forceUnicode.
Groups:
Strings

Syntax

Syntax:
str splitString delimiters
Parameters:
str: String - the string to split
delimiters: String - one or more characters, each used as a separate delimiter; if empty, str is split by each character
Return Value:
Array

Examples

Example 1:
_str = "- This, is a sample string." splitString "-,. "; // ["This","is","a","sample","string"] _str joinString "#"; // "This#is#a#sample#string"
Example 2:
"\A3\ui_f\data\map\vehicleicons\iconLogic_ca.paa" splitString "\."; // ["A3","ui_f","data","map","vehicleicons","iconLogic_ca","paa"]
Example 3:
"1:2:3" splitString ":"; // ["1","2","3"]
Example 4:
"Japa is the best!" splitString "" joinString " "; // "J a p a i s t h e b e s t !"
Example 5:
_cmd = currentMagazineDetail player; // "9mm 16Rnd Mag(13/16)[id/cr:10000011/0]" _cmd splitString "([ ]/:)"; // ["9mm","16Rnd","Mag","13","16","id","cr","10000011","0"]
Example 7:
Remove all \r\n from file:
loadFile "somefile.txt" splitString toString [13,10] joinString " ";

Additional Information

See also:
joinString set resize reverse select in find toArray forEach count deleteAt deleteRange append sort param params arrayIntersect toString endl forceUnicodeUnicode Character Table strtok (C++)

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note
BaerMitUmlaut - c
Posted on May 17, 2016 - 14:18 (UTC)
This command is unreliable/broken when it comes to some non-ASCII characters (as of Arma 3 v1.58):
"Mörser" splitString "" // -> ["M"," "," ","r","s","e","r"] "Mörser" splitString "ö" // -> ["M","rser"] "Mörser" splitString "ä" // -> ["M"," rser"]
Killzone_Kid - c
Posted on Oct 17, 2016 - 14:03 (UTC)
This commands works only with ASCII chars, any chars with codes > 127, also used in different languages, will be converted to [" ", " "]. Use toArray to preserve special characters:
_spaced = toArray "Mörser" apply {toString [_x]} joinString " "; // M ö r s e r
R3vo - c
Posted on Jul 19, 2019 - 11:17 (UTC)
When working with string which contain linebreaks...
"stringWith" + endl + "Linebreak" splitString ",; "; /* result is ["someStringWith Linebreak"] */
...one can remove those by adding endl as delimiter
"stringWith" + endl + "Linebreak" splitString ",; " + endl; // result is ["someStringWith","Linebreak"]