将缺失的值添加到字符串

编程入门 行业动态 更新时间:2024-10-28 06:28:50
本文介绍了将缺失的值添加到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好... 我有一个字符串,如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.

更多推荐

将缺失的值添加到字符串

本文发布于:2023-10-12 17:42:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1485418.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   缺失

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!