Results 1 to 9 of 9
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Best way of doing strings in VB / C# / C++ / Net?

    Hi all,

    What is the latest and greatest way of doing strings in VB / C++ / C# / Net.

    Using (Visual Express 2008.

    I am designing an "Intro to Programming" course for tech writers.

    I am writing sample code in a number of languages.

    Pray tell me what is the latest and greatest way of doing strings in VB / C++ / Net?

    Is it the .Net class, String, as in:

    http://msdn.microsoft.com/en-us/libr...em.string.aspx

    ?

    Once upon a time, VB did strings its own way, C did strings in its own way, C++ did strings in its own way.

    Now that .Net has come along, they are all supposed to do it in the same way. Right?

    And how about this article:
    "New Recommendations for Using Strings in Microsoft .NET 2.0"
    http://msdn.microsoft.com/en-us/library/ms973919.aspx

    Does the Recommendation change between .Net 2.0 and .Net 3.5?

    Tia

    - avi

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Best way of doing strings in VB / C# / C++ / Net?

    I'm not sure I understand your question. Doing what with strings? In .Net, regardless of the language, strings are objects. While you can manipulate them as you do in VB, etc., what appears to be happening isn't really what's going on. If you write something like

    myString = "ABC"

    You've assigned "ABC" to the string variable. But if you then write:

    myString = myString & "123"

    you aren't just concatenating more characters to the string, you're actually creating a new object that contains the value of the old object plus the new text.

    The "preferred" method is (again in VB.Net) String.Format if you're building a single string with replaceable values. If you need to build a string in pieces, possibly based on conditions, use a System.Text.StringBuilder, which has append and appendformat methods that allow you to build a long string by appending to it. Note, though, the the StringBuilder object is not a string, so you have to use its ToString method to convert it.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Best way of doing strings in VB / C# / C++ / N

    Great thanks, Charlotte!

    (Excuse me, but it often takes me this long to respond. I am a freelancer with multiple clients/projects and sometimes I just stop in the middle ... and come back later.
    It's just an issue of timescale...
    )

    If I was MS, I would have called StringBuilder, as described, by the name "String"
    and String, as described, by the name "StringDumb".

    Tnx again,

    - avi

  4. #4
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Best way of doing strings in VB / C# / C++ / N

    I am looking ta the members, and I do not see a Find function to find a substring withing the string.

    In VB6 there was InStr.

    In general, I seem to note that there are fewer string ops in StringBuilder than I remember in VB6 String type.

    Thanks again,

    - avi

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Best way of doing strings in VB / C# / C++ / N

    You can use Instr, just as you did in VB/VBA. Or you can use RegEx. StringBuilder is just what it says, an object that allows you to build a string without creating a new object with every addition to the string. You can also use the Concat method of a string object, but it's far less flexible than StringBuilder. What you use depends on what you're doing with it. There aren't really any cut and dried answers in .Net because there are so many ways you could do something.
    Charlotte

  6. #6
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Re: Best way of doing strings in VB / C# / C++ / N

    Thanks.

    - avi

  7. #7
    New Lounger
    Join Date
    Dec 2009
    Location
    Massachusetts USA
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In C and in "straight" C++, strings are not-so-simple-to-use character arrays. However, many C++ developers use the Microsoft Foundation Classes (MFC), which allows for the much simpler use of CStrings (which are almost as easy to use as strings are in VB, I'd say).

  8. #8
    Lounger
    Join Date
    Apr 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by amakeler View Post
    Once upon a time, VB did strings its own way, C did strings in its own way, C++ did strings in its own way.

    Now that .Net has come along, they are all supposed to do it in the same way. Right?
    You need to watch out for NULL terminated strings. VB.Net in particular doesn't like NULL terminated strings - remove the NULL before using them otherwise you'll get some strange results - anything you append AFTER the NULL won't appear at all. This includes things like carriage returns.

    Thanks

  9. #9
    New Lounger
    Join Date
    Jan 2010
    Location
    Plano, TX
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by amakeler View Post
    I am looking ta the members, and I do not see a Find function to find a substring withing the string.

    In VB6 there was InStr.

    In general, I seem to note that there are fewer string ops in StringBuilder than I remember in VB6 String type.

    Thanks again,

    - avi
    You might have use for an alternative method. Consider the following code fragment.

    Dim MyString As String = "Every good boy does fine."
    Dim Index As Integer = MyString.IndexOf("good")
    Dim BadIndex As Integer = MyString.IndexOf("bad")
    Dim GoodString As String = MyString.Substring(Index, 4)

    After the above executes, variables will have the following values:
    MyString = "Every good boy does fine."
    Index = 6
    BadIndex = -1
    GoodString = "good'
    For complex string manipulations, StringBuilder is highly efficient but unless you're doing a lot of string manipulation, working with the String class directly is plenty good enough. It is vastly more efficient than the old VB6 string management. So, it's perfectly OK to write:

    For i As Integer = 1 To 50
    MyString = MyString & " " & i.ToString()
    Next

    and it will execute pretty darn quick. However, when (i) starts getting large, StringBuilder is the better choice.

    Lastly, there are a lot functions in .Net that are there simply to make porting old VB code easier. You will find InStr(), InStrRev(), CStr(), CInt(), etc. If you're writing new code, try not to use them. Instead, get used to String.IndexOf(), String.LastIndexOf(), Integer.ToString(), Integer.Parse(), and so on. You'll be better off.

    Good luck.

Posting Permissions

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