Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Mar 14, 2012 12:38 PM by mameenkhn
Member
21 Points
14 Posts
Mar 14, 2012 12:18 PM|LINK
Hi
When i run below code it gives me integer value
var st1 ="asd"; var st2 = st1[0]+st1[1]; <h1>st2</h1>
and i am expecting "as" only . Can anyone tell me where i am wrong
Participant
986 Points
247 Posts
Mar 14, 2012 12:26 PM|LINK
It should not give.
But still if this is the case then you can write
var st2 = st1[0].ToString()+st1[1].ToString();
Contributor
5438 Points
730 Posts
Mar 14, 2012 12:29 PM|LINK
The correct syntax is
@{ var st1 ="asd"; var st2 = st1.Substring(0,2); } <h1>@st2</h1>
2026 Points
391 Posts
Mar 14, 2012 12:38 PM|LINK
Dear uahmed..
This is because of the + operator..
var st2 = st1[0]+st1[1];
because if you use + then C# will assume both the operands as Integer and take the ascii value of the alphabets
st2 = 97 + 115;
97 is ascii of a
and 115 is for s
uahmed
Member
21 Points
14 Posts
Adding Variable result integer
Mar 14, 2012 12:18 PM|LINK
Hi
When i run below code it gives me integer value
var st1 ="asd"; var st2 = st1[0]+st1[1]; <h1>st2</h1>and i am expecting "as" only . Can anyone tell me where i am wrong
dinesh kumar...
Participant
986 Points
247 Posts
Re: Adding Variable result integer
Mar 14, 2012 12:26 PM|LINK
It should not give.
But still if this is the case then you can write
Jai Jagannath
GmGregori
Contributor
5438 Points
730 Posts
Re: Adding Variable result integer
Mar 14, 2012 12:29 PM|LINK
The correct syntax is
@{ var st1 ="asd"; var st2 = st1.Substring(0,2); } <h1>@st2</h1>mameenkhn
Contributor
2026 Points
391 Posts
Re: Adding Variable result integer
Mar 14, 2012 12:38 PM|LINK
Dear uahmed..
This is because of the + operator..
var st2 = st1[0]+st1[1];
because if you use + then C# will assume both the operands as Integer and take the ascii value of the alphabets
st2 = 97 + 115;
97 is ascii of a
and 115 is for s
--------------------------------------------------
Muhammad Amin
محمد امين