Export (0) Print
Expand All

Zero-based vs. One-based String Access in Visual Basic

This topic compares how Visual Basic and the .NET Framework provide access to the characters in a string. The .NET Framework always provides zero-based access to the characters in a string, whereas Visual Basic provides zero-based and one-based access, depending on the function.

For an example of a one-based Visual Basic function, consider the Mid function. It takes an argument that indicates the character position at which the substring will start, starting with position 1. The .NET Framework StringSubstring method takes an index of the character in the string at which the substring is to start, starting with position 0. Thus, if you have a string "ABCDE", the individual characters are numbered 1,2,3,4,5 for use with the Mid function, but 0,1,2,3,4 for use with the StringSubstring method.

For an example of a zero-based Visual Basic function, consider the Split function. It splits a string and returns an array containing the substrings. The .NET Framework StringSplit method also splits a string and returns an array containing the substrings. Because the Split function and Split method return .NET Framework arrays, they must be zero-based.

Show:
© 2014 Microsoft