I was wondering if anyone could give me a link to a site that has the list of string minipulation functions. I am in need of learning these, and i could not find them on my own. Also if you had a link to the char, the ones that make new lines and such.
Thanks in advance, Adam Botsford
Comments
: Thanks in advance, Adam Botsford
:
Here are examples of most of the STring and Char functions. But to learn more go to
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconprogrammingwithnetframework.asp
This will have everything u ever need to know about .NET. And what i demonstrate below with strings are the basics. There are so much more you can do, especially when Globalization is factored in, ect...
[code]
Dim S As String
Dim i As Integer
'DEMONSTRATES
' strVal.Chars
' strVal.Length
' Char.IsLetter
' Char.IsSpace
' Char.IsPunctuation
Dim letterCount As Integer = 0
Dim spaceCount As Integer = 0
Dim punctCount As Integer = 0
S = "This is a test."
For i = 0 To S.Length - 1
If Char.IsLetter(S.Chars(i)) Then
letterCount += 1
End If
If Char.IsWhiteSpace(S.Chars(i)) Then
spaceCount += 1
End If
If Char.IsPunctuation(S.Chars(i)) Then
punctCount += 1
End If
Next
' DEMONSTRATES
' strVal.StartsWith
' strVal.EndsWith
S = "--Test--"
If S.StartsWith("--") Then
' Do Something
End If
If S.EndsWith("--") Then
' Do Something
End If
' DEMONSTRATES
' strVal.IndexOf
' strVal.IndexOfAny
' strVal.LastIndexOf
' strVal.LastIndexOfAny
S = "Test1,Test2@Test3!Test4,test5"
If S.IndexOf(","c) <> -1 Then
' Index of the first "," was found
End If
If S.LastIndexOf(",") <> -1 Then
' Index of the second "," was found
End If
' IndexOfAny requires an array of Char's
Dim any() As Char = {"*"c, "@c, !"c}
If S.IndexOfAny(any) <> -1 Then
' Index of "@" was found because it occurs first
End If
' Here is a more concise version of the code above.
' NOTE: It takes advantage of initializing arrays on the fly.
If S.IndexOfAny(New Char() {"*"c, "@c, !"c}) <> -1 Then
' Index of "@" was found because it occurs first
End If
If S.LastIndexOfAny(New Char() {"*"c, "@c, !"c}) <> -1 Then
' Index of "!" was found because it occurs last
End If
' DEMONSTRATES
' strVal.Insert
S = "Jones"
S = S.Insert(0, "mr. ") ' S = "mr. Jones" now
' DEMONSTRATES
' strVal.Split
' String.Join
S = "Tom,Jones,34,Singer"
' Create an array of strings
Dim fields() As String = S.Split(","c)
' Rebuild The string
S = String.Join("*", fields) ' S = "Tom*Jones*34*Singer"
' HERE IS A PATTERN I USE ALOT
' Create an array no matter what.
' This minimizes coding.
If S.IndexOf(","c) <> -1 Then
fields = S.Split(","c)
Else
' Create an array on the fly
fields = New String() {S}
End If
' DEMONSTRATES
' strVal.PadLeft
' strVal.PadRight
S = "Value"
S = S.PadLeft(10, "."c) ' S = ".....Value"
S = "Value"
S = S.PadRight(10, "."c) ' S = "Value....."
' DEMONSTATES
' strVal.Remove
' strVal.Replace
' strVal.Substring
S = "Leap Dog"
S = S.Replace("Dog", "Frog") ' S = "Leap Frog"
S = "mr. Jones"
S = S.Remove(0, 4) ' S = "Jones"
S = "This is a test"
S = S.Substring(5, 2) ' S = "is"
' DEMONSTRATES
' strVal.ToLower
' strVal.ToUpper
S = "Tom Jones"
S = S.ToUpper ' S = "TOM JONES"
S = S.ToLower ' S = "tom jones"
' DEMONSTRATES
' strVal.Trim
' strVal.TrimStart
' srVal .TrimEnd
' Trim trims spaces by default
' So i'll demonstrate trimming other characters
@Test@@**--"
S = S.TrimStart(New Char() {"-"c, "*"c, "@c}) ' S = Test@@**--"
@Test**--"
S = S.TrimEnd(New Char() {"-"c, "*"c, "@Test"
@Test@@**--"
S = S.Trim(New Char() {"-"c, "*"c, "@c}) ' S = Test"
' DEMONSTRATES
' strVal = New String
' strVal.ToCharArray
' STRING OBJECTS SUPPORT 3 CONSTRUCTORS
' 1. Create a string from an array of Char's
' NOTE: That i am using ToCharArray for demo purposes
S = "Tom Jones"
Dim cArr() As Char = S.ToCharArray()
Dim s2 As New String(cArr) ' s2 = "Tom Jones"
' 2. Create a string from partial array of Char's
s2 = New String(cArr, 4, 5) ' s2 = "Jones"
' 3. Duplicate a string
s2 = New String("."c, 10) ' s2 = ".........."
' INITIALIZE A STRING
Dim s3 As String = String.Empty
' DEMONSTRATES
' String.Format
' The format function is the most robust
S = "{0} is a {1}"
S = String.Format(S, "This", "test") ' S = "This is a Test"
S = "The price is {0:c}"
S = String.Format(S, 2.5) ' S = "The price is $2.50"
' I recommend doing a little research on this method.
' Also read about FormatProviders
' CHAR Object - It supports all shared methods
' In Option Strict mode you must preix lietral strings ...
' with a lower-case "c" to signify that it is a Char".
Dim bTest As Boolean
Dim c As Char = "g"c
bTest = Char.IsDigit(c) ' False
bTest = Char.IsLetter(c) ' True
bTest = Char.IsLetterOrDigit(c) ' True
bTest = Char.IsLower(c) ' True
bTest = Char.IsNumber(c) ' False
bTest = Char.IsPunctuation(c) ' False
bTest = Char.IsSeparator(c) ' False
bTest = Char.IsSymbol(c) ' False
bTest = Char.IsUpper(c) ' False
bTest = Char.IsWhiteSpace(c) ' False
' PARSE A STRING TO A CHAR
S = "A"
c = Char.Parse(S) ' c = "A"c
' CONVERT BACK TO A STRING
S = c.ToString
' CONVERT CASE OF CHAR
c = "b"c
c = Char.ToUpper(c) ' c = "B"c
c = Char.ToLower(c) ' c = "b"c
' INITIALIZING CHAR
Dim c2 As Char = Char.MinValue
Dim c3 As Char = Char.MaxValue
[/code]
: Thanks in advance, Adam Botsford
:
Hello,
How do you arrange strings in an ascending order?
(input:dcba - output:abcd)
Thank you
: : Thanks in advance, Adam Botsford
: :
: Hello,
: How do you arrange strings in an ascending order?
: (input:dcba - output:abcd)
: Thank you
:
[code]
Dim names() As String = {"Tom", "Phil", "Andy", "Mike"}
Array.Sort(names) ' Ascending Order
Array.Reverse(names) ' Now Descending Order
[/code]
As a side note, all the base data types like string, integer, and double asll implement the IComparable interface. The Array Sort and Reverse methods understand this interface and Sort accordingly. If you want to be able to sort a custom class object, this is how you could proceed:
[code]
Public Class Person
Implements IComparable
Public FirstName As String = ""
Public LastName As String = ""
Sub New(ByVal firstName As String, lastName As String)
Me.FirstName = FirstName
Me.LastName = LastName
End Sub
Public ReadOnly Property FullName() As String
Get
Return FirstName & " " & LastName
End Get
End Property
Public Function CompareTo(ByVal obj As Object) As Integer _
Implements System.IComparable.CompareTo
If obj Is Nothing Then Return 1
Dim compare As Person = CType(obj, Person)
Return String.Compare(Me.FullName, compare.FullName, True)
End Function
End Class
[/code]
The Array function will sort Person objects now.
[code]
Dim people() = { New("Tom", "Jones"), New ("Andy", "Smith") }
Array.Sort(people)
Array.Reverse(people)
[/code]