将缺失的值添加到字符串 [英] Adding Missed Values To A String

查看:93
本文介绍了将缺失的值添加到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
我有一个字符串,如Str ="1,2,4,6"在这里,我需要在2和4之间添加值3,还需要在4和6之间添加值5,我尝试了很多方法,但是我没有获取解决方案
谁能帮我吗
在此先感谢

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

推荐答案

我假设"1,2,4,6"只是一个例子?
如果您的字符串总是要用逗号分隔,请考虑拆分字符串,然后重新构建它:
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():




亲爱的先生,
我从运行时获取字符串,我不知道它会是(1,2,5,7)还是其他(3,4,6),等等.在这里,我的问题是我需要添加缺少的值"



然后,我将执行与上面相同的操作,但是使用第一个和最后一个值来生成中间值:




"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!


就像OriginalGriff告诉您的那样,您可以拆分字符串并重新构建它们.
在这两个操作之间,您可以构造一个int列表并对其进行排序:

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:

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();
    ....
}


如果它将是一个表示用逗号分隔的整数的字符串,我将使用method来为我完成所有麻烦的工作.例如尝试以下方法:

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.


这篇关于将缺失的值添加到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