C# Programming - Strings - Discussion

Discussion :: Strings - General Questions (Q.No.5)


Which of the following will be the correct output for the C#.NET code snippet given below?

String s1 = "Nagpur";
String s2;
s2 = s1.Insert(6, "Mumbai"); 

[A]. NagpuMumbair
[B]. Nagpur Mumbai
[C]. Mumbai
[D]. Nagpur
[E]. NagpurMumbai

Answer: Option E


No answer description available for this question.

Asish Kumar Satpathy said: (May 29, 2012)  
If the expression will be like this "s2=s1.insert (5, "mumbai") ;".
Then what will be the output?

Ashish Tiwari said: (Oct 17, 2012)  
How will be the indexing of string here? from 0 or from 1?

Rajitha Reddy said: (Mar 16, 2013)  
The Insert() function in String Class will insert a String in a specified index in the String instance.

string string.Insert(int ind,string str)


ind - The index of the specified string to be inserted.

str - The string to be inserted.


String - The result string.


System.ArgumentOutOfRangeException: startIndex is negative or greater than the length of this instance

System.ArgumentNullException : If the argument is null.

Newbie said: (Jun 11, 2013)  
According to msdn site of String.Insert() method, it says index here is zero-based, but how come can the index of a string be equal to its length?

Vinod Bhosale said: (Jun 20, 2013)  
If we see a String as an array of the characters then its indexing will start from 0 as it is known & it can also be considered as dynamic array.

In the rule of array 0 is the first index.

So if NAGPUR is stored in a string it will store following value at this index:

0 - N
1 - A
2 - G
3 - P
4 - U
5 - R

So if we look at line below :

s2 = s1. Insert (6, "Mumbai") ;.

It says to put the mumbai at index 6 which is a free unused space & hence instead of replacing the 'R' it stores the 'MUMBAI' after 'NAGPUR' is completed.

Constrantine said: (Sep 30, 2014)  
Then why in question 2, in substring function 12 is not considered as index ?

String s2;
s2 = s1.Substring(12, 3);

David said: (Nov 19, 2015)  
Shouldn't this return an argument out of range exception?

Dev said: (Nov 10, 2018)  
String is 1 index based thats why its start from 1.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.