GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C# - Strings
Selected Reading
C# - StringsIn C#, you can use strings as array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System.String class. Creating a String ObjectYou can create a string object using one of the following methods −
Example to Create a String ObjectThe following example demonstrates all the methods listed above: using System;
namespace StringApplication {
class Program {
static void Main(string[] args) {
// Creating a string using a string literal and concatenation
string fname, lname;
fname = "Rowan";
lname = "Atkinson";
char[] letters = {'H', 'e', 'l', 'l', 'o'};
string[] sarray = {
"Hello",
"From",
"Tutorials",
"Point"
};
string fullname = fname + " " + lname;
Console.WriteLine("Full Name: {0}", fullname);
// Creating a string using a character array
string greetings = new string(letters);
Console.WriteLine("Greetings: {0}", greetings);
// Joining an array of strings
string message = String.Join(" ", sarray);
Console.WriteLine("Message: {0}", message);
// Formatting method to convert a value
DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
Console.WriteLine("Message: {0}", chat);
}
}
}
When the above code is compiled and executed, it produces the following result − Full Name: Rowan Atkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 5:58 PM on Wednesday, October 10, 2012 Creating a StringYou can create a string in C# using string literals or the String class. ExampleIn the following example, we are creating a simple string and printing it. using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string message = "Hello, C#!";
Console.WriteLine(message);
}
}
}
When the above code is compiled and executed, it produces the following result − Hello, C#! Finding String LengthYou can find the length of a string using the ExampleIn the following example, we are finding and printing the length of a given string. using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "C# Programming";
int length = str.Length;
Console.WriteLine("Length of the string: " + length);
}
}
}
When the above code is compiled and executed, it produces the following result − Length of the string: 14 Iterate Over String CharactersYou can iterate over each character in a string using a ExampleIn the following example, we are iterating through each character of a string and printing them separately. using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "C# by TP";
foreach (char ch in str) {
Console.WriteLine(ch);
}
}
}
}
When the above code is compiled and executed, it produces the following result − C # b y T P Comparing StringsYou can compare two strings using the ExampleIn the following example, we are comparing two strings and displaying whether they are equal or not. using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str1 = "Hello";
string str2 = "World";
if (String.Compare(str1, str2) == 0) {
Console.WriteLine("Strings are equal.");
} else {
Console.WriteLine("Strings are not equal.");
}
}
}
}
When the above code is compiled and executed, it produces the following result − Strings are not equal. Checking for a SubstringYou can check if a string contains another string using the ExampleIn the following example, we are checking if a given substring exists in a string. using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "Welcome to C# programming";
if (str.Contains("C#")) {
Console.WriteLine("The sequence 'C#' was found.");
}
}
}
}
When the above code is compiled and executed, it produces the following result − The sequence 'C#' was found. Getting a SubstringYou can extract a part of a string using the ExampleIn the following example, we are extracting a part of a string and displaying it: using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string str = "C# Programming Language";
Console.WriteLine(str);
string substr = str.Substring(3, 11);
Console.WriteLine(substr);
}
}
}
When the above code is compiled and executed, it produces the following result − Programming Joining StringsYou can join multiple strings using the ExampleIn the following example, we are joining an array of strings using a newline character: using System;
namespace StringApplication {
class StringProg {
static void Main(string[] args) {
string[] lines = new string[] {
"C# is a modern",
"Object-oriented",
"Programming language"
};
string result = String.Join("\n", lines);
Console.WriteLine(result);
}
}
}
When the above code is compiled and executed, it produces the following result − C# is a modern Object-oriented Programming language Handling Strings Using StringBuilderStringBuilder helps to modify strings because regular strings cannot be changed directly. You can use Example: Using StringBuilderThe following example demonstrates how to use using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder("Hello");
sb.Append(" World!");
sb.Insert(6, "C# ");
sb.Replace("World", "Everyone");
Console.WriteLine(sb.ToString());
}
}
When executed, this program outputs: Hello C# Everyone! Properties of the String ClassThe String class has the following two properties −
Methods of the String ClassThe String class has numerous methods that help you in working with the string objects. The following table provides some of the most commonly used methods −
In this chapter, we have summarized key methods of the string class commonly used in C# programming. For the complete list of methods and String class constructors, visit the MSDN library. Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |