Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#将字符串中第一个数值后的空格/字符替换为指定字符?_C#_Str Replace - Fatal编程技术网

如何使用C#将字符串中第一个数值后的空格/字符替换为指定字符?

如何使用C#将字符串中第一个数值后的空格/字符替换为指定字符?,c#,str-replace,C#,Str Replace,我有字符串包含特殊字符和数值 Eg: 3-3 3 3-3"3/4 3-3-3/4 3-3 3/4 3 3 3 3'3 3 Output must be: 3f3 3 3f3"3/4 3f3-3/4 3f3 3/4 3f3 3 3f3 3 我试过使用: public static string ReplaceFirst(string text, string search, string replace) {

我有
字符串
包含
特殊字符和数值

Eg:
3-3 3 
3-3"3/4
3-3-3/4
3-3 3/4
3 3 3
3'3 3

Output must be:
    3f3 3 
    3f3"3/4
    3f3-3/4
    3f3 3/4
    3f3 3
    3f3 3
我试过使用:

public static string ReplaceFirst(string text, string search, string replace)
        {
            int pos = text.IndexOf(search);
            if (pos < 0)
            {
                return text;
            }
            return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
        }
但对于
3 3-3输出:3 3f3
根据代码是正确的。但是我想在第一个数字后面替换空格/字符,这将是
3f3-3


谢谢你的帮助

简单的循环应该可以工作

var match = Regex.Match(text, search); //To search for any special character, use @"\W" as the second parameter
var editable = text.ToCharArray();
editable[match.Index] = replace[0];
text = new String(editable);
return text;
for(int i =0; i < myListOfStrings.Count; i++)
{
    char[] chars = myListOfStrings[i].ToCharArray();
    for (int j = 0; j < chars.Count(); j++)
    {
       if (Char.IsDigit(chars[j]))
       {
           if(j + 1 < chars.Count())
           {
              chars[j + 1] = 'f'; //'f' being your replacing character
              myListOfStrings[i] = new string(chars);
           }
           break;
       }
    }
}
根据评论更新

for (int i = 0; i < myListOfStrings.Count; i++)
{
     char[] chars = myListOfStrings[i].ToCharArray();
     for (int j = 0; j < chars.Count(); j++)
     {
         if (!Char.IsDigit(chars[j]))
         {
             if (j + 1 < chars.Count())
             {
                 chars[j] = 'f'; //'f' being your replacing character
                 myListOfStrings[i] = new string(chars);
             }
             break;
         }
     }
}
for(int i=0;i
以下内容对我很有用:

修改代码:

string S="10-3 3";
char[] chars = S.ToCharArray();
                for (int j = 0; j < chars.Count(); j++)
                {
                    if (Char.IsDigit(chars[j]))
                    {

                    }
                    else
                    {
                        if (j + 1 < chars.Count())
                        {
                            chars[j] = 'f'; //'f' being  replacing character
                            S = new string(chars);
                        }
                        break;
                    }
                }

Output: 10f3 3
string S=“10-3”;
char[]chars=S.ToCharArray();
对于(int j=0;j
每一行都是单独的字符串吗?或者有新的行字符吗?是的@Jeroen1984每行都是单独的字符串这些是一些示例!您的意思是在第一个数字之后,空格或-或应替换为f。。。。。。。没有别的了。正确的?所有的字符串都是分开的吗?第一个字符是否始终为数字?您好@GAPS,是的,第一个字符将为数字,我的意思与您指定的相同!您好@keyboardP,当我使用10-3时,它会给出1f-3,这是不正确的。它必须是10f3-3。请帮助我解决这个问题?您已经更改了要求(或者可能是输入错误)。为什么
10-3 3
变成
10f3-3
?它不应该是
10f3 3
?如果是这样的话,这应该是一个简单的解决方案,我会更新答案。请不要误解@keyboardP,我提到过这些是示例,我需要替换第一个数字,它表示3-3中的3,10200,10-3 3200 3等等。但是,谢谢你的样品。我已经修改并发布了。很抱歉,上面的评论拼错了。我道歉!谢谢你@不,那很好,但是你的评论改变了格式,打破了我原来的帖子。你说的是
10f3-3
。为什么仪表盘移动了?编辑-刚刚看到你的答案。这是你评论中的一个输入错误。您可以稍微简化代码(请参阅更新),但当字符串为3-3时,使用现有代码3输出到3f3 3,但当值为10-3 3时,它显示1f3 3,这是错误的,因此。。!
for (int i = 0; i < myListOfStrings.Count; i++)
{
     char[] chars = myListOfStrings[i].ToCharArray();
     for (int j = 0; j < chars.Count(); j++)
     {
         if (!Char.IsDigit(chars[j]))
         {
             if (j + 1 < chars.Count())
             {
                 chars[j] = 'f'; //'f' being your replacing character
                 myListOfStrings[i] = new string(chars);
             }
             break;
         }
     }
}
string S="10-3 3";
char[] chars = S.ToCharArray();
                for (int j = 0; j < chars.Count(); j++)
                {
                    if (Char.IsDigit(chars[j]))
                    {

                    }
                    else
                    {
                        if (j + 1 < chars.Count())
                        {
                            chars[j] = 'f'; //'f' being  replacing character
                            S = new string(chars);
                        }
                        break;
                    }
                }

Output: 10f3 3