c# – 连接特殊字符“ – ”的相邻字符
发布时间:2020-12-15 08:18:51 所属栏目:百科 来源:网络整理
导读:我正在使用c#.net开发一个应用程序,其中我需要如果用户输入的输入包含字符’ – ‘(连字符),那么我想连接连字符( – )的直接邻居,例如,如果用户输入 A-B-C then i want it to be replaced with ABCAB-CD then i want it to be replaced like BCABC-D-E then
我正在使用c#.net开发一个应用程序,其中我需要如果用户输入的输入包含字符’ – ‘(连字符),那么我想连接连字符( – )的直接邻居,例如,如果用户输入
A-B-C then i want it to be replaced with ABC AB-CD then i want it to be replaced like BC ABC-D-E then i want it to be replaced like CDE AB-CD-K then i want it to be replaced like BC and DK both separated by keyword and 得到这个后,我必须准备我的查询到数据库. 我希望我能解决问题,但如果需要更多澄清,请告诉我. 谢谢, 解决方法
未经测试,但这应该可以解决问题,或者至少引导您朝着正确的方向前进.
private string Prepare(string input) { StringBuilder output = new StringBuilder(); char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] == '-') { if (i > 0) { output.Append(chars[i - 1]); } if (++i < chars.Length) { output.Append(chars[i]) } else { break; } } } return output.ToString(); } 如果希望每对在数组中形成单独的对象,请尝试以下代码: private string[] Prepare(string input) { List<string> output = new List<string>(); char[] chars = input.ToCharArray(); for (int i = 0; i < chars.Length; i++) { if (chars[i] == '-') { string o = string.Empty; if (i > 0) { o += chars[i - 1]; } if (++i < chars.Length) { o += chars[i] } output.Add(o); } } return output.ToArray(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |