c# – 连接特殊字符“ – ”的相邻字符

c# – 连接特殊字符“ – ”的相邻字符,第1张

概述我正在使用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 i want it to be rep 我正在使用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 i want it to be replaced like CDEAB-CD-K then i want it to be replaced like BC and DK both separated by keyword and

得到这个后,我必须准备我的查询到数据库.

我希望我能解决问题,但如果需要更多澄清,请告诉我.
任何帮助将不胜感激.

谢谢,
Devjosh

@H_419_13@解决方法 未经测试,但这应该可以解决问题,或者至少引导您朝着正确的方向前进.
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();}
总结

以上是内存溢出为你收集整理的c# – 连接特殊字符“ – ”的相邻字符全部内容,希望文章能够帮你解决c# – 连接特殊字符“ – ”的相邻字符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1248354.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存