VB.Net by API/System/String — различия между версиями

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

Версия 19:40, 26 мая 2010

String.Compare

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            Dim result As Integer 
            result = String.rupare(s1, s2)
            Console.WriteLine( _
              "compare s1: {0}, s2: {1}, result: {2}" _
              & Environment.NewLine, s1, s2, result)
            result = String.rupare(s1, s2, True)
            Console.WriteLine("Compare insensitive. result: {0}" _
               & Environment.NewLine, result)
  End Sub

End Class


 </source>


String.Concat

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            " concatenation method
            Dim s3 As String = String.Concat(s1, s2)
            Console.WriteLine("s3 concatenated from s1 and s2: {0}", s3)
            " use the overloaded operator
            Dim s4 As String = s1 & s2
            Console.WriteLine("s4 concatenated from s1 & s2: {0}", s4)
  End Sub

End Class


 </source>


String.Copy

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            Dim s3 As String = "Liberty Associates, Inc. provides "
            s3 = s3 & "custom .NET development"
            Dim s5 As String = String.Copy(s2)
            Console.WriteLine("s5 copied from s2: {0}", s5)
            Dim location As Integer = s3.IndexOf("provides")
            Dim s10 As String = s3.Insert(location, "usually ")
            Console.WriteLine("s10: {0}", s10)
  End Sub

End Class


 </source>


String.Empty

<source lang="vbnet">

Option Strict On Public Module CommandTest

  Public Sub Main()
     Dim arguments As String = Command
     If arguments = String.Empty Then
        Console.WriteLine("An empty string.")
     Else
        " handle command line argument(s)
        Console.WriteLine("Command line: {0}", arguments)
     End If
  End Sub

End Module


 </source>


String.Equals

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            " the String copy method
            Dim s5 As String = String.Copy(s2)
            Console.WriteLine("s5 copied from s2: {0}", s5)
            " copy with the overloaded operator
            Dim s6 As String = s5
            Console.WriteLine("s6 = s5: {0}", s6)
            " member method
            Console.WriteLine("Does s6.Equals(s5)?: {0}", s6.Equals(s5))
            " shared method
            Console.WriteLine("Does Equals(s6,s5)?: {0}", _
               String.Equals(s6, s5))
  End Sub

End Class


 </source>


String.Format

<source lang="vbnet">

Public Class Tester

   Public Shared Sub Main
       Console.WriteLine(String.Format( _
           "There are about {0} days in {1} years.", _
           365.25 * 3, 3, 17))

   End Sub

End Class


 </source>


String.GetEnumerator

<source lang="vbnet">

public class Test

  public Shared Sub Main
       Dim S As String = "Welcome to VB"
       Dim Enumerator As CharEnumerator = S.GetEnumerator
   
       While (Enumerator.MoveNext())
         Console.WriteLine(Enumerator.Current())
       End While
  End Sub

End class


 </source>


String.GetHashCode()

<source lang="vbnet">

public class Test

  public Shared Sub Main
   Dim Number As String
   Number = "4"
   Console.WriteLine(Integer.Parse(Number))
   Console.WriteLine(Number.GetHashCode())
  End Sub

End class


 </source>


String.IndexOfAny(String sub)

<source lang="vbnet"> Imports System Public Class MainClass

  Shared Sub Main()
     Dim letters As String = "abcdefghijklmabcdefghijklm"
     Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
     Console.WriteLine("IndexOfAny to find first occurrence of character in array")
     Console.WriteLine("First occurrence of ""c""," & _
       " ""a"" or ""$"" is located at " & _
       letters.IndexOfAny(searchLetters))
     Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
        """$"" is located at " & _
        letters.IndexOfAny(searchLetters, 7))
     Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
        """$"" is located at " & _
        letters.IndexOfAny(searchLetters, 20, 5))
  End Sub " Main

End Class


 </source>


String.IndexOfAny(String sub, Int count)

<source lang="vbnet"> Imports System Public Class MainClass

  Shared Sub Main()
     Dim letters As String = "abcdefghijklmabcdefghijklm"
     Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
     Console.WriteLine("IndexOfAny to find first occurrence of character in array")
     Console.WriteLine("First occurrence of ""c""," & _
       " ""a"" or ""$"" is located at " & _
       letters.IndexOfAny(searchLetters))
     Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
        """$"" is located at " & _
        letters.IndexOfAny(searchLetters, 7))
     Console.WriteLine("First occurrence of ""c"", ""a"" or " & _
        """$"" is located at " & _
        letters.IndexOfAny(searchLetters, 20, 5))
  End Sub " Main

End Class


 </source>


String.IndexOf(String substring)

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            Dim s3 As String = "Liberty Associates, Inc. provides "
            s3 = s3 & "custom .NET development"
            Dim s5 As String = String.Copy(s2)
            Console.WriteLine("s5 copied from s2: {0}", s5)
            Dim location As Integer = s3.IndexOf("provides")
            Dim s10 As String = s3.Insert(location, "usually ")
            Console.WriteLine("s10: {0}", s10)
  End Sub

End Class


 </source>


String.Insert

<source lang="vbnet"> Imports System Imports System.Collections Public Class MainClass

   Shared Sub Main()
            Dim s1 As String = "abcd"
            Dim s2 As String = "ABCD"
            Dim s3 As String = "Liberty Associates, Inc. provides "
            s3 = s3 & "custom .NET development"
            Dim s5 As String = String.Copy(s2)
            Console.WriteLine("s5 copied from s2: {0}", s5)
            Dim location As Integer = s3.IndexOf("provides")
            Dim s10 As String = s3.Insert(location, "usually ")
            Console.WriteLine("s10: {0}", s10)
  End Sub

End Class


 </source>


String.IsNullOrEmpty

<source lang="vbnet"> Option Strict On Imports System.Collections.ObjectModel Imports System.IO Public Module FileSystem

  Public Sub Main()
     Dim dirPath As String = "c:\\"
     If String.IsNullOrEmpty(dirPath) Then Exit Sub
     If Not Directory.Exists(dirPath) Then Exit Sub
     
     Dim dirs As ReadOnlyCollection(Of String) = My.ruputer.FileSystem.GetDirectories(dirPath)
     For Each dir As String In dirs
        Dim dirInfo As DirectoryInfo = New DirectoryInfo(dir)
        Console.WriteLine("{0} : Created {1}", dirinfo.Name,dirInfo.CreationTime)
     Next
  End Sub

End Module


 </source>


String.LastIndexOf

<source lang="vbnet"> Imports System Public Class MainClass

  Shared Sub Main()
     Dim letters As String = "abcdefghijklmabcdefghijklm"
     Dim searchLetters As Char() = New Char() {"c"c, "a"c, "$"c}
     Console.WriteLine("LastIndexOf to find a character in a string")
     Console.WriteLine("Last ""c"" is located at " & _
        "index " & letters.LastIndexOf("c"c))
     Console.WriteLine("Last ""a"" is located at index " & _
        letters.LastIndexOf("a"c, 25))
     Console.WriteLine("Last ""$"" is located at index " & _
        letters.LastIndexOf("$"c, 15, 5))
  End Sub " Main

End Class


 </source>


String.PadLeft(Int length)

<source lang="vbnet">

Public Class MainClass

  Public Shared Sub Main()
      "Strings
       Dim strConstant As String = "ABC"
       Dim strRepeat As New String("A"c, 20)
       Dim strMyString As String = "Hello World"
       Console.WriteLine(strMyString.PadLeft(30))
       Console.WriteLine(strMyString.PadLeft(20, "."c))
       
  End Sub

End Class


 </source>


String.PadLeft(Int length, String char)

<source lang="vbnet"> Public Class MainClass

  Public Shared Sub Main()
      "Strings
       Dim strConstant As String = "ABC"
       Dim strRepeat As New String("A"c, 20)
       Dim strMyString As String = "Hello World"
       Console.WriteLine(strMyString.PadLeft(30))
       Console.WriteLine(strMyString.PadLeft(20, "."c))
       
  End Sub

End Class


 </source>


String.Replace

<source lang="vbnet"> Imports System Public Class MainClass

   Shared Sub Main()
       "Declare variables
       Dim strData As String
       Dim strNewData As String
       "Get the text from the TextBox
       strData = "Hello You"
       "Replace the string occurance
       strNewData = strData.Replace("Hello", "Goodbye")
       "Display the new string
       System.Console.WriteLine(strNewData)
   End Sub

End Class


 </source>


String.Split

<source lang="vbnet"> Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "The important thing is not to " & _
           "stop questioning. --Albert Einstein"
       Dim strArray1() As String = Split(quote, "ing")
       Dim strArray2() As String = quote.Split(CChar("ing"))
       Dim counter As Integer
       For counter = 0 To strArray1.Length - 1
           Console.WriteLine(strArray1(counter))
       Next counter
       For counter = 0 To strArray2.Length - 1
           Console.WriteLine(strArray2(counter))
       Next counter
       
   End Sub

