1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Programming Tutorial - About Strings and Chars

By , About.com Guide

8 of 10

Some More String Functions and Methods

The String class provides just about everything you could possibly need, and many of the methods are overloaded to allow different types. Take Concat- it works just like + except you can have more than two parameters and one can be an object which means you can concat anything to a string. This, taken from Ex5 shows an int literal, a double literal and an int object all being concatted.
string a="David ";
int b = 0;
a= string.Concat(a,4,5.6,b) ;
Console.WriteLine("a= {0}",a) ;

The output is a= David 45.60
Download Example Ex5

The Contains and IndexOf Methods

This is a simple boolean check if one string is contained within another. Continuing with Ex5, this example uses the string a defined earlier.
if (a.Contains("45.6"))
Console.WriteLine("{0} contains 45.6",a) ;
In this case it does, so the above string is output but more often you want to know the exact index of where the string is contained. For this you use IndexOf() which has many overloads. Use it to return the position of a char in a string or even specify the type of search through the StringComparison numerations. These provide a comprehensive handling of Culture searching (both case sensitive and insensitive) which is the .NET name equivalent of locale. We'll come back to that in a later tutorial.
int pos = a.IndexOf(".6") ; Console.WriteLine("position of .6 is {0}", pos) ;
This return 8, remember in C# the first char in a string is at position 0.

On the next page - More String Methods

Explore C / C++ / C#
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. Some More String Functions and Methods

©2009 About.com, a part of The New York Times Company.

All rights reserved.