Strings - HiStructClient/femcad-doc GitHub Wiki

Using strings in FemCad scripting

Basic string definiton uses quotation marks

 "this is a string"

Composition of strings

String can be composed using + sign

example

A := "First name"
B := "Last name"
C := A + ", " + B

gives

C := "First name, Last name"

Join strings

 ["Hi","my","friend"].JoinStrings()
 "Himyfriend"

Join strings with

 ["Hi","my","friend"].JoinStringsWith(" ")
 "Hi my friend"

Join strings fn

 strAr =  ["Hi","my","friend"]
 idxAr  = [1,0,1,0,2]
 idxAr.JoinStringsFn(i=>strAr[i])
 "myHimyHifriend"

Join strings with fn

 strAr =  ["Hi","my","friend"]
 idxAr  = [1,0,1,0,2]
 idxAr.JoinStringsWithFn("-",i=>strAr[i])
 "my-Hi-my-Hi-friend"

Convert double into string

 array.ToString(digits wanted)

example

A := 35.6568465
B := A.ToString("0")

gives

B := "36"

Notes to digits count:

There is a systematic format that defines how many digits in front of/after the decimal point is displayed. One can either use:

- zeros to define it precisely, 
- hashes and zeros to limit the numeber of digits (if exist),
- predefined formats in form of "F" or "E" + number

A := 123456.0987
B := A.ToString("0.0")  => 123456.1

A := 123456.0987
B := A.ToString("000 000 0") => 012 345 6

A := 123456.11111
B := A.ToString("#.00#")  => 123456.111

A := 123456.0987
B := A.ToString("F3")  => 123456.099

A := 123456
B := A.ToString("E")  => 1.234560E+005

Substring

It is possible to extract a substring from particular one.

 string.Substring(number)
 string.Substring(number, number)

example

name := "IPE 360"
type := name.Substring(0,3)
height := name.Substring(4)

gives

type := "IPE"
height := "360"

!! make sure the string is longer than used numbers !!

Contains substring

Test whether the string contains some sub-string

example

A := "Hello".Contains( "el" )
B := "Hello".Contains( "World" )

gives

A := True
B := False

IndexOf

It is possible to find the position of substring in string. It returns starting position of first appearance of the substring.

example

A := "Hello world!".IndexOf( "o" )
B := "Hello world!".IndexOf( "world" )

gives

A := 4
B := 6

Length of string

It is possible to get the length of string.

length := someString.Length

Replacement of symbol

A symbol in string can be replaced with:

replacedString := ("Zprav si to.").Replace("Z", "S") => "Sprav si to."
stringWithSpaces := ("0, 1, 2, 3  ").Replace(" ","") => "0,1,2,3"

Quotation marks

Quotation marks can be created with:

quoteString = Fcs.Converters.ToJson( "" ).Substring( 1, 1 )

Conversion of strings into numerical values

it is possible to use similar trick as for quotation marks, but it is necessary to make sure the string can be converted into value.

stringToNum := str => Fcs.Converters.ParseJson(" { 'myVal':"+str+"}").myVal

(this function is available in FcsFunctions (branch Apac_MT))

Array of values to string

When you need to convert array of values to one string with values separated by commas, this function can be used:

ArrayToOneString := array => ( (array.Count > 0)
   ?( array.Aggregate( [], a, b => a + ( ", " + b ) ).Substring(4) )
   :(Fcs.Exception.Throw( "Array must have at least one string." ) ) )

(the function is available in FcsFunctions (branch Deve-TB in fcs-HBC repos.)

  1. JoinStrings
  2. JoinStringsFn
  3. JoinStringsWith
  4. JoinStringsWithFn
⚠️ **GitHub.com Fallback** ⚠️