Strings

A string represents a sequence of characters equivalent to an array of char. It is declared like this:

string[string_length]

The specifier string_length is a number of characters a string consists of. The string is stored internally as the given sequence of characters plus a final null character (zero). This appended “stamp” does not count against string’s total length.

A null string ("") is stored as a single null character.

You can assign string literals or other strings to string variables. The string on the right side of an assignment operator has to be shorter than another one, or of equal length. For example:

dim msg1 as string[20]
dim msg2 as string[19]

main:
  msg1 = "This is some message"
  msg2 = "Yet another message"

  msg1 = msg2  ' this is ok, but vice versa would be illegal

Alternately, you can handle strings element–by–element. For example:

dim s as string[5]
' ...
s = "mik"
' s[0] is char literal "m"
' s[1] is char literal "i"
' s[2] is char literal "k"
' s[3] is zero
' s[4] is undefined
' s[5] is undefined

Be careful when handling strings in this way, since overwriting the end of a string will cause an unpredictable behavior.

Array of string is declared in this manner:

typedef str as string[5]   ' first, declare str as a string of 5 elements

dim buffer as str[5]       ' now, declare buffer as a array of str elements

String Concatenating

mikroBasic PRO for PIC allows you to concatenate strings by means of plus operator. This kind of concatenation is applicable to string variables/literals, character variables/literals. For control characters, use the non-quoted hash sign and a numeral (e.g. #13 for CR).

Here is an example:

dim msg as string[20]
    res_txt as string[5]
    res, channel as word

main:

  '...

  ' Get result of ADC
  res = Adc_Read(channel)

  ' Create string out of numeric result
  WordToStr(res, res_txt)

  ' Prepare message for output
  msg = "Result is " +      ' Text "Result is"
          res_txt           ' Result of ADC


  '...
  Notes :
Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code