C# Programming - Strings - Discussion

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

2. 

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

String s1 = "ALL MEN ARE CREATED EQUAL";
String s2;
s2 = s1.Substring(12, 3); 
Console.WriteLine(s2);

[A]. ARE
[B]. CRE
[C]. CR
[D]. REA
[E]. CREATED

Answer: Option B

Explanation:

No answer description available for this question.

Mallu said: (Jan 3, 2012)  
Can any body explain please?

Bharath said: (Jan 9, 2012)  
Its substring right so its consider after 12 letters.

Vedha said: (Mar 5, 2012)  
We have to consider last 3 letters of 12 because the second value is 3.

Jay said: (Apr 11, 2012)  
Such type of question first count first argument including space and after that count second number including space.

Sridevi said: (Sep 30, 2012)  
12 represents the position from where to start searching and 3 represents the no of characters to be displayed from that position (12).

Faisal said: (May 31, 2013)  
substring (12, 3) , Here the first argument is used to count the string till letter 12 and second argument is for printing 3 letters after character 12.

Nishant said: (Jul 22, 2013)  
Substring extracts a substring from the entered position.

Now here position is 12 and 'C' of Created has position 13 so substring will extract position 13, 14, 15 because range is upto 3 only. So the answer will be CRE.

Akhilesh said: (Jan 14, 2014)  
String s1 = "ALL MEN ARE CREATED EQUAL";
String s2;
s2 = s1.Substring(12, 3);
Console.WriteLine(s2);

According to question,
If we compare the string to array it will start from 0.
But given the ans it will not compare the value of string to array that saw it will start from 1.

ALL MEN ARE CREATED EQUAL";
.... .... .... ...
1234 5678 9101112 131415.
.... .... .... ...

According to question 12, 3 = 12+3 = 15.

Ohanra said: (Jul 10, 2014)  
It will count the space between the char also, so it print CRE.

Gloops said: (Aug 3, 2014)  
With just one less in the first argument of Substring, you obtained " CR", but this is not answer C as it does not include the space.

@Mallu :

Please just have a look there :
http://msdn.microsoft.com/en-US/library/aka44szs%28v=vs.110%29.aspx.

Somasekhar Kommera said: (Apr 28, 2015)  
First count the index of the given string its starting from 0th index. So 12th index value is "C", then count from 12th index to next 3 characters. Final answer is "CRE".

Naveen said: (Mar 3, 2016)  
The count starts from 0 or 1?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.