Share via


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.

One-Based

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 System.String.Substring(System.Int32) 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 System.String.Substring(System.Int32) method.

Zero-Based

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 System.String.Split(System.Char[]) 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.

See Also

Tasks

Troubleshooting Collections

Reference

Mid Function (Visual Basic)
Split Function (Visual Basic)
Substring
Split

Other Resources

Introduction to Strings in Visual Basic