Strings
Strings are collections of printable characters. String values in the code can be delimited by either the single quote or double quote character, although the character used to close must match the character used to open.
The only arithmetic operation that is valid for string variables is addition, which causes two strings to be concatenated.
The backslash \ character is used as a special escape to indicate special non-printable characters. The table below identifies characters available following a backslash and what they mean.
‘ |
place single quote in string |
“ |
place double quote in string |
b |
backspace |
t |
tab |
r |
carriage return |
n |
CR/LF |
\ |
single backslash |
An arithmetic operation cannot have only one argument that is a string variable. The intrinsic function
string()
must be used if a number is to be included as part of a string variable
(see variable xx in the example shown above). Also note the use of intrinsic function
type()
, which identifies the type of argument.
- The contents of a string may be accessed with parenthesis ().
A single argument must be a non-zero integer indicating the position of the character in the string and will return a one-character length string.
A negative value can be used to index from the back of the string, with -1 used to indicate the last character, -2 the next character to the left, and so on.
These same rules apply to a two argument access, in which case a substring from the first to the second index is returned. If the second is less than the first, a null string is returned.
On get, referring outside the string bounds returns a null string.
Either a single or double index may be used to assign a string of any length, in which case the single character or substring is replaced with the new string. If on assignment the second index is less than the first, then the index order is implicitly reversed.
Examples of this follow:
local a = "Frederick" local b = a(2) ; b = "r" local c = a(-3) ; c = "i" local d = a(2,4) ; d = "red" local e = a(2,-2) ; e = "rederic" a(3) = "upper" ; a = "Frupperderick" a(3,-7) = "lower" ; a = "Frlowerderick"
A common use of strings in FISH functions is to control interactive input and output. The following example illustrates this for user-supplied input parameters for Young’s modulus and Poisson’s ratio.
model new
fish define in_def(msg,default)
local xx = io.in(msg+'('+'default:'+string(default)+'):')
if type(xx) = 3
in_def = default
else
in_def = xx
endif
end
def moduli_data
global y_mod = in_def('Input Young`s modulus ',1.0e9)
global p_ratio = in_def('Input Poisson`s ratio ',0.25)
if p_ratio = 0.5 then
io.out(' Bulk mod is undefined at Poisson`s ratio = 0.5')
io.out(' Select a different value --')
p_ratio = in_def
endif
global s_mod = y_mod / (2.0 * (1.0 + p_ratio))
global b_mod = y_mod / (3.0 * (1.0 - 2.0 * p_ratio))
end
[moduli_data]
[y_mod]
[p_ratio]
[s_mod]
[b_mod]
All string FISH functions are indexed in the FISH Reference.
Strings are an iterable type. This means you can use it as the target of a loop foreach statement (see Loop ForEach and can be split (see Splitting). Each character in the string is iterated in order as a string of length 1.
Was this helpful? ... | Itasca Software © 2024, Itasca | Updated: Dec 19, 2024 |