Hack Forums
[C#] C# SubStrings [Intermediate Level Tutorial] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] C# SubStrings [Intermediate Level Tutorial] (/showthread.php?tid=758624)



C# SubStrings [Intermediate Level Tutorial] - Pulser - Oct 22, 2010

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);

        }



* - BaussHacker - Oct 22, 2010

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 :)


* - Pulser - Oct 22, 2010

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.


* - BaussHacker - Oct 22, 2010

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


* - tflo - Oct 22, 2010

Good job, Substring is very useful function and this should help many people.


* - BaussHacker - Oct 22, 2010

(Oct 22, 2010 02:44 PM)Vanquish 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.


* - Pulser - Oct 22, 2010

(Oct 22, 2010 02:44 PM)Vanquish 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