Skip to main content

Strings and Numbers

BBj's string and numeric functions are more powerful than they first appear. A single CVS() call replaces what would be multiple method calls in Java, and STR() with masks handles formatting that other languages need entire libraries for. If you are coming from Java or .NET, note that BBj uses substring notation A$(pos,len) rather than named functions for extraction.

At a Glance

FunctionSyntaxPurpose
LENlen(a$)String length in bytes
Substringa$(pos,len)Extract substring (1-based)
CVScvs(a$, mask)Trim, case-convert, clean whitespace
POSpos("find"=a$)Find substring position
MASKmask(a$, "regex")Perl 5 regex pattern match
STRstr(num:"mask")Format number or string
NUMnum(a$)Convert string to number

For Java, Python, and C# Developers

TaskJavaPythonC#BBj
Get lengths.length()len(s)s.Lengthlen(s$)
Substrings.substring(1, 4)s[1:4]s.Substring(1, 3)s$(2, 3) (1-based)
Find in strings.indexOf("x")s.find("x")s.IndexOf("x")pos("x" = s$)
Replaces.replace("a", "b")s.replace("a", "b")s.Replace("a", "b")stbl("!REPLACE", s$, "a", "b")
Trim whitespaces.trim()s.strip()s.Trim()cvs(s$, 3)
String to numberInteger.parseInt(s)int(s)int.Parse(s)num(s$)
Number to stringString.valueOf(n)str(n)n.ToString()str(n)
Regex matchPattern.matches(p, s)re.match(p, s)Regex.IsMatch(s, p)mask(s$, p$)

BBj string positions are 1-based, not 0-based. pos() uses the syntax pos(needle$ = haystack$) with the needle on the left side of the = sign. For the complete cross-language reference, see BBj for Java, Python, and C# Developers.

Complete Runnable Examples

This chapter's code snippets illustrate individual concepts. For complete, runnable programs you can open directly in the BBj IDE, see the sample files in samples/05-strings-and-numbers/:

  • string_basics.bbj -- LEN(), substring extraction, concatenation, CVS()
  • pos_searching.bbj -- POS() basic search, backward scan, occurrence counting
  • str_num_formatting.bbj -- STR() and NUM() formatting and conversion
  • mask_regex.bbj -- MASK() with Perl 5 regex patterns

See Running Samples for setup instructions.