C# Fundamentals: Development for Absolute Beginners

Working with Strings - 12

Download this episode

Download Video

Download captions

Download Captions

Description

Since often times in our applications we'll want to work with string data, this lesson will approach a number of different string manipulations, whether changing the manner in which it is displayed to the end user through String.Format() and the formatting syntax like {0:C} or through using escape sequences to include characters  otherwise reserved by C# for other purposes. We'll look at built-in String methods to manipulate the content inside of a literal string, and will look at the StringBuilder class for concatenating many strings together in a memory and resource friendly manner.

 

Download the source code for Working with Strings

Tags:

C#, Visual Studio

Embed

Format

Available formats for this video:

Actual format may change based on video formats available and browser capability.

    The Discussion

    • Rob Cade

      Great videos - we're recommending them to our students. Thanks.

      Those are BACK-slashes, though, aren't they?

      Rob

    • Rob G

      I'm really enjoying learning from this series. Very well presented.

      I'd just like to point out that the description and link on this page refer to the next video.

    • BobTabor

      @Rob ... Oops .. let's hope that's my ONLY mistake.  Wink

    • Arwen

      Just to point out, the description of this video is the same as video 13. It doesn't really apply to video 12.

      On another note, great video!

    • Naqi Agha

      Sir in this series do have you covered the complete concepts of object oriented programming too?? Like the tutorial on www.learnvisualstudio.com??

    • BobTabor

      @Arwen: Thanks for the heads up ... I'll make sure the good folks at Channel9 see this issue.  Thanks again!

      @Naqi Agha: Object Oriented Programming (beyond the absolute basics) was NOT covered in this Channel9 Fundamentals series.  I do talk about classes, properties, methods, and inheritance in this series, but I don't really address the OOP thought process here.  Sorry, it just wasn't in the scope of what the Channel9 team was looking for.

    • thens

      @BobTabor: Hi Bob, Won't we get intellisense inside the formatting string for currency, numeric and etc, after pressing the semicolon like {0: ? 

    • Jose Roberto

      Hello Bob,
      the link of code of lesson 12 target to code of lesson 13.

    • jake123

      Hi Bob,

      I love your style and I can easily follow you. These videos are excellent especially for a beginner like me in C#. Can you kindly direct me to a lesson which will teach me how to read html tables from a webpage and then use these numbers in further calculations. Does a string become a number?

      Thxs

      J

    • BobTabor

      @thens: Probably so ... Intellisense should guide you (even if it didn't show up in the video I created ... ??)

      @Jose Roberto: Unfortunately that's not something I can fix ... you will need to get Microsoft's attention by clicking the Feedback link in the Footer of this site so they can repair the links.  Thanks for the heads up.  Smiley

      @jake123: Thanks for the kind words ... I don't have a free lesson on scraping a website, but I know there are a bunch of articles out there on the web that describe the technique.  It definitely can be done.  Search for terms like "web page scraping C#" or the like.  Best wishes!

    • bill

      When I download the source code I get Date and Times not Strings??

    • BobTabor

      @bill: Thanks, that is something Microsoft / Channel 9 will need to fix.  I'll bring that to their attention.  Thank you again.

    • golnazal

      the source code should be fixed. thanks for letting me know.

    • Krati

      http://blogs.msdn.com/b/csharpfaq/archieve/2004/03/12/88415.aspx

      is not acessible

    • Krati

      is there a way to trim the spaces in between words?
      say "hello world"

    • BobTabor

      @Krati: Bummer ... I hate when that happens.  Try searching in your favorite search engine for the keywords ... I'm sure they just moved it somewhere new.

      re: Spaces in between words ... there are probably several ways to accomplish this.  I'm not in a code editor, but you would do something like this (syntax might be off a bit):

      var myNewString = myOldString.Replace("  ", " ");

      Note ... I would send in the "bad" string (with two spaces between double-quotes) as the first parameter, and the "good"string (with one space between double-quotes).

      If you need to be more selective, you might need to turn the entire string into a char array and loop through every character in the string looking for a sequence that fits the criteria you need.  That would allow for a pretty advanced case, but would be code intensive.  Hope that helps!

    • Raviteja varma

      it is my pleasure writing you,really your video lectures helped me a lot.thanks a lot.

    • Chomneau

      Thanks Bob for This lesson...I learn somethings.

       

    • sebastiaan hilbers

      Wow this is helping me learn c# so much. I am coming from PHP and recognizing alot of similarities.

    • Ryan

      Hah, funny. If for whatever reason you want 2 backward slashes you'd have to type c:\\\\. If you just input 3, you get the squiggly line.

    • Rasika

      Thanks a lot Bob,This is helping me a lot :)

    • Asdilo

      This series is a lot of help. Thank you and your team.

       

    • Jonagesh

      The below code is not working for me.

      
      StringBuilder myString = new StringBuilder();
      for (int i = 0; i < 50; i++)
      {
      myString = myString +"--"+i.ToString();
      }
      


      Getting below error:
      Cannot implicitly convert type 'string' to 'System.Text.StringBuilder'

    • Jonagesh
    • Varun

      Hi Bob,

      I am a beginner and find your videos extremely informative and helpful.
      I have a doubt regarding the way while loop functions, how is the while function able to move onto the next line in the txt file with out us telling it to. I mean in the for loop we have to increment the value everytime the loop finishes and so it moves on to the next.

    • Yunus

      Thanks a lot for your valuable tutorials!

    • Ramisat

      @Jonagesh, you appear to have written wrong. You should have it like below:

      StringBuilder myString = new StringBuilder();
      for(int i = 0; i < 50; i++)
      {
      myString.Append("--");
      myString.Append(i);
      }

    • Lyx

      Bob, thanks for the great tutorials for C#, very helpful for beginners like me.

      For some reasons, VS 2010 accepts both

      myString = String.Format("Length Before: {0} -- After: {1}", myString.Length, myString.Trim().Length);

      and

      myString = string.Format("Length Before: {0} -- After: {1}", myString.Length, myString.Trim().Length);

      (the String.Format in the first line of code is in block letter S; the string.Format in the second line of code is in small letter s)

      However, VS2012 only accepts the second line of code. I also notice that string and String are interchangeable sometimes.

    • jaymz7783

      So the string formatting isn't just for string values...... That is confusing considering numbers, phone numbers and currency aren't string values.

    • Najeeb Anwer

      The description of this video does not match the content of the video. It is identical to video #13, where it is correctly describing the content of that video.

    • BobTaborFan

      umm can't we use these substring, append functions on string data type variable? Why are we using StringBuilder class to perform these actions on strings?

    • Clint

      @BobTaborFan: String are immutable objects.  That means every time you do something to it, like string foo = foo + "bar"; you actually create a new string rather than altering the original!  This can become costly and actually become a performance hit.  StringBuilder is a class that prevents that scenario from happening.

    • Erik

      So, if I wanted to do some additional formatting, and not have the ugly "--" before the 1 - 99 string, how can I call substring? When I try it here, I get errors:

      Console.WriteLine(myString.Substring(3, myString.Length));
      Console.ReadLine();

      Error 1 'System.Text.StringBuilder' does not contain a definition for 'Substring' and no extension method 'Substring' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?) c:\users\edarl_000\documents\visual studio 2012\Projects\ConsoleApplication5\ConsoleApplication5\Program.cs 32 40 ConsoleApplication5

      Thanks

    • Clint

      @Erik: depends on what types of additional formatting you want to do.  StringBuilder does have a .ToString(int startIndex, int length) property which is basically substring.  Remember to do some stuff certain ways, you need to craft it back into something that is useable or build the tools to do what you want.

    • lasseran

      I'm just trying to learn C# and these videos are great! Now I faced something I couln't find an answer to. Line 19 in your example

      string myString = string.Format("{0:C}", 123.45);

      When you run it you get $123.45 as expected.

      When I run the sample I get 123.45 ? Why is the exclamation mark there and the string is not formatted properly?

      I'm using VS2008 or VS2012 Express - the same result.

      TIA

    • lasseran

      I was  maybe too hasty when asking about the formatting. It is according the settings in Windows except it doesn't show the right currency symbol - €. How do I get that fixed?

      TIA

    • Clint

      @lasseran: I did a quick test here and what I saw was the default console font lacks the Euro symbol.  If I did a breakpoint and looked at the output from within VS, it did have the 123.45 €

      You can flip your console's default font if you want or change your Console.WriteLine to Debug.Writeline where it outputs to the output window from with-in Visual Studio and you'll get the proper symbol too.

      How I forced from en-US to a country with the Euro:

      Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-FR");
      
      string myString = (123.456).ToString("C");

    • lasseran

      @Clint:Thank you

    • LFurtado

      Awesome videos! Really. It helps a lot to understand basic concepts that I had learned only to accept as "that is how it is".

      So far I had found no beginner material that could compare to yours.

      Thanks to help us learn how to code, and how to code right!

    • som chaudhary

      Hi..Bob ..thank u very much for video ..i have been listening your video since last month which really helped me to learn c#program..again thanks for giving us this kind of service..But after a month i realized that it would be more effective if we will have some homework or exercise at the end of the video....dont mind Bob its my just personal request and kind of suggestion as well ..by the way i m not trying to smart anyway ...if u have something for exercise plz plz it will be appreciated ..thank u Bob..u have really wonderful teaching style:):)

    Comments closed

    Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.