Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# SubStrings [Intermediate Level Tutorial]

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.
Ah, I have never worked much with substrings, I would either use a splitter for this, because, when u can save datas into files eg. and use the data easier, this will only come handy in special sections.
Ive been using splitters for most things, where I was gonna get things inside a string.
Eg. for my 2d mmo that I was working on, but quitted long time ago.
Then I wass using it for getting items in inventory and equipments.
Like:
name~level~damage~class
and where ~ was the splitter.
Also splitters could be used as space, then just write " ", but only useful in some cases, if ur reading from ini or writing to ini, then using a sign is better, but good little guide for substrings :)
Yeah, a lot of the time it may be difficult to find an instance that you need to use the substring method. Nevertheless, opportunities present themselves.
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.
They would be good if ur gonna get a port or a special number in an ip.
Or something, but they aren't really used much.
I dont think I have used Substrings ever :P
Good job, Substring is very useful function and this should help many people.
(Oct 22, 2010 02:44 PM)tflo Wrote:  Good job, Substring is very useful function (IN VERY VERY SPECIAL CASES) and this should help many people.

fixed ^

It is useful, but it is only in very special cases.
As I stated above, using Splitters are much better and much more efficient.
(Oct 22, 2010 02:44 PM)tflo Wrote:  Good job, Substring is very useful function and this should help many people.

Thanks for the comment Vanquish, glad to see you are keeping up to date with my tuts :3
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 211 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017