Click here to Skip to main content
15,908,013 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All...
i have one string Like Str="1,2,4,6" Here I need to add values 3 in between 2 and 4 and also add value 5 in between 4 and 6 ,i tried in many ways but i did't get solution
can any one please help me
thanks in advance
Posted

I assume that "1,2,4,6" is just an example?
If your string is always going to be comma separated, then consider splitting the string, and rebuilding it:
string myString = "1,2,4,6";
string[] parts = myString.Split(',');
StringBuilder sb = new StringBuilder();
sb.Append(parts[0]);
sb.Append(",");
sb.Append(parts[1]);
sb.Append(",3,");
sb.Append(parts[2]);
sb.Append(",5,");
sb.Append(parts[3]);
myString = sb.toString():



"Dear Sir,
I Get String From Runtime I Dont Know How It Will Came Either Like (1,2,5,7) Or Else (3,4,6) So On.. Here My Problem Is I Need To Add Missing Values"



Then I would do much the same as above, but use the first and last values to generate the intermediate ones:
string myString = "1,2,4,6";
string[] parts = myString.Split(',');
int first = int.Parse(parts[0]);
int last = int.Parse(parts[parts.Length - 1]);
StringBuilder sb = new StringBuilder();
string separator = "";
for (int i = first; i <= last; i++)
    {
    sb.Append(separator);
    sb.Append(i.ToString());
    separator = ",";
    }
myString = sb.ToString();
Console.WriteLine(myString);
This assumes that the incoming string is effectively sorted! If not, then you have to do something about that first!
 
Share this answer
 
v2
Comments
tulasiram3975 8-Apr-11 4:34am    
Thank you Sir But I Have 6 String Like '3,5,6" here i need to add 1,2,4 I need Clear Solution For Different Strings
OriginalGriff 8-Apr-11 4:52am    
I'm not sure I understand your requirement: could you explain in more detail?
tulasiram3975 8-Apr-11 5:55am    
Dear Sir,
I Get String From Runtime I Dont Know How It Will Came Either Like (1,2,5,7) Or Else (3,4,6) So On.. Here My Problem Is I Need To Add Missing Values
OriginalGriff 8-Apr-11 6:07am    
Answer updated
tulasiram3975 8-Apr-11 6:17am    
Am Really Very Sorry Sir,
If String Came Like (3,4,7) Here I Need To Add (1,2) First And Then Only Add 6,7
As OriginalGriff told you, you can split the strings and rebuild them.
Between these two operations, you can construct a list of int and sort it:

C#
void FillList(List<int> list, string str)
{
    //split the string
    string[] parts = str.Split(',');
    //convert each part to int and put each int in a list
    foreach (string s in parts)
        list.Add(int.Parse(s));
}

void func()
{
    List<int> list = new List<int>();
    //fill the list
    FillList(list, "1,2,4,6");
    FillList(list, "3,5");
    //sort the list
    list.Sort();
    //build the final string
    StringBuilder sb = new StringBuilder();
    foreach (int i in list)
    {
        sb.Append(i);
        sb.Append(",");
    }
    string finalString = sb.ToString();
    ....
}
 
Share this answer
 
v3
If it is going to be a string representing integers delimited with comma I would use method which will do all the nasty work for me. For instance try this one:

string MyStringMethod(string originalString, string newValue)
{
  //original string is dissected to array of strings 
  string[] pomArr = originalString.Split(',');
  //List<t> offers all functionality I need for this.
  List<string> pomList = new List<string>();
  pomList.AddRange(pomArr);
  //new value
  pomList.Add(newValue);
  //magic :)! Sort() method is our friend
  pomList.Sort();
  //caller gets new string with properly inserted new value
  return string.Join(",", pomList.ToArray());
}</string></string></t>


It is simple and I believe self-explanatory.
 
Share this answer
 
You can do it in this way :

C#
String Str = "1,2,4,6";

// Pass the number to add and Str.
// It will return you a String with sorted numbers
private String NumberToAdd(int num, String baseString)
{
String[] ar = str.Split(',');
List<String> list = new List<String>();
list.AddRange(ar);
list.Add(num.ToString());
list.Sort();

int count = list.Count;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < count; i++)
{
    if (i != count - 1)
    {
        builder.Append(list[i] + ",");
    }
    else
    {
        builder.Append(list[i]);
    }
}
return builder.ToString();
}
 
Share this answer
 
v2

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