Strings

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

Declaration of string

1.
2.

strings are objects.


string is a reference type.

1.

static string Copy(string str)

2.

int CompareTo(string str)

3.

int IndexOf(string str)

4.

int LastIndexOf(string str)

5.

string ToLower( )

6.

string ToUpper( )

A string is a set of characters enclosed by


double quotes. For example,
"this is a test"

is a string.
using System;
class MainClass
{
static void Main(string[] args)

{
string MyString = "Hello World";
string Path = @"c:\Program Files";
string Path2 = "c:\\Program Files";
string Name = "Joe";
}

String is object
using System;
class MainClass
{
static void Main(string[] args) {
string strOriginal = "Original String";
Console.WriteLine( "Value of strOriginal before call: {0}", strOriginal );
TryToAlterString( strOriginal );
}

Console.WriteLine( "Value of strOriginal after call: {0}", strOriginal );

static void TryToAlterString(string str) {


str = "Modified String";
}
}

Empty string
using System;
using System.IO;
using System.Text;
public class MainClass
{
public static void Main(string[] args)
{
string address = String.Empty;
}
}

Use Indexer to reference chars in a string


using System;
class MainClass
{
public static void Main()
{
string myString = "To be or not to be";
for (int count = 0; count < myString.Length; count++)
{
Console.WriteLine("myString[" + count + "] = " + myString[count]);
}
}
}

Use the Compare() method to compare


strings
using System;
class MainClass
{
public static void Main()
{
int result;
result = String.Compare("bbc", "abc");
Console.WriteLine("String.Compare(\"bbc\", \"abc\") = " + result);
result = String.Compare("abc", "bbc");
Console.WriteLine("String.Compare(\"abc\", \"bbc\") = " + result);
result = String.Compare("bbc", "bbc");
Console.WriteLine("String.Compare(\"bbc\", \"bbc\") = " + result);
result = String.Compare("bbc", "BBC", true);
Console.WriteLine("String.Compare(\"bbc\", \"BBC\", true) = " + result);
result = String.Compare("bbc", "BBC", false);
Console.WriteLine("String.Compare(\"bbc\", \"BBC\", false) = " + result);
result = String.Compare("Hello World", 6, "Goodbye World", 8, 5);

Console.WriteLine("String.Compare(\"Hello World\", 6, " + "\"Goodbye World\", 8, 5)


}
}

Use the Concat() method to concatenate


strings
using System;
class MainClass
{

public static void Main()


{
string myString4 = String.Concat("A, ", "B");
Console.WriteLine("String.Concat(\"A, \", \"B\") = "+ myString4);
string myString5 = String.Concat("A, ", "B, ", "and countrymen");
Console.WriteLine("String.Concat(\"A, \", \"B, \", " + "\"and countrymen\") = " + m
}
}

Use the addition operator (+) to


concatenate strings
using System;
class MainClass
{
public static void Main()
{
string myString6 = "To be, " + "or not to be";
Console.WriteLine("\"To be, \" + \"or not to be\" = " + myString6);
}
}

Use the Copy() method to copy a string


using System;
class MainClass
{
public static void Main()
{
string myString4 = "string4";
Console.WriteLine("myString4 = " + myString4);
Console.WriteLine("Copying myString4 to myString7 using Copy()");
string myString7 = String.Copy(myString4);
Console.WriteLine("myString7 = " + myString7);
}
}

Use the Equals() method and equality


operator to check if two strings are
equal
using System;
class MainClass
{
public static void Main()
{
bool boolResult;
string myString = "str";
string myString2 = "str2";
boolResult = String.Equals("bbc", "bbc");
Console.WriteLine("String.Equals(\"bbc\", \"bbc\") is " + boolResult);
boolResult = myString.Equals(myString2);
Console.WriteLine("myString.Equals(myString2) is " + boolResult);
boolResult = myString == myString2;
Console.WriteLine("myString == myString2 is " + boolResult);
}

Use the Format() method to format a


string
using System;
class MainClass
{
public static void Main()
{
float myFloat = 1234.56789f;
string myString8 = String.Format("{0, 10:f3}", myFloat);
Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8);
}
}

Use the Format() method to format a


string
using System;
class MainClass
{
public static void Main()
{
float myFloat = 1234.56789f;
string myString8 = String.Format("{0, 10:f3}", myFloat);
Console.WriteLine("String.Format(\"{0, 10:f3}\", myFloat) = " + myString8);
}
}

Use the Join() method to join strings


using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString9 = String.Join(".", myStrings);
Console.WriteLine("myString9 = " + myString9);
}
}

Use the Split() method to split strings


using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString9 = String.Join(".", myStrings);
myStrings = myString9.Split('.');
foreach (string mySplitString in myStrings)
{
Console.WriteLine("mySplitString = " + mySplitString);
}
}
}

Use the IndexOfAny() and


LastIndexOfAny() methods to search
for character arrays in a string

using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString = String.Join(".", myStrings);
char[] myChars = {'b', 'e'};
int index = myString.IndexOfAny(myChars);
Console.WriteLine("'b' and 'e' occur at index " + index + " of myString");
index = myString.LastIndexOfAny(myChars);
Console.WriteLine("'b' and 'e' last occur at index " + index + " of myString");
}
}

Use the Insert(), Remove(), and Replace()


methods to modify strings
using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString = String.Join(".", myStrings);

}
}

string myString10 = myString.Insert(6, "A, ");


Console.WriteLine("myString.Insert(6, \"A, \") = " + myString10);
string myString11 = myString10.Remove(14, 7);
Console.WriteLine("myString10.Remove(14, 7) = " + myString11);
string myString12 = myString11.Replace(',', '?');
Console.WriteLine("myString11.Replace(',', '?') = " + myString12);
string myString13 = myString12.Replace("to be", "Or not to be A");
Console.WriteLine("myString12.Replace(\"to be\", \"Or not to be A\") = " + myString

Use the PadLeft() and PadRight()


methods to align strings
using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString = String.Join(".", myStrings);
string myString14 = '(' + myString.PadLeft(20) + ')';
Console.WriteLine("'(' + myString.PadLeft(20) + ')' = " + myString14);
string myString15 = '(' + myString.PadLeft(20, '.') + ')';
Console.WriteLine("'(' + myString.PadLeft(20, '.') = " + myString15);
string myString16 = '(' + myString.PadRight(20) + ')';
Console.WriteLine("'(' + myString.PadRight(20) + ')' = " + myString16);
string myString17 = '(' + myString.PadRight(20, '.') + ')';
Console.WriteLine("'(' + myString.PadRight(20, '.') + ')' = " + myString17);
}
}

Use the Trim(), TrimStart(), and


TrimEnd() methods to trim strings
using System;
class MainClass
{
public static void Main()
{
string myString18 = '(' + "
Console.WriteLine("'(' + \"
string myString19 = '(' + "
Console.WriteLine("'(' + \"
string myString20 = '(' + "
Console.WriteLine("'(' + \"

Whitespace
Whitespace
Whitespace
Whitespace
Whitespace
Whitespace

".Trim() + ')';
\".Trim() + ')' = " + myString18);
".TrimStart() + ')';
\".TrimStart() + ')' = " + myString19);
".TrimEnd() + ')';
\".TrimEnd() + ')' = " + myString20);

}
}

Use the Substring() method to retrieve


substrings
using System;
class MainClass
{
public static void Main()
{
string[] myStrings = {"To", "be", "or", "not", "to", "be"};
string myString = String.Join(".", myStrings);
string myString21 = myString.Substring(3);
Console.WriteLine("myString.Substring(3) = " + myString21);
string myString22 = myString.Substring(3, 2);
Console.WriteLine("myString.Substring(3, 2) = " + myString22);
}
}

You might also like