Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

String Built-in Functions

These built-in functions are accessed via the pseudo unit “String”

String:concat

Returns a string value that is the concatenated result of two or more arguments passed to the function. As of Helium 1.3.1 null values will be treated as empty strings.

 

  1. @Test
  2. void testConcatenate(){
  3.     Assert:isEqual("abcdef", String:concat("abc","def"), "Concatenate returned an incorrect string");
  4.     Assert:isEqual("abcdef", String:concat("abc",null,"def"), "Concatenate returned an incorrect string");
  5. } 


String:split

Returns an array of string values that represent the tokens from the value expression that are separated by the separator expression.

Note that in order to split a special character such as '|', one needs to escape the string with a '\' character.  The '\' may need to be escaped as well, so in this case one will end up with String:split(someText, "\\|").

 

  1. @Test
  2. void testSplit(){
  3.     string[] result = String:split("abc def", " ");
  4.  
  5.     Assert:isEqual(2, result.length(), "Split returned an incorrect number of tokens");
  6.     Assert:isEqual(result.get(0), "abc", "Split returned an incorrect first token");
  7.     Assert:isEqual(result.get(1), "def", "Split returned an incorrect second token");
  8. }

 

String:length

Returns the length of the string being passed

 

  1. int i = String:length("Hello world");

 

String:substring

Substring of a string based on index positions

 

  1. @Test
  2. void testSubstring(){
  3.     Assert:isEqual("ello", String:substring("Hello World", 1, 4), "String:substring failed");
  4.     Assert:isNotEqual("ello", String:substring("Hello World", 1, 3), "String:substring failed");
  5. }

 

String:lower

Convert string to lowercase

 

  1. @Test
  2. void testLower(){
  3.     Assert:isEqual("hello world", String:lower("Hello World"), "String:lower failed");
  4.     Assert:isNotEqual("Hello world", String:lower("Hello World"), "String:lower failed");
  5. }

 

String:upper

Convert string to uppercase

 

  1. @Test
  2. void testUpper() {
  3.     Assert:isEqual("HELLO WORLD", String:upper("hello world"), "String:upper failed");
  4.     Assert:isNotEqual("hello world", String:upper("hello world"), "String:upper failed");
  5. }

 

String:startsWith

Check if a particular string starts with a string

 

  1. @Test
  2. void testStartsWith(){
  3.     Assert:isTrue(String:startsWith("Hello World", "Hello"), "String:startsWith failed");
  4.     Assert:isFalse(String:startsWith("Hello World", "ello"), "String:startsWith failed");
  5. }

 

String:endsWith

Check if a particular string ends with a string

 

  1. @Test
  2. void testEndsWith(){
  3.     Assert:isTrue(String:endsWith("Hello World", "World"), "String:endsWith failed");
  4.     Assert:isFalse(String:endsWith("Hello World", "Worl"), "String:endsWith failed");
  5. }

 

String:indexOf

Returns the index within this string of the first occurrence of the specified substring.

 

  1. @Test
  2. void testIndexOf(){
  3.     Assert:isEqual(0, String:indexOf("Hello World", "Hello"), "String:indexOf failed");
  4.     Assert:isEqual(1, String:indexOf("Hello World", "ello "), "String:indexOf failed");
  5.     Assert:isEqual(2, String:indexOf("Hello World", "llo W"), "String:indexOf failed");
  6.     Assert:isEqual(3, String:indexOf("Hello World", "lo Wo"), "String:indexOf failed");
  7. }

 

String:join

Join a collection of strings into one result string with the specified join character

 

  1. @Test
  2. void testJoin(){
  3.     string original = "abc def ghi";    
  4.     string separator = " ";
  5.     Assert:isEqual(original, String:join(String:split(original, separator), separator), "String:join failed");
  6. }

 

 

 

 

 

 

 

 

 

  • No labels