C# SubStrings
What Is It:
The "SubString" method gets certain characters from a string. You can get all of the text after a certain number of characters, or even just a certain number of characters. This is a useful method a lot of the time in programming. For example, I used this method quite a bit while making a Command Line Interpreter.
Getting Started:
The first thing we want to do is setup a string variable. If you do not know how to do this, please check out my variable tutorial!
Code
string hf = "hello hackforums";Next, we want to setup another string variable, but set it up to be a substring of the first variable.
Code
string hf2 = hf.Substring(5);This creates another string variable. It then says that it will be equal to a Substring of the first variable, (5) says that it is a substring of everything AFTER the fifth character.
So, since we know it will be all of the text after the 5th character, it should only grab the word "Hackforums". The reason it is 5 is because it accounts the space as a character.
So, lets setup a messagebox to display our text.
Code
MessageBox.Show(hf2);[IMG]http://i51.[censored].com/mshmo7.jpg[/IMG]
You can also setup parameters when working with the Substring method.
Code
string hf2 = hf.Substring(5, 9);Using (5, 9) says we want it to start at everything after the 5th character but only grab up to 9 characters.
[IMG]http://i51.[censored].com/30b35h0.jpg[/IMG]
Code:
Code
private void button1_Click(object sender, EventArgs e)
{
string hf = "hello hackforums";
string hf2 = hf.Substring(5, 9);
MessageBox.Show(hf2);
}http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


