Skip to content

Commit 03170ae

Browse files
committed
Add string function LEFT
1 parent 31a0c40 commit 03170ae

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

src/main/java/com/scriptbasic/utility/functions/StringFunctions.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ static public String rtrim(final String s) {
7272
return s.replaceAll("\\s*$", "");
7373
}
7474

75+
/**
76+
* Return {@code len} number of characters from the left (the beginning) of the
77+
* string.
78+
*
79+
* @param s parameter
80+
* @param len parameter
81+
* @return return value
82+
*/
83+
@BasicFunction(classification = {com.scriptbasic.classification.String.class,
84+
com.scriptbasic.classification.Utility.class})
85+
static public String left(final String s, final int len) {
86+
return s.length() > len ? s.substring(0, len) : s;
87+
}
88+
7589
/**
7690
* Return a substring from the string that starts at the position
7791
* {@code start} and has a length of {@code len}.

src/test/java/com/scriptbasic/testprograms/TestPrograms.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,11 @@ public void testStringConcatenation() throws Exception {
160160
codeTest("TestStringConcatenation.bas", "");
161161
}
162162

163+
@Test
164+
public void testStringFunctions() throws Exception {
165+
codeTest("TestStringFunctions.bas", "0189");
166+
}
167+
163168
@Test
164169
public void testJavaObjectFieldAccess() throws ScriptBasicException, ClassNotFoundException, AnalysisException {
165170
final var e = new TestingExecutor();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
v = "0123456789"
2+
PRINT left(v,2)
3+
PRINT right(v,2)

0 commit comments

Comments
 (0)