Click here to Skip to main content
16,017,100 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how i add int value to string value for eg string value is S001 And int value is 2 result comes S003
BUT MY RESULT COMES SOO12
Posted
Comments
[no name] 25-Jun-14 6:44am    
You will have to extract the number from the string, convert the string to a real integer, do your addition and then remake your string from the result.
Member 10285877 25-Jun-14 6:47am    
sir can u give me short example

You have to split the string in two, with the alpha on one side and the numerics on the other:
C#
string input = "S001";
int addOn = 2;
string alpha = input.Substring(0, 1);
string numeric = input.Substring(1);
You can then parse the numeric to get a number, add the value to it, and generate your new string:
C#
int value;
if (int.TryParse(numeric, out value))
    {
    string result = string.Format("{0}{1,1:D3}", alpha, value + addOn);
    }


[edit]Forgot to change the numeric part![/edit]
 
Share this answer
 
v2
Comments
Member 10285877 25-Jun-14 7:31am    
thanks sir its working
C#
string input = "S001";
            int addOn = 2;
            string alpha = input.Substring(0, 1);
            string numeric = input.Substring(1, input.length-1);
            numeric=numeric+addOn;
            alpha=alpha+numeric.ToString() // you got your string here whatever you wanted
 
Share this answer
 
Comments
Member 10285877 25-Jun-14 7:28am    
sir its out put is s0012
but i need s003

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900