PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Classic VB - Datatype introductions: What is a String?-VBForums
Results 1 to 1 of 1

Thread: Classic VB - Datatype introductions: What is a String?

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Classic VB - Datatype introductions: What is a String?

    This introduction to strings starts off with a quick summary, goes through technical details on how strings actually work, which is very useful information for any programmer, and ends with an introduction to functions you can use.


    Visual Basic has a very "intelligent" and easy to use datatype called String. The String is simply a series of characters followed by eachother. What makes strings intelligent?
    • You can combine strings together with the & operator. (You could also use + operator, but it can give you unexpected results, see about coercion below.)
    • You can get a slice out of a string with Left$, Mid$ and Right$ functions.
    • You can split and join string to string array and vice versa with Split and Join functions.
    • There are various other functions for replacing, searching and formatting data: Replace, InStr, Format$, Len, LCase$, UCase$... a wide variety of string functions make strings easy to use in many tasks.
    • VB's major feature, datatype coercion, lets you use almost any other datatype with strings: most commonly the other datatype is "forced" to string, meaning data is converted into series of characters and it is no longer in it's original format. Coercion can also go into the other direction, but it requires the string is properly formatted.

    Ease of use and intelligency also has it's cost. VB's string datatype is slower than in most other languages. However, the ease of use often pays back, especially in development time: often you don't need to take care of handling memory to make sure there is enough space for the string data, which is what you need to take care of with several other programming languages (namely C/C++).



    Datatype information
    The string variable itself is a pointer to BSTR structure. A pointer is a 32-bit value that points to memory address where the string structure resides. You can get this pointer value with VarPtr (variable pointer). The VarPtr function works with any variable.

    BSTR structure is separated to three parts. The first part is a Long value that identifies how many bytes of data follow. After this begins the actual string data if the byte count is bigger than 0. The last, third part of the structure is the terminating null character. The null character means two bytes that are 0. Why two? Because each character of string data is Unicode: one character occupies two bytes.

    A simple hex dump of a BSTR structure:
    Code:
    Hex     : 06 00 00 00 41 00 42 00 43 00 00 00
    Decimal : 6           65    66    67    0
    What    : Byte length "A"   "B"   "C"   vbNullChar
    You can also get a pointer to the beginning of the string data. You get this pointer with StrPtr (string pointer).

    Note that VarPtr can be zero! This means a string that hasn't been initialized and it does not have the BSTR structure. It consumes no memory (except the variable pointer). This means that there is practical difference in the following example:
    VB Code:
    1. strHasBSTR = ""
    2. strNoBSTR = vbNullString
    vbNullString is simply a variable with zero pointer. It doesn't have a BSTR structure. When you use "", you create a new BSTR structure into memory, consuming 6 "extra" bytes when in comparison to vbNullString.



    Datatype behavior and features
    What happens when you add two strings together? The answer is simple: you create a new string where the two existing strings are stored. For an example:
    VB Code:
    1. strABC = "A" & "B" & "C"
    What happens here memorywise is that you create a new string: VB allocates memory for a BSTR structure that contains three characters (totaling 4 [length] + 6 [data] + 2 [null] = 12 bytes). After this the data section is filled with string data of each of the passed strings.

    This can lead to performance problems on special occasions. Consider the following:
    • strBuffer = 1MB of string data
    • Start loop: loop 1000 times
    • strBuffer = strBuffer & strBuffer
    • End loop

    First loop: allocate 2MB of memory, copy strBuffer data two times, destroy old BSTR structure (1MB).
    Second loop: allocate 4MB of memory, copy strBuffer data two times, destroy old BSTR structure (2MB).
    Third loop: allocate 8MB of memory, copy strBuffer data two times, destroy old BSTR structure (4MB).
    Fourth loop: allocate 16MB of memory...

    I guess you can see the problem. Looping can have surpising effects. Note that even though this example is not very practical, the important part is the fact you're doing strBuffer = strBuffer & - if you do this within a loop, the loop will slow down on each step of the loop.



    The functions

    Left$(String, Length)
    Left$ returns X given amount of characters from the left size of the string.


    Right$(String, Length)
    Same as Left$, but starting from the right edge of the string.


    Mid$(String, Start, [Length])
    Mid$ clips a string from the given start position. If a length is given, it takes the given amount of characters; otherwise it takes all strings after the given start position.
    VB Code:
    1. strTest = Mid$("ABC", 2, 1)
    2. ' strTest = "B"
    3.  
    4. strTest = Mid$("ABC", 2)
    5. ' strTest = "BC"
    You can also use Mid$ in the other side, on the side to be passed to. There is a limitation to this: you must give a length and you can only pass characters of the given length. For example:
    VB Code:
    1. strTest = "ABC"
    2. Mid$(strTest, 2, 1) = "D"
    3. ' strTest = "ADC"


    Chr$(Code) and ChrW$(Code)
    Chr$ returns characters for the current computer locale/character set.
    ChrW$ returns characters in Unicode compatible manner.

    Of the two, ChrW$ is faster to execute, because it doesn't need to take locale into account.
    VB Code:
    1. strTest = Chr$(65)
    2. ' strTest = "A"
    3.  
    4. strTest = Chr$(&H41)
    5. ' strTest = "A"
    6.  
    7. strTest = ChrW$(65)
    8. ' strTest = "A"
    9.  
    10. strTest = ChrW$(&H41)
    11. ' strTest = "A"


    Asc(String) and AscW(String)
    Asc returns character code of a value; it does it compatible to the current locale. If you pass more than one character to the function, the other characters are ignored.

    AscW does the same, but returns Unicode standard compatible character codes.

    Of the two, AscW is faster because it doesn't take locale into account.


    Space$(Number)
    Creates a string that has given amount of space characters (character code 32).


    String$(Number, String)
    Makes a string where given string parameter is repeated given amount of times.


    InStr([Start], String, Keyword, [CompareMethod])
    Search a string in either binary or text comparison mode. Defaults to binarywise comparison. Return value is 0 if keyword string was not found. -1 is returned if an error occurred.
    VB Code:
    1. lngPos = InStr("ABC", "B")
    2. ' lngPos = 2
    3.  
    4. lngPos = InStr("ABC", "b")
    5. ' lngPos = 0
    6.  
    7. lngPos = InStr(1, "ABC", "b", vbTextCompare)
    8. ' lngPos = 2
    9.  
    10. lngPos = InStr(3, "ABC", "b", vbTextCompare)
    11. ' lngPos = 0


    Replace(String, From, To, [Start], [Count], [CompareMethod])
    Replaces a search keyword to another in a string starting from given position for given amount of times using the given comparison method. You don't need to define start position, the amount of replaces and the compare method: the functions defaults to all and to binary comparison.
    VB Code:
    1. strTest = Replace("ABC", "B", "b")
    2. ' strTest = "AbC"
    3.  
    4. strTest = Replace("ABCA", "A", "B", 2, 1)
    5. ' strTest = "ABCB"
    6.  
    7. strTest = Replace("AaAaAa", "a", "B", , , vbTextCompare)
    8. ' strTest = "BBBBBB"


    Split(String, [Keyword])
    Splits string to string array by given keyword. Defaults to space character if no keyword is given.
    VB Code:
    1. Dim strArray() As String
    2. strArray = Split("A B C")
    3. ' strArray(0) = "A"
    4. ' strArray(1) = "B"
    5. ' strArray(2) = "C"
    6.  
    7. strArray = Split("ABC")
    8. ' strArray(0) = "ABC"
    9.  
    10. strArray = Split("ABC", "B")
    11. ' strArray(0) = "A"
    12. ' strArray(1) = "C"


    Join(String, [Keyword])
    Combines a string array to a string using given string keyword as separator. Keyword defaults to space character.


    LCase$(String)
    Returns the given string in lower case letters.


    UCase$(String)
    Returns the given string in upper case letters.


    Format$(Expression, [Format], [FirstDayOfWeek], [FirstWeekOfYear])
    Returns a given expression or data in the given format. The default format is to return as a string.

    This function is has a wide variety of features. The most used special features are that it can handle dates and do numeric formatting. It follows the current locale settings of a computer.
    VB Code:
    1. strTest = Format$(0.1, "0.00")
    2. ' strTest = "0.10"
    3.  
    4. strTest = Format$(0.1, "0.##")
    5. ' strTest = "0.1"
    6.  
    7. strTest = Format$(0.01, "0.##")
    8. ' strTest = "0.01"
    9.  
    10. strTest = Format$(0.001, "0.##")
    11. ' strTest = "0."


    CStr(Variable)
    Converts the given variable to a string formatted data.
    Last edited by si_the_geek; Sep 29th, 2006 at 02:54 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width