C# Programming - Strings - Discussion

Discussion Forum : 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);
ARE
CRE
CR
REA
CREATED
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
12 comments Page 1 of 2.

Naveen said:   8 years ago
The count starts from 0 or 1?

Somasekhar Kommera said:   9 years ago
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".

Gloops said:   10 years ago
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.

Ohanra said:   1 decade ago
It will count the space between the char also, so it print CRE.

Akhilesh said:   1 decade ago
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.

Nishant said:   1 decade ago
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.

Faisal said:   1 decade ago
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.

Sridevi said:   1 decade ago
12 represents the position from where to start searching and 3 represents the no of characters to be displayed from that position (12).

Jay said:   1 decade ago
Such type of question first count first argument including space and after that count second number including space.

Vedha said:   1 decade ago
We have to consider last 3 letters of 12 because the second value is 3.


Post your comments here:

Your comments will be displayed after verification.