String function

Forgive me for this probably simple question, but is there a predefine function that will return part of a string.

ex.

If s is a string value of "basic" and I wanted to return just the first letter "b", is there a predefine function that will do that



Answer this question

String function

  • Kishore

    Spotty,

    The Mid$ functions, holdover from VB6 are 1 based instead of 0-based.

    That's important to know....



  • Herve ANCHER

    Or Mid$, left$ and right$ methods


  • Kammy

    All three methods will work and do the trick you ask for but it slightly different ways.

    I would suggest that you look at the help for these methods/properties and with your task you are trying to achieve - see which one is most suitable for your task.

    As with most software development there are numerous ways to solve a problem.


  • DavidBarracuda

    Substring would be the way to go ala:

          Dim firstChar as String = s.Substring(0,1)



  • Christofer

    For a single character the most direct way (and most efficient) is to just use the Chars property of String (which returns the array of characters making up the string):

    Dim ThisChar As Char = SomeString.Chars(0)

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# Converter
    Instant VB: C# to VB Converter
    Instant C++: C# to C++ Converter
    Instant C++: VB to C++ Converter
    Clear VB: Cleans up VB code



  • NicBar

    That is correct.    Left$, Mid$ and Right$ are all functions that have existed in many different versions of Basic including VB6 and even way pre-dating Visual Basic,  

    I look back to many of the versions of BASIC that existing in the 80's such as the Sinclair, TRS, Dragon, BBC Micro machines as well as GWBasic, Quickbasic etc, that all had these functions in.

     As I said, all three methods work but do so in slightly different ways and seeing which most suits you purpose.

     

     


  • String function