Substring function in vb

providence Posted messages 99 Status Member -  
 bob -
Hello,
I would like to know if the 'SUBSTRING' function is indeed a function in Visual Basic, and if so, what is its principle of operation. Thank you for your help.
Configuration: Windows XP Firefox 2.0.0.4

2 answers

  1. irem Posted messages 166 Status Member 99
     
    substring is indeed a function of vb but only in .net

    The usage is simple

    $toto ="123456789"
    $toto.substring (2,4) gives "2345", from the 2nd character 4 characters

    In the older versions, you had to use mid$ in the form mid$(toto,2,4) -> "2345"

    Irem
    11
    1. providence Posted messages 99 Status Member 7
       
      Thank you, Irem, for your response. In fact, I have a program to write in VB6 as part of a school project. The statement is as follows:
      In the 2007 encyclopedia of games, it took 2007 times "1" to number all the pages.
      Write a VB6 program that allows us to determine the number of pages in this encyclopedia.
      Note: When numbering from 1 to 20, we use "1" a total of 12 times!

      On the forum, I got a rough solution that involves the SUBSTRING function, but I'm not very sure how it works. I would like to get your opinion on this below is the solution:

      Option Explicit

      Dim i As Integer
      Dim chaine As String
      Dim nb_page As Integer

      nb_page = 0
      i = 0

      While (i <> 2007)

      chaine = Str(i) 'I have doubts about the syntax for converting an integer to a string

      'Here you read your string using an if statement like this

      ' We loop as long as there are "1"s in the string.
      j = 0
      while (substring(j, chaine, "1") > 0)
      nb_page = nb_page + 1
      j = substring(j, chaine, "1")
      wend
      i = i + 1 'essential to avoid an infinite loop!

      Wend
      0
    2. bob
       
      saol irem
      0
  2. irem Posted messages 166 Status Member 99
     
    Hello Providence,

    Your algorithm is wrong, indeed your loop actually counts the number of 1s in 2007 pages and not the number of pages to get 2007 "1s", it would be better to write

    Function test() As Integer
    Dim int_I, Int_Pages As Integer
    Dim str_Temp As String
    int_I = 0
    Int_Pages = 0
    While int_I < 2007
    Int_Pages = Int_Pages + 1
    str_Temp = Int_Pages
    While InStr(1, str_Temp, "1") > 0
    int_I = int_I + 1
    str_Temp = Right(str_Temp, Len(str_Temp) - InStr(1, str_Temp, "1"))
    Wend
    Wend
    test = Int_Pages
    End Function

    And you get 3169 pages

    Irem
    1