A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
All about stringsHow to compare two strings, reverse a string, search in a string and many other techniques, each with examples. |
On Saturday, May 15th 2004 at 03:29 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 3.9 with 16 votes) |
||
Finding the length of a stringThe easy way to find the length of a string in C# is using the 'Length' property. Length of the string includes spaces and all the special characteres.
Reversing a stringWe reverse a string by taking each character in that string and reversing its position. We do this with the help of the string index that is similar to an array.
Comparing stringsThere are several ways to compare a string. Although for sorting strings using method CompareTo() is probably the best way, because it returns 0 if the values are equal, -1 if the first value (that invokes) is smaller than the second value and 1 if the first value is bigger than the second value.
IndexOf and LastIndexOfThe position of a character / string in a stringMethod IndexOf returns the index of a searched character in a string. For example if we searched for character 'e' in the string 'This is the string in which we search.' IndexOf would return 10. You may say... hmm... 'e' is character number 11 in the string, not 10. I would reply - you are still not used to counting from 0? Let's see the code now.
String StartsWith and EndsWithCreate a new Windows Application project 'stringStart'. Add to it a TextBox 'txtBegin' and a ListBox 'listWords'. The following is the complete code that should make the program work. The comments make the code self-explanatory.
SubStringI brought you an example with self-explanatory code, again :
ToUpper() and ToLower()Converts a string all to uppercase or lowercase. So simple to use that I didn't want to mention it in the first place.
TrimTrim cuts the empty space at the beginning and at the end of a string. Using trim you can clean a string like " I'm going to be trimmed ", the result beeing "I'm going to be trimmed".
ReplaceYou can easily replace each occurence of a character or string in a string using method Replace():
|
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsExcellent tutorial. Great examples. Thank you.
hello sir
i just want to say that
change event of text box the select item of list box will change
hi sir,I want to print double forward slash as a path for a server like https://.....how can I save // to seem lıke comment???
how can you forget how to search a string!?!??!?!
OK, REVERSING A STRING IS FOR PRIMITIVE PEOPLE. I have one question how you reverse whole document. Oops don't tell convert document into a string and then reverse. It eats up lot of memory!!!
I am looking this functionality with a minimal code..
Using a pointers ? guys any idea ?
What kind of document? Word? Or simply a text file?
"I have one question how you reverse whole document."
I already done this with Wave file to hear the reverse (preserving headers).
You need to create a new file, open your source file with random access, read your source file block by block, starting from the end (using seek), reversing each block, and writing it to your new file.
hi the article was excellent and i exactly got the thing that i was searching for the past 4hrs.
very very nice article on strings boss,really its very very good,keep it up
can any1 tel me what can be the maximum length of a string in c#.net ?
There is no limit set for the string datatype. As long as you store a decent amount of characters in that string (less than 10,000), you shouldn't worry about slowing down the application, either.
For more information on C# data types, see <a href="http://www.geekpedia.com/tutorial29_Csharp-data-types.html">the C# data types tutorial</a>.
// You can have some fun with version 3.0,
// add the following class and you automatically
// get a .Reverse() method on all your strings.
public static class StringExtensions
{
public static string Reverse( this string input )
{
string result = null;
if ( input != null )
{
var output = new char[input.Length];
var index = output.Length;
foreach ( char character in input )
{
output[--index] = character;
}
result = new String( output );
}
return result;
}
}
Crap... I\'m looking for any char (?,x) or any string (*) and can\'t find a shit...
i would like to know how to find the length of the string without using the length function..
i would like to know how to find the length of the string without using the length function..
Related Tutorials
Related Source Code
C# Job Search