End Class


 </source>


String.StartsWith

<source lang="vbnet"> Imports System Public Class MainClass

  Shared Sub Main()
     Dim strings As String()
     Dim i As Integer
     Dim quotes As Char = ChrW(34)
     strings = New String() {"started", "starting", _
        "ended", "ending"}
     Console.WriteLine(" test every string to see if it starts with "st"")
     For i = 0 To strings.GetUpperBound(0)
        If strings(i).StartsWith("st") Then
           Console.WriteLine(quotes & strings(i) & quotes & _
              " starts with " & quotes & "st" & quotes )
        End If
     Next
     Console.WriteLine(" test every string to see if it ends with "ed"")
     For i = 0 To strings.GetUpperBound(0)
        If strings(i).EndsWith("ed") Then
           Console.WriteLine( quotes & strings(i) & quotes & _
              " ends with " & quotes & "ed" & quotes )
        End If
     Next
  End Sub " Main

End Class


 </source>


String.Substring(Int begin)

<source lang="vbnet">

Public Class MainClass

  Public Shared Sub Main()
      "Strings
       Dim strConstant As String = "ABC"
       Dim strRepeat As New String("A"c, 20)
       Dim strMyString As String = "Hello World"
       Console.WriteLine(strMyString.Substring(0, 5))
       Console.WriteLine(strMyString.Substring(6))
       
  End Sub

End Class


 </source>


String.Substring(Int begin, Int end)

<source lang="vbnet">

Public Class MainClass

  Public Shared Sub Main()
      "Strings
       Dim strConstant As String = "ABC"
       Dim strRepeat As New String("A"c, 20)
       Dim strMyString As String = "Hello World"
       Console.WriteLine(strMyString.Substring(0, 5))
       Console.WriteLine(strMyString.Substring(6))
       
  End Sub

End Class


 </source>


String.ToCharArray()

<source lang="vbnet">


Imports System Public Class MainClass

  Shared Sub Main()
     Dim string1 As String = "hello"
     Dim string2 As String = "GOOD BYE "
     Dim string3 As String = "   spaces   "
     Dim i As Integer
     Dim charArray() As Char = string1.ToCharArray()
     Console.WriteLine("string1 = " & string1 & vbCrLf & "string2 = " & _
        string2 & vbCrLf & "string3 = " & string3)
     For i = 0 To charArray.Length - 1
        Console.WriteLine(charArray(i))
     Next
  End Sub " Main

End Class


 </source>


String.ToLower()

<source lang="vbnet"> Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "AbCdEfG"
       Dim result As New System.Text.StringBuilder
       result.AppendLine("Original:   " & quote)
       result.AppendLine("Upper Case: " & quote.ToUpper())
       result.AppendLine("Lower Case: " & quote.ToLower())
       result.AppendLine("Mixed Case: " & MixedCase(quote))
       Console.WriteLine(result.ToString())
   End Sub
   Public Shared Function MixedCase(ByVal origText As String) As String
       Dim counter As Integer
       Dim textParts() As String = Split(origText, " ")
       For counter = 0 To textParts.Length - 1
           If (textParts(counter).Length > 0) Then _
              textParts(counter) = _
              UCase(Microsoft.VisualBasic.Left( _
              textParts(counter), 1)) & _
              LCase(Mid(textParts(counter), 2))
       Next counter
       Return Join(textParts, " ")
   End Function

End Class


 </source>


String.ToUpper()

<source lang="vbnet"> Public Class Tester

   Public Shared Sub Main
       Dim quote As String = "AbCdEfG"
       Dim result As New System.Text.StringBuilder
       result.AppendLine("Original:   " & quote)
       result.AppendLine("Upper Case: " & quote.ToUpper())
       result.AppendLine("Lower Case: " & quote.ToLower())
       result.AppendLine("Mixed Case: " & MixedCase(quote))
       Console.WriteLine(result.ToString())
   End Sub
   Public Shared Function MixedCase(ByVal origText As String) As String
       Dim counter As Integer
       Dim textParts() As String = Split(origText, " ")
       For counter = 0 To textParts.Length - 1
           If (textParts(counter).Length > 0) Then _
              textParts(counter) = _
              UCase(Microsoft.VisualBasic.Left( _
              textParts(counter), 1)) & _
              LCase(Mid(textParts(counter), 2))
       Next counter
       Return Join(textParts, " ")
   End Function

End Class


 </source>