Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
Sign in to follow this  
formlesstree4

Lesson 5: String.Format()

Recommended Posts

One of the most useful formatting abilities that VB.net can offer is "String.Format()"

 

String.Format() allows us to take a group of variables and put them however we want.

 

For example:

String.Format("{0} {1}", "Hello", "there!")

That was a very..poor example of how to use String.Format().

When this is parsed in VB.net, this is the outcome: "Hello there!"

 

This is especially useful when creating dynamic strings for later usage, such as when you're in a 'loop' (more on loops in another lesson).

 

A perfect example is when someone asks you for a name. Let's say you have a Textbox on the form called "Textbox1", and you wanted to display the name in a greeting. Normally, you'd have to resort to this:

Messagebox.show("Hello there " & TextBox1.text & "! How are you today?")

That can look rather nasty, but String.Format() allows us to clean that up a little bit:

Messagebox.show(String.Format("Hello there '{0}'! How are you today?", TextBox1.Text))

 

That will display the exact same thing! Why is that though? String.Format() takes a minimum of two arguments:

String.Format("This is where the sentence goes with these: {0} - {1}", "This will replace the {0}", "This will replace the 1")

 

The first sentence is the layout of your string. The {0} represents the first variable that will be replaced in the arguments list. Whatever is after the sentence, A.K.A., the 1st Argument, will be used in there. The {1} will be used by the 2nd Argument, and so on. I have yet to see a limit on the String.Format()'s array, so you can have a string that would count up to '{1000}', and I think it'll work...but if you have 1000 variables or so to replace, then you're obviously doing something wrong :D

 

In the next lesson, I will show you how to combine String.Format() and a loop to show every single file in a specified directory! But for now, I have a small homework assignment [for those that actually will care to do them]. Make a simple program that will say hello to a person [Name is taken from a TextBox], and display their age [Also taken from a TextBox].

Share this post


Link to post
Share on other sites

Yay! Thanks for writing this! *reads*

 

Can you provide the homework assignments from the previous lesson when you write a lesson? That way we can see what we did wrong.

Share this post


Link to post
Share on other sites

I'll make a sticky with homework assignments for each lesson. They'll be a little different than what you've been originally assigned, and I might even include links to the finished product so you can compare.

Share this post


Link to post
Share on other sites

Yeah that's what I want, the finished product.... 'Cause I'm a noob. ^_^

 

Thanks again!

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...