Are strings any easier in the new C#?

I stopped using C# about a year ago. I hated the complexity of using strings. Gone were the days of MyString = "Hello"; I had to now make an object. String manipulation, which was once easy using MID, RTRIM, REPLACE, etc., now is tedious.

Anyway, I was wondering if the newest version of C# has made it any easier



Answer this question

Are strings any easier in the new C#?

  • lrryklly

    Can you go this :

    String s = "Hello World";

    //Now change it immedietely
    s = "My world";
    s = mygrid.text;

    It seemed like 's' was fixed in memory, and not easily changed.

    PS. is there a Split funtion


  • Titou

    Amen.  THAT is what I was saying!  All the first replies acted so amazed at the notion of Strings not being optimal in C#.  Thus I guess my question is answered.  Strings are still no better/optimized/easier than version 1.0
  • Michael W Powell

    Yes, those statements are valid. When you say "s was fixed in memory", perhaps you are referring to the fact that in the .Net world, strings are immutable. So when you do this:

    string s = "Hello World"; //Create a string

    s = mygrid.Text; //here, the original string is not altered. Instead a new string is created.

    That's why you would use a stringBuilder if you had many concatenations to do, especially in a loop. But for standard string manipulation, coding as you would intuitively is fine.

    String does have split:

    string s = "All work and no play";
    string[] words = s.Split(' ');




  • hueys

    actually, there is no difference with how strings are manipulated.

    The only difference is you are now aware of their immutability.

    String routines have never been optimal, the StringBuilder is the fix.

    string a = b +"world"

    doesnt need a string builder for optimization, but

    string s = "Hello";

    s = s + ", Brian";

    would benefit from a string builder.



  • N.Raja

    Yeah, I hear you.  I've run into resistance myself when I pose questions about the design of C# and the base framework. But I will agree with Blair's reply to you.  C# does not imply types for variables (it's not dynamically typed).  As for the VB string functions you're used to, Blair correctly pointed out the .NET framework equivalants.

     


  • Trubble

    Brian, thanks for your reply

     

    Blair, any dentists in your family


  • SanthoshGV

    none that I know of. . . why

  • Marcello1965

    No. It seemed that there was some 'catch' to manipulating strings in C#.

    I remember that I had to use StringBuilder class to manipulate strings.


  • Mondo327

    I'm still wondering why Microsoft couldn't have engineered String to have the same performance benefits of StringBuilder, perhaps with the help of the compiler. Can anyone enlighten me  

    I should add that I am aware that String objects are designed to be immutable, so it can't have certain methods that StringBuilder has, such as Append.  I can see it bringing elegance and maintainability to the code, but is it a worthwhile hard requirement  

    It would be interesting to know if the compiler could use lifetime analysis of strings and change them into stringbuilder instances, so that code like string a = b + " world"; followed by the kill of b lets the compiler reuse it as a stringbuilder type object

    Thanks.

    Brian


  • 勇敢的心

    Um, you couldnt do -

    string s = "Hello";

    what was so hard about -

    string s1 = s.Trim()

    string s1 = s.Substring(1,2);

    Its no different than in VB.Net. Unless you use the non CLR compilant VisualBasic assemblies;

    no different except that, in vb, declarations are more tedious -

    Dim s as String = "Hello"



  • Bruce T

    No, you never had to use StringBuilder, it's just more efficient in dealing with string manipulation. It's increased efficiency is minor when dealing with small strings and small amounts of manipulation, but has a big impact when doing large, complex manipulation.

    string s = "Hello" + " World";

    StringBuilder sb = new StringBuilder("Hello");

    sb.Append(" World");

    Both work fine. Change "Hello" and " World" to 2MB strings however and the StringBuilder would be far faster and consume way less RAM.



  • cyr2224

    What I meant by string routines never really being optimal, is they were never really optimal in any language/platform.

    I'll give you a couple of days to investigate what a string really is (a dynamic array of characters) and how direct manipulation is a poor performer.



  • Coder_John

    I think the OP must have misunderstood how strings worked in C#...



  • HsiaoI

    Yeah.  I still wonder if having the String/StringBuilder redundancy--particularly the cost of more complexity in the framework and missed performance opportunities--really outweighs the need to keep String immutable.  It's interesting that you say "StringBuilder is the fix" which reinforces my suspicion that String could have been better designed.
  • Are strings any easier in the new C#?