Results 1 to 9 of 9

Thread: work with string......

  1. #1

    Thread Starter
    Fanatic Member manhit45's Avatar
    Join Date
    May 2009
    Location
    Ha noi - Viet Nam
    Posts
    826

    work with string......

    Hi every one. This write will mention to string which we frequently meet it.
    ( i collected on the internet)


    How to VB.NET String.Length()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            str = "This is a Test"
            MsgBox(str.Length())
        End Sub
    End Class
    How to VB.NET String.Insert()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = "This is VB.NET Test"
            Dim insStr As String = "Insert "
            Dim strRes As String = str.Insert(15, insStr)
            MsgBox(strRes)
        End Sub
    End Class
    How to VB.NET String.IndexOf()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            str = "VB.NET TOP 10 BOOKS"
            MsgBox(str.IndexOf("BOOKS"))
        End Sub
    End Class
    How to VB.NET String.Format()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim dNum As Double
            dNum = 32.123456789
            MsgBox("Formated String " & String.Format("{0:n4}", dNum))
        End Sub
    End Class
    How to VB.NET String.Equals()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str1 As String = "Equals"
            Dim str2 As String = "Equals"
    
            If String.Equals(str1, str2) Then
                MsgBox("Strings are Equal() ")
            Else
                MsgBox("Strings are not Equal() ")
            End If
        End Sub
    End Class
    How to VB.NET String.Copy()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str1 As String
            Dim str2 As String
    
            str1 = "VB.NET Copy() test"
            str2 = String.Copy(str1)
    
            MsgBox(str2)
        End Sub
    End Class
    How to VB.NET String.CopyTo()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str1 As String = "CopyTo() sample"
            Dim chrs(5) As Char
            str1.CopyTo(0, chrs, 0, 6)
            MsgBox(chrs(0) + chrs(1) + chrs(2) + chrs(3) + chrs(4) + chrs(5))
        End Sub
    End Class
    How to VB.NET String.Contains()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String
            str = "VB.NET TOP 10 BOOKS"
            If str.Contains("TOP") = True Then
                MsgBox("The string Contains() 'TOP' ")
            Else
                MsgBox("The String does not Contains() 'TOP'")
            End If
        End Sub
    End Class
    How to VB.NET String.Compare()


    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str1 As String
            Dim str2 As String
    
            str1 = "vb.net"
            str2 = "VB.NET"
    
            Dim result As Integer
    
            result = String.Compare(str1, str2)
            MsgBox(result)
    
            result = String.Compare(str1, str2, True)
            MsgBox(result)
        End Sub
    End Class
    How to VB.NET String.Clone()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = "Clone() Test"
            Dim clonedString As String
            clonedString = str.Clone()
            MsgBox(clonedString)
        End Sub
    End Class
    How to VB.NET String.Chars()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim str As String = "Returned Character Chars()"
            Dim singleChar As Char
            singleChar = str.Chars(3)
            MsgBox(singleChar)
        End Sub
    End Class
    How to VB.NET String.substring()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim str As String
            Dim retString As String
            str = "This is substring test"
            retString = str.Substring(8, 9)
            MsgBox(retString)
    
        End Sub
    End Class
    How to VB.NET String.Split()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim str As String
            Dim strArr() As String
            Dim count As Integer
            str = "vb.net split test"
            strArr = str.Split(" ")
            For count = 0 To strArr.Length - 1
                MsgBox(strArr(count))
            Next
    
        End Sub
    End Class
    How to VB.NET String.EndsWith()
    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, 
    		ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim str As String
            str = "VB.NET TOP 10 BOOKS"
            If str.EndsWith("BOOKS") = True Then
                MsgBox("The String EndsWith 'BOOKS' ")
            Else
                MsgBox("The String does not EndsWith 'BOOKS'")
            End If
    
        End Sub
    End Class


    How to VB.NET String.Concat()

    Code:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, _
    	ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim str1 As String
            Dim str2 As String
    
            str1 = "Concat() "
            str2 = "Test"
            MsgBox(String.Concat(str1, str2))
    
        End Sub
    End Class
    Last edited by manhit45; May 21st, 2009 at 08:57 AM.
    --***----------***-----

    If i help you please rate me.

    Working with Excel * Working with String * Working with Database * Working with array *

    K51 ĐH BÁCH KHOA HÀ NỘI - Khoa CNTT pro.

  2. #2
    New Member
    Join Date
    May 2009
    Posts
    1

    Re: work with string......

    That is good. Thanks...

  3. #3
    Addicted Member
    Join Date
    Jun 2008
    Location
    Macedonia
    Posts
    188

    Re: work with string......

    Very good. Thanks

  4. #4
    New Member
    Join Date
    Aug 2009
    Posts
    15

    Re: work with string......

    Nice, repped for this. Good work.

  5. #5
    Lively Member
    Join Date
    May 2008
    Posts
    113

    Re: work with string......

    Great post, very handy.
    Thanks

  6. #6
    New Member
    Join Date
    Dec 2007
    Posts
    11

    Re: work with string......

    pardon me for getting you into trouble, but how would you do this?
    in C++ str1[5] = "s" (im asigning "s" to the fifth (or six indeed) element in string)
    but in vbasic str1(5) = "s" is not posible!!!
    x_x how is posibble that in a higher level language a basic operation like that cant be posible in a simple way?!!! x_x
    Last edited by rataro; Jun 25th, 2010 at 11:51 AM.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: work with string......

    The reason is because C doesn't have the string data type. Strings are an array of character, then end of which is signified by a NULL termination character.

    That's why str1[5] works in C/C++ ... you're accessing the 6th element (it starts with 0) of the character array. VB doesn't represent strings as an array of characters, so what you are doing in the simplest sense, doesn't work.

    Not to mention that strings are immutable... meaning any change to the string results in a new string being created.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: work with string......

    Quote Originally Posted by rataro View Post
    pardon me for getting you into trouble, but how would you do this?
    in C++ str1[5] = "s" (im asigning "s" to the fifth (or six indeed) element in string)
    but in vbasic str1(5) = "s" is not posible!!!
    x_x how is posibble that in a higher level language a basic operation like that cant be posible in a simple way?!!! x_x
    While we may not be able to do that easily with a single call, it's also not that hard to assign a single char within a string either:
    Code:
    Dim Str As String = "12345"
    Dim Chars() As Char = Str.ToCharArray()
    Chars(2) = "a"c
    Str = Chars
    That being said, it's extremely rare when I need to assign a single char at a specific location within a string. Usually I'm replacing a certain char (or a string) within a string regardless of it's location.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  9. #9
    New Member
    Join Date
    Dec 2007
    Posts
    11

    Re: work with string......

    Thanks for the answers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width