VBA/Excel/Access/Word/Data Type/String Function

Материал из VB Эксперт
Перейти к: навигация, поиск

Format() Formats strings, dates, and numbers as specified.

   <source lang="vb">

Sub strDemo15()

  Debug.Print Format("ABCD", "<")

End Sub

</source>
   
  


InStr(): A number representing the place value of a particular character within a string

   <source lang="vb">

Sub InstrLeft()

   Dim userName As String
   Dim firstName As String
   Dim spaceLoc As Integer
   userName = "First Last"
   spaceLoc = InStr(1, userName, " ")
   firstName = Left(userName, spaceLoc - 1)
   Debug.Print firstName

End Sub

</source>
   
  


Instr function returns the position where one string begins within another string

   <source lang="vb">

Sub InstrExample()

 Debug.Print InStr("A Balter", "Balter") "Returns 8
 Debug.Print InStr("Hello", "l") "Returns 3
 Debug.Print InStr("c:\my documents\my file.txt", "\") "Returns 3

End Sub

</source>
   
  


InStr() Returns the location of the first character of a substring within another string.

   <source lang="vb">

Sub strDemo8()

  Debug.Print InStr("Test", "e")

End Sub

</source>
   
  


InStrRev begins searching at the end of a string and returns the position where one string is found within another string:

   <source lang="vb">

Sub InstrRevExample()

   Debug.Print InStrRev("c:\my documents\my file.txt", "\")

End Sub

</source>
   
  


LCase() Converts the string to lowercase.

   <source lang="vb">

Sub strDemo10()

  Debug.Print lcase("ABCD")

End Sub

</source>
   
  


Left(string, length) returns the specified number of characters from the left end of a string

   <source lang="vb">

Sub leftDemo()

   Dim strPhone As String
   strPhone = "123-1231233"
   Dim strArea As String
   strArea = Left(strPhone, 3)
   
   Debug.Print strArea

End Sub

</source>
   
  


Len() Returns the number of characters in a string.

   <source lang="vb">

Sub strDemo9()

  Debug.Print Len("Test")

End Sub

</source>
   
  


Len() return the number of characters in a string

   <source lang="vb">

Sub lenDemo()

   Dim userName As String
   Dim firstName As String
   Dim spaceLoc As Integer
   Dim strLength As Integer
   
   userName = "First Last"
   spaceLoc = InStr(1, userName, " ")
   firstName = Left(userName, spaceLoc - 1)
   strLength = Len(firstName)
   Debug.Print strLength

End Sub

</source>
   
  


Mid(string, start[, length]) returns the specified number of characters from inside the given string

   <source lang="vb">

Sub midDemo()

   Dim strPhone As String
   strPhone = "123-1231233"
   
   Dim strLocalExchange As String
   strLocalExchange = Mid(strPhone, 4, 3)
   
   Debug.Print strLocalExchange

End Sub

</source>
   
  


Right(string, length) returns a specified number of characters from the right end of a string

   <source lang="vb">

Sub rightDemo()

   Dim strPhone As String
   strPhone = "123-1231233"
   
   Dim strLocalNumber As String
   strLocalNumber = Right(strPhone, 7)
   Debug.Print strLocalNumber

End Sub

</source>
   
  


StrReverse() Reverses the order of the characters in a string.

   <source lang="vb">

Sub strDemo14()

  Debug.Print StrReverse("Test")

End Sub

</source>
   
  


UCase() Converts the string to uppercase.

   <source lang="vb">

Sub strDemo11()

  Debug.Print UCase("abcd")

End Sub

</source>
   
  


UCase returns a string that is all uppercase

   <source lang="vb">

Sub UCaseExample()

   Debug.Print UCase$("Hello World") "Prints HELLO WORLD

End Sub

</source>
   
  


Using LTrim, RTrim, and Trim to Trim Spaces from a String

   <source lang="vb">

Sub TrimDemo()

   Dim strUntrimmed As String, strTrimmed As String
   strUntrimmed = Selection.Text
   strTrimmed = Trim(strUntrimmed)

End Sub

</source>