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# String - Insert() Method
Selected Reading
C# String - Insert() MethodThe C# String Insert() method is used to return a new string in which a specified string is inserted at a given index position in this instance of string. ExceptionThere are a few exceptions to this method −
SyntaxFollowing is the syntax of the C# string Insert() method − public string Insert(int startIndex, string value); ParametersThis method accepts the following parameters −
Return valueThis method returns a new string. That is equal to the current string object. But the string is modified with a value inserted at the specified position. Example 1: Insert Space CharacterThe following example inserts a space character in the fourth character position (i.e. the character at index 3) of a string using the Insert() method −
using System;
public class Program {
public static void Main() {
String original_str = "tutorialspoint";
Console.WriteLine("The original string: '{0}'", original_str);
// modify the string...
String modified_str = original_str.Insert(3, " ");
Console.WriteLine("The modified string: '{0}'", modified_str);
}
}
OutputFollowing is the output − The original string: 'tutorialspoint' The modified string: 'tut orialspoint' Example 2: Insert a StringLet us look at another example of the Insert() method. Here, we define a string, and between the string's words, we are inserting another one − using System;
public class Program {
public static void Main() {
String str = "Hello World!";
String to_be_insert = "tutorialspoint";
Console.WriteLine("The original string: '{0}'", str);
// modify the string...
String modified_str = str.Insert(3, to_be_insert);
Console.WriteLine("The modified string: '{0}'", modified_str);
}
}
OutputFollowing is the output − The original string: 'Hello World!' The modified string: 'Heltutorialspointlo World!' Example 3: Insert at the Beginning of a StringIn this example, we use the Insert() method to add a string at the beginning of the string, specifically at index position 0 − using System;
class Program {
static void Main() {
string str = "World!";
string modified_str = str.Insert(0, "Hello ");
Console.WriteLine(modified_str);
}
}
OutputFollowing is the output − Hello World! Example 4: Insert at the End of a StringIn this example, we use the Insert() method to insert a string at the end of the current string object − using System;
class Program {
static void Main() {
string str = "Hello";
string to_be_insert = " tutorialspoint!";
// modified the string
string modified_str = str.Insert(str.Length, to_be_insert);
Console.WriteLine(modified_str);
}
}
OutputFollowing is the output − Hello tutorialspoint! csharp_strings.htm 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. |