11package com .scriptbasic .utility .functions ;
22
33import com .scriptbasic .api .BasicFunction ;
4+ import com .scriptbasic .interfaces .BasicRuntimeException ;
45
56/**
67 * <p>
@@ -72,6 +73,20 @@ static public String rtrim(final String s) {
7273 return s .replaceAll ("\\ s*$" , "" );
7374 }
7475
76+ /**
77+ * Return {@code len} number of characters from the left (the beginning) of the
78+ * string.
79+ *
80+ * @param s parameter
81+ * @param len parameter
82+ * @return return value
83+ */
84+ @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
85+ com .scriptbasic .classification .Utility .class })
86+ static public String left (final String s , final int len ) {
87+ return s .length () > len ? s .substring (0 , len ) : s ;
88+ }
89+
7590 /**
7691 * Return a substring from the string that starts at the position
7792 * {@code start} and has a length of {@code len}.
@@ -80,11 +95,15 @@ static public String rtrim(final String s) {
8095 * @param start parameter
8196 * @param len parameter
8297 * @return return value
98+ * @throws BasicRuntimeException incorrect parameter
8399 */
84100 @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
85101 com .scriptbasic .classification .Utility .class })
86- static public String mid (final String s , final int start , final int len ) {
87- return s .substring (start , start + len );
102+ static public String mid (final String s , final int start , final int len ) throws BasicRuntimeException {
103+ if (start < 1 ) {
104+ throw new BasicRuntimeException ("Incorrect value in parameter start: " + start );
105+ }
106+ return s .substring (start - 1 , start - 1 + len );
88107 }
89108
90109 /**
0 commit comments