-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringOps.au3
More file actions
1360 lines (925 loc) · 33.7 KB
/
StringOps.au3
File metadata and controls
1360 lines (925 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
;~ Last Modified: 2020-07-10
Description: A collection of custom
string functions for AutoIt as mostly inspired
by the stringr package from R and string methods
from Ruby.
https://stringr.tidyverse.org/reference/index.html
https://ruby-doc.org/core-2.6/String.html
Notes: Unless otherwise noted, The "V" at the end of the
function name denotes that it applies to every element in a 1D array.
List of Functions:
1. StringReplaceV()
2. StringSplitV()
3. StringMidV()
4. StringLeftV()
5. StringRightV()
6. StringLenV()
7. StringConcatenateV() = Concatenate a string to another string.
1. StringPaste() = Shorthand for StringConcatenateV().
8. StringRegExpReplaceV() = Replace a string based on a regular expresion.
1. StringSub() = Replace a string based on a regular expression (shorthand of StringRegExpReplace()).
2. StringSubV() = Replace each array element with another string based on a regular expression (shorthand of StringRegExpReplaceV()).
3. StringRemove() = Removes a specified string from a larger string.
4. StringRemoveV() = Applies StringRemove() to each element in an array.
9. StringInStrV()
10. StringStripCRV()
11. StringStripWSV()
12. StringIsSpaceV()
13. ZeroFlag() = Specify a width to flag a 0 at the beginning of a string.
14. ZeroFlagV()
15. ZeroFlagSSN() = Flag zeros to fill a 9-digit string.
16. ZeroFlagSSNV()
17. StringJoin() = Combine all array elements into a single string.
18. StringDetect() = Detect whether a string meets a specified regular expression.
19. StringDetectV()
20. StringExtract() = Return array of string matches based on a regular expression.
21. StringExtractV()
22. StringDup() = Repeat a string a specified number of times.
1. StringDupV()
23. StringPos() = Return array of positions of a specified string pattern.
1. StringSubset() = Subset an array of strings based on a regular expression pattern.
24. StringChomp() = Remove all white space characters from a string.
1. StringChompV()
25. StringLocate() = List the starting and ending positions of a specified substring.
1. StringLocateV()
2. StringStart() = Output the starting position of a string. A simpler StringInStr().
3. StringStartV()
4. StringEnd() = Output the ending position of a string.
5. StringEndV()
26. StringRange() = Similar to StringMid, but the 3rd input is an ending position index.
27. StringTrim() = Removes leading and trailing white spaces.
1. StringTrimV()
2. StringTrim2WS() = Removes double or more spaces.
3. StringTrim2WSV()
28. StringPrefix() = Appends a prefix to a string.
1. StringPrefixV()
2. StringSuffix() = Appends a suffix to a string.
3. StringSuffixV()
29. StringClear() = Removes all characters from a string.
1. StringClearV()
30. StringSwitch() = For an array, replaces specified values with another set of specified values.
1. swap() = synonym for StringSwitch().
31. StringSwitchSub() = Replace specified values with another set of specified values based on a regular expression.
1. swapsub() = synonym for StringSwitchSub().
32. StringInsertV() = Insert a string at a specified position for each element in an array.
33. grep() = synonym for StringSubset()
1. grepl() = synonym for StringDetect()
2. greplv() = vectorization of grepl(); synonym for StringDetectV().
3. gsub() = synonym of StringSub()
4. gsubv() = synonym of StringSubV().
34. CombineWords() = Combine all elements of an array into a single string, inserting a conjunction before the last element.
35. StringAny() = check if any array element matches a string pattern.
36. StringTitleCase() = capitalize each word.
1. StringTitleCaseV() = capitalize each word for each array element.
#ce ---------------------------------------
; DEPENDENCIES
;#include ".\RS__Library.au3" ; Load this in a separate tab to test the functions below.
#include <StringConstants.au3>
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringReplaceV()
Description: A vectorization of StringReplace().
In other words, it applies StringReplace() for
every element in an array (for 1 dimension).
An attempted replication of gsub() from R.
#ce ---------------------------------------
Func StringReplaceV($array, $search, $replace) ; array, character(s) to search for, character(s) to replace the search query.
; For each element, search for the character(s) to replace and then replace it with the desired character(s).
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringReplace($array[$i], $search, $replace)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$a2 = StringReplaceV($my_array, "_", " ")
_ArrayDisplay($a2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringSplitV()
Description: A vectorization of StringSplit().
In other words, it applies StringSplit() for
every element in an array (for 1 dimension).
*WARNING: The output is an array of arrays.
#ce ---------------------------------------
Func StringSplitV($array, $split) ; array, character to split by
; For each element, search for the character(s) to split by.
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringSplit($array[$i], $split)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
#include ".\_NestedArrayDisplay.au3"
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$a2 = StringSplitV($my_array, "_")
_NestedArrayDisplay($a2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringMidV()
Description: A vectorization of StringMid().
In other words, it applies StringMid() for
every element in an array (for 1 dimension).
#ce ---------------------------------------
Func StringMidV($array, $start, $count = -1) ; array, character to split by
; For each element, search for the character(s) to split by.
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringMid($array[$i], $start, $count)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$a2 = StringMidV($my_array, 3, 5)
_ArrayDisplay($a2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringLeftV()
Description: A vectorization of StringLeft().
In other words, it applies StringLeft() for
every element in an array (for 1 dimension).
#ce ---------------------------------------
Func StringLeftV($array, $count) ; array, character to split by
; For each element, search for the character(s) to split by.
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringLeft($array[$i], $count)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$a2 = StringLeftV($my_array, 3)
_ArrayDisplay($a2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringRightV()
Description: A vectorization of StringRight().
In other words, it applies StringRight() for
every element in an array (for 1 dimension).
#ce ---------------------------------------
Func StringRightV($array, $count) ; array, character to split by
; For each element, search for the character(s) to split by.
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringRight($array[$i], $count)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$a2 = StringRightV($my_array, 3)
_ArrayDisplay($a2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-05
Function: StringLenV()
Description: A vectorization of StringLen().
In other words, it applies StringLen() for
every element in an array (for 1 dimension).
#ce ---------------------------------------
Func StringLenV($array) ; array, character to split by
; For each element, search for the character(s) to split by.
$x = Map(StringLen, $array); Map() is from RS_Functionals.au3. It applies a function to each element in an array.
Return $x ; always use the Return command to return a value when creating a function.
EndFunc
#cs -- BEGIN TEST
#include <Array.au3>
#include ".\RS_ArrayOps.au3" ; to load _ArrayColInsert2()
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
; Get the lengths of each element from $my_array.
$lens = StringLenV($my_array) ; from RS_StringOps.au3
; Have to create a new object because _ArrayColInsert2() is immutable (i.e., it does not auto-update the reference array).
$my_array2 = _ArrayColInsert2($my_array, 1, $lens)
_ArrayDisplay($my_array2)
#ce -- END TEST
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-08
Function: StringConcatenateV(), StringPaste()
Description: Concatenate elements for each element
in an array.
StringPaste() is a shorthand for StringConcatenateV().
#ce ---------------------------------------
Func StringConcatenateV($array, $concat)
For $i = 0 to UBound($array) - 1
$array[$i] = $array[$i] & $concat
Next
Return $array
EndFunc
Func StringPaste($array, $concat)
Return StringConcatenateV($array, $concat)
EndFunc
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date Created: 2020-06-08
Date Modified: 2020-06-14
Functions: StringRegExpReplaceV(),
StringSub(),
StringSubV(),
StringRemove(),
StringRemoveV()
Description: A vectorization of StringRegExpReplace().
StringSub() is a shorthand for the defaults of StringRegExpReplace().
StringSubV() is a shorthand for the StringRegExpReplaceV()
("generalized substitution").
StringRemove() removes a matched pattern from a string.
StringRemoveV() is a vectorization of StringRemove().
#ce ---------------------------------------
Func StringRegExpReplaceV($array, $search, $replace) ; array, character(s) to search for, character(s) to replace the search query.
; For each element, search for the character(s) to replace and then replace it with the desired character(s).
For $i = 0 to Ubound($array) - 1 ; AutoIt uses 0-based indexing, so the maximum index value is the number of elements minus 1.
$array[$i] = StringRegExpReplace($array[$i], $search, $replace)
Next
Return $array ; always use the Return command to return a value when creating a function.
EndFunc
Func StringSub($string, $search, $replace)
Return StringRegExpReplace($string, $search, $replace)
EndFunc
Func StringSubV($array, $search, $replace)
$x = StringRegExpReplaceV($array, $search, $replace)
Return $x
EndFunc
Func StringRemove($string, $string_to_remove)
Return StringSub($string, $string_to_remove, "")
EndFunc
Func StringRemoveV($array, $string_to_remove)
For $i = 0 to UBound($array) - 1
$array[$i] = StringRemove($array[$i], $string_to_remove)
Next
Return $array
EndFunc
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: StringInStrV()
Description: A vectorization of StringInStr().
As of 2020-06-09, the count parameter is
not used.
#ce ---------------------------------------
Func StringInStrV($array, $substring, $casesense = 0, $occurrence = 1, $start = 1)
For $i = 0 to Ubound($array) - 1
$array[$i] = StringInStr($array[$i], $substring, $casesense, $occurrence, $start)
Next
Return $array
EndFunc
#cs --
#include <Array.au3>
Local $my_array[3] = ["hi_robert", "hi_nathan", "hi_faith"]
$ssv_test = StringInStrV($my_array, "_")
_ArrayDisplay($ssv_test)
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: StringStripCRV()
Description: A vectorization of StringStripCR().
#ce ---------------------------------------
Func StringStripCRV($array)
$x = Map(StringStripCR, $array) ; Map() is from RS_Functionals.au3. It applies a function to each element in an array.
Return $x
EndFunc
#cs -- TEST
#include <Array.au3>
#include ".\RS_Functionals.au3"
Local $my_array[3] = ["hi" & Chr(13) & "robert", "hi" & Chr(13) & "nathan", "hi" & Chr(13) & "faith"]
$test = StringStripCRV($my_array)
_ArrayDisplay($test)
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: StringStripWSV()
Description: A vectorization of StringStripWS().
#ce ---------------------------------------
; $Str_STRIPALL is from StringConstants.au3
; https://www.autoitscript.com/autoit3/docs/functions/StringStripWS.htm
Func StringStripWSV($array, $flag = $STR_STRIPALL)
For $i = 0 to Ubound($array) - 1
$array[$i] = StringStripWS($array[$i], $flag)
Next
Return $array
EndFunc
#cs -- TEST
#include <Array.au3>
#include <StringConstants.au3>
Local $my_array[3] = ["hi robert", "hi nathan", "hi faith"]
$test = StringStripWSV($my_array)
_ArrayDisplay($test)
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: StringIsSpaceV()
Description: A vectorization of StringIsSpace().
#ce ---------------------------------------
Func StringIsSpaceV($array)
$x = Map(StringIsSpace, $array) ; Map() is from RS_Functionals.au3. It applies a function to each element in an array.
Return $x
EndFunc
#cs -- TEST
#include <Array.au3>
#include ".\RS_Functionals.au3"
Local $my_array[4] = [Chr(13), "", " ", "hi"]
$test = StringIsSpaceV($my_array)
_ArrayDisplay($test)
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: ZeroFlag()
Description: Flag a 0 at the beginning of
a string if a specified width is not met.
#ce ---------------------------------------
Func ZeroFlag($string, $width)
; Prefix a 0 to the string until a width of $width has been reached.
; https://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm
$x = StringFormat("%0" & $width & "s", $string)
Return $x
EndFunc
#cs -- TEST
$s = 700
MsgBox(1, 'ZeroFlag($s, 4) output', ZeroFlag($s, 4))
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: ZeroFlagV()
Description: A vectorization of ZeroFlag().
#ce ---------------------------------------
Func ZeroFlagV($array, $width)
For $i = 0 to UBound($array) - 1
; Prefix a 0 to the string until a width of $width has been reached.
; https://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm
$array[$i] = StringFormat("%0" & $width & "s", $array[$i])
Next
Return $array
EndFunc
#cs --
#include <Array.au3>
Local $test[3] = [700, 930, 1625]
_ArrayDisplay(ZeroFlagV($test, 4))
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: ZeroFlagSSN()
Description: Flag a 0 at the beginning of
a string until a width of 9 is reached.
#ce ---------------------------------------
Func ZeroFlagSSN($string)
; %09s --> prefix a 0 to the string until a width of 9 has been reached.
; https://www.autoitscript.com/autoit3/docs/functions/StringFormat.htm
$x = StringFormat("%09s", $string)
Return $x
EndFunc
#cs -- TEST
$s = 700
MsgBox(1, 'ZeroFlagSSN($s) output', ZeroFlagSSN($s))
#ce --
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-09
Function: ZeroFlagSSNV()
Description: A vectorization of ZeroFlagSSN().
#ce ---------------------------------------
Func ZeroFlagSSNV($array)
$x = Map(ZeroFlagSSN, $array) ; Map() is from RS_Functionals.au3. It applies a function to each element in an array.
Return $x
EndFunc
#cs -- TEST
#include <Array.au3>
#include "RS_Functionals.au3"
Local $test[3] = [700, 930, 1625]
_ArrayDisplay(ZeroFlagSSNV($test))
#ce
;=====================================================
#cs ---------------------------------------
Author: Robert Schnitman
Date Created: 2020-06-09
Date Modified: 2020-06-11
Function: StringJoin()
Description: Concatenates all elements of an
array into a single string.
Inspired by the Ruby programming language's
.join method.
#ce ---------------------------------------
Func StringJoin($array, $separator = "")
; initialize loop
$x = ""
; self-concatenate until the last element has been concatenated.
For $i in $array
$x &= $i & $separator
Next
; if the last character is a separator, remove it.
$last_char = StringRight($x, 1)
If $last_char = $separator Then
$y = StringLeft($x, StringLen($x) - 1) ; Take out final separator
Else
$y = $x
EndIf
; Output should be the concatenation of all the elements into a single string.
Return $y
EndFunc
#cs -- TEST
Local $strings[5] = ["hi", "mon ami", "robert", "nathan", "faith"]
$strings2 = StringJoin($strings, "|")
MsgBox(1, 'test', $strings2) ; output: "hi|mon ami|robert|nathan|faith"
#ce --
; =======
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-11
Functions: StringDetect(), StringDetectV()
Description: StringDetect() Detects whether there is a
regular pattern match and outputs True/False.
StringDetectV() is a vectorized version of
StringDetect(), intended for arrays.
#ce ---------------------------------------
Func StringDetect($string, $pattern)
If StringRegExp($string, $pattern) = 1 Then
$x = True
Else
$x = False
EndIf
Return $x
EndFunc
#cs -- TEST
$str = "Loan Report_ABC Test.xls"
$test_me = StringDetect($str, "ABC|CBA|XYZ")
MsgBox(1, "test", $test_me & ": " & $str & " is in 'ABC|CBA|XYZ'")
#ce --
Func StringDetectV($array, $pattern) ; $a is an array
For $i = 0 to UBound($array) - 1
$array[$i] = StringDetect($array[$i], $pattern)
Next
Return $array
EndFunc
#cs -- TEST
#include <Array.au3>
Local $stra[] = ["Loan Report_ABC Test.xls", "Loan Report_CBA Test.xls", "Loan Report_dont select me.xls"]
$test_me = StringDetectV($stra, "ABC|CBA|XYZ")
_ArrayDisplay($test_me)
#ce --
; ====
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-11
Function: StringExtract(), StringExtractV()
Description: StringExtract() extracts a
substring based on a pattern match.
StringExtractV() is a vectorization of
StringExtract. Can pull multiple matches
per row.
#ce ---------------------------------------
Func StringExtract($string, $pattern)
; 3 = returns array of global matches
; https://www.autoitscript.com/autoit3/docs/functions/StringRegExp.htm
Return StringRegExp($string, $pattern, 3)
EndFunc
Func StringExtractV($array, $pattern, $separator = ",")
For $i = 0 to UBound($array) - 1
$array[$i] = StringJoin(StringExtract($array[$i], $pattern), $separator)
Next
Return $array
EndFunc
#cs -- TEST
Local $stra[] = ["Loan Report_ABC Test.xls", "Loan Report_CBA Test.xls", "Loan Report_dont select me.xls"]
$stra_join = StringJoin($stra)
$search_query = "ABC|XYZ|CBA"
_ArrayDisplay(StringExtract($stra_join, $search_query))
;_ArrayDisplay($x)
#ce
; ====
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-12
Functions: StringDup(), StringDupV()
Description: StringDup() repeats a string
a specified number of times.
StringDupV() is a vectorization of
StringDup().
#ce ---------------------------------------
Func StringDup($string, $times)
Local $array[$times] = [0]
For $i = 0 to UBound($array) - 1
$array[$i] = $string
Next
Return StringJoin($array)
EndFunc
Func StringDupV($array, $times)
For $i = 0 to UBound($array) - 1
$array[$i] = StringDup($array[$i], $times)
Next
Return $array
EndFunc
#cs -- TEST
#include "I:\SAI\Auto-it Files\Include\RS__Library.au3"
$s = "robert"
$test = StringDup($s, 5)
MsgBox(1, 'test', $test)
#ce --
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringPos(), StringSubset()
Description: StringPos() finds specific indices
of an array based on a regular expression pattern.
StringSubset() calls StringPos() to return
the matching values.
#ce ---------------------------------------
Func StringPos($array, $search_pattern)
$indices = _ArrayFindAll($array, $search_pattern, Default, Default, Default, 3); 3 = regular expression pattern
Return $indices
EndFunc
; simpler version of _ArrayFilter() from RS_ArrayOps.au3.
Func StringSubset($array, $search_pattern)
; find the indices
$indices = StringPos($array, $search_pattern)
; initialize loop
Local $output[UBound($indices)] = [0]
; Insert into $indices the matching values from $a.
For $i = 0 to UBound($indices) - 1
$output[$i] = $array[$indices[$i]]
Next
; Return the array.
Return $output
EndFunc
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringChomp(), StringChompV()
Description: StringChomp() removes all spaces.
It is a shorthand for StringStripWS(string, 8).
StringChompV is a vectorization of StringChomp().
Inspired by Ruby's chomp method (https://ruby-doc.org/core-2.4.0/String.html#method-i-chomp).
#ce ---------------------------------------
Func StringChomp($string)
Return StringStripWS($string, 8)
EndFunc
Func StringChompV($array)
$output = Map(StringChomp, $array) ; Map() is from RS_Functionals.au3. It applies a function to each element in an array.
Return $output
EndFunc
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringLocate(),
StringLocateV()
StringStart(),
StringStartV()
StringEnd(),
StringEndV()
Description: Locate the start and end of a specific
string. These functions are case-sensitive.
#ce ---------------------------------------
; StringStart()
Func StringStart($string, $substring)
Return StringinStr($string, $substring, 1) ; case-sensitive
EndFunc
Func StringStartV($array, $substring)
For $i = 0 to UBound($array) - 1
$array[$i] = StringStart($array[$i], $substring)
Next
Return $array
EndFunc
; StringEnd()
Func StringEnd($string, $substring)
Return StringStart($string, $substring) + StringLen($substring) - 1 ; Need to discount position 0?
EndFunc
Func StringEndV($array, $substring)
For $i = 0 to UBound($array) - 1
$array[$i] = StringEnd($array[$i], $substring)
Next
Return $array
EndFunc
; StringLocate()
Func StringLocate($string, $substring)
$start = StringStart($string, $substring)
$end = StringEnd($string, $substring)
$out = $start & "," & $end
Return $out
EndFunc
Func StringLocateV($array, $substring)
For $i = 0 to UBound($array) - 1
$array[$i] = StringLocate($array[$i], $substring)
Next
Return $array
EndFunc
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringRange()
Description: Similar to StringMid(), but the 3rd input
is not a "count" but a position index.
#ce ---------------------------------------
Func StringRange($string, $start, $end)
Return StringMid($string, $start, $end - $start + 1)
EndFunc
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringTrim(),
StringTrimV(),
StringTrim2WS(),
StringTrim2WSV()
Description:
StringTrim() removes leading and trailing whitespaces from a string.
StringTrimV() applies StringTrim for each element in an array.
StringTrim2WS() removes double spaces from a string.
StrimgTrim2WSV() applies StringTrim2WS over an array.
#ce ---------------------------------------
Func StringTrim($string)
Return StringStripWS($string, 3) ; 3 = removes leading and trailing white space.
EndFunc
Func StringTrimV($array)
Return Map(StringTrim, $array) ; Map() is from RS_Functionals.au3. It applies a function to each element in an array.
EndFunc
Func StringTrim2WS($string)
Return StringStripWS($string, 4) ; 4 = removes double or more spaces between words
EndFunc
Func StringTrim2WSV($array)
Return Map(StringTrim2WS, $array) ; Map is from RS_Functionals.au3. It applies a function to each element in an array.
EndFunc
; ===
#cs ---------------------------------------
Author: Robert Schnitman
Date: 2020-06-14
Functions: StringPrefix(),
StringSuffix()
StringPrefixV()
StringSuffixV()
StringPrefix() prefixes a given string to another string.
StringSuffix() appends a string to another string.
The String*fixV() functions are vectorized versions of the above functions.
#ce ---------------------------------------
Func StringPrefix($string, $prefix)
Return $prefix & $string
EndFunc