@@ -8,6 +8,79 @@ using StringTools;
88 */
99final class DataUtil
1010{
11+ function new () {}
12+
13+ /**
14+ * Inserts string `toInsert` inside of string `s` based on the index.
15+ *
16+ * ## Example
17+ * ```
18+ * var s:String = 'This is so cool!';
19+ * insertStringAtIndex(s, 'function ', 4); // Output: This function is so cool!
20+ * ```
21+ *
22+ * @param s The string to be changed.
23+ * @param toInsert The string to insert.
24+ * @param index The index to insert `toInsert`.
25+ * @return The modified string.
26+ */
27+ public static function insertStringAtIndex (s : String , toInsert : String , index : Int ): String
28+ {
29+ var toReturn : String = ' ' ;
30+ var toConcat : Array <String > = [];
31+ // Add all characters to the list
32+ // (including the string to insert at the wanted index)
33+ for (i in 0 ... s .length )
34+ {
35+ toConcat .push (s .charAt (i ));
36+ if (i == index )
37+ {
38+ toConcat .push (toInsert );
39+ }
40+ }
41+ // Reconstruct the string back with the inserted text
42+ for (char in toConcat )
43+ {
44+ toReturn + = char ;
45+ }
46+ return toReturn ;
47+ }
48+
49+ /**
50+ * Deletes a specific part of text from string `s`
51+ * based on `startIndex` and `endIndex`.
52+ *
53+ * If `startIndex` is greater than `endIndex`, then the numbers are swapped.
54+ *
55+ * ## Example
56+ * ```
57+ * var s:String = 'The word "not" is not deleted!';
58+ * deleteTextFromString(s, 18, 21); // Output: The word "not" is deleted!
59+ * ```
60+ *
61+ * @param s The string to delete text from.
62+ * @param startIndex The starting index to delete text from.
63+ * @param endIndex The ending index to delete text from (inclusive).
64+ * @return The modified string.
65+ */
66+ public static function deleteTextFromString (s : String , startIndex : Int , endIndex : Int ): String
67+ {
68+ var si : Int = (! (startIndex > endIndex )) ? startIndex : endIndex ;
69+ var ei : Int = (! (startIndex > endIndex )) ? endIndex : startIndex ;
70+ var toReturn : String = ' ' ;
71+ for (i in 0 ... s .length )
72+ {
73+ // If the current character isn't in the range of
74+ // the text to be "deleted", then add the character
75+ // at the end of toReturn
76+ if (! (i >= si && i <= ei ))
77+ {
78+ toReturn + = s .charAt (i );
79+ }
80+ }
81+ return toReturn ;
82+ }
83+
1184 /**
1285 * Removes a word from a string by its index.
1386 *
@@ -30,22 +103,14 @@ final class DataUtil
30103 var sentence : Array <String > = s .trim ().split (delimiter );
31104 // Convert it back to a regular string, but
32105 // without the removed word
33- for (i in 0 ... sentence .length )
106+ for (word in 0 ... sentence .length )
34107 {
35- if (i != index )
108+ if (word != index )
36109 {
37- toReturn + = ' ${sentence [i ]} ' ;
110+ toReturn + = ' ${sentence [word ]} ' ;
38111 }
39112 }
40-
41- if (trim )
42- {
43- return toReturn .trim ();
44- }
45- else
46- {
47- return toReturn ;
48- }
113+ return (trim ) ? toReturn .trim () : return toReturn ;
49114 }
50115
51116 /**
@@ -54,11 +119,11 @@ final class DataUtil
54119 *
55120 * @param object The JSON `Dynamic` object to obtain the field from.
56121 * @param field The field to get.
57- * @param defaultValue The value that is returned if the field does not exist.
122+ * @param defaultValue The value that is returned if the field does not exist. Default value is `null`.
58123 * @return The value found from the said field. If it isn't found, then the
59124 * `defaultValue` parameter is returned instead.
60125 */
61- public static inline function getDynamicField (object : Dynamic , field : String , defaultValue : Dynamic ): Any
126+ public static inline function getDynamicField (object : Dynamic , field : String , ? defaultValue : Dynamic ): Any
62127 {
63128 return (Reflect .hasField (object , field )) ? Reflect .field (object , field ) : defaultValue ;
64129 }
0 commit comments