Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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# - Fatal编程技术网

从特定字符到第一个空格的C#子字符串

从特定字符到第一个空格的C#子字符串,c#,C#,我想用另一个字符串替换以特定字符开头的字符串,直到后面的第一个空格(或者可能是第一个特定字符) 例如,如果我们有这样一个字符串: aaa bbb ccc @ddd eee fff @ggg hh#iii. jj 我想把它做成这样 aaa bbb ccc @MMM eee fff @MMM hh#MMM. jj 但这对我没有帮助 更新: 我想将所有以@或#开头并以或结尾的单词或其他我想要的字符串替换为MMM 在一个真实的例子中,我只知道一些工作是从@或#开始的 我认为这将为你想要的东西提供一个

我想用另一个字符串替换以特定字符开头的字符串,直到后面的第一个空格(或者可能是第一个特定字符)

例如,如果我们有这样一个字符串:

aaa bbb ccc @ddd eee fff @ggg hh#iii. jj
我想把它做成这样

aaa bbb ccc @MMM eee fff @MMM hh#MMM. jj
但这对我没有帮助

更新:

我想将所有以
@
#
开头并以
结尾的单词或其他我想要的字符串替换为
MMM

在一个真实的例子中,我只知道一些工作是从
@
#
开始的


我认为这将为你想要的东西提供一个良好的开端,尽管根据你的具体需要可能需要一些润色

    string val = "aaa bbb ccc @ddd eee fff @ggg hh#iii. jj";
    string[] values = val.Split(' '); //identify the separate entities you want split
    val = "";//reset string to empty string
    foreach(string a in values)
    {
        if (a.Contains('@') || a.Contains('#'))
            val += a[0] + "MMM"; //change the values to 'M' of the original
        else
            val += a;
    }
    return val; //return the string with values changed to 'M'
Juharr在评论中提供的使用Linq的单行示例

return string.Join(" ", val.Split().Select(s => s.StartsWIth('@') || s.StartsWith('#') ? s[0] + "MMM" : s));
string example=“aaa bbb ccc@ddd eee fff@ggg hh”//初始化输入
string[]splitExample=example.Split(“”)//将每个空格后的字符串拆分为子字符串数组
for(int i=0;i

我不确定这是不是最好的方法,这是我脑子里想不出来的。我更新了它。请再次查看。更新不会更改任何内容。使用
string.Replace()
..@Jonesopolis在实际示例中,我只知道一些工作是从
@
#
开始的。您的for循环需要一些帮助来做任何事情或编译。是的,我并没有在IDE中实现这一点,我在枯燥的计算机基础课上花了额外的时间写了这篇文章:/即使我的答案能产生
“aaa bbb ccc@ddd eee fff@ggg hh#iii”,你的答案也肯定会更好。jjaaabbbccc@MMMeeefff@mmmhmjj“
现在它只是
”aaabbbccc@MMMeeefff@mmmhmmjj“
您需要重新添加空格。@如果您是正确的,可能需要添加一个空格,以及更好地处理似乎是数据中间序列的内容。只需执行
返回string.Join(“,val.Split()。选择(s=>s.StartsWIth('@')| | s.StartsWIth('#')?s[0]+“MMM”:s))Linq的良好使用。
    string example = "aaa bbb ccc @ddd eee fff @ggg hh"; //Init the input 
    string[] splitExample = example.Split(' '); //Split the string after every space into an array of substrings
    for (int i = 0; i < splitExample.Length; i++) //Iterate through each substring
    {
        if (splitExample[i] != null) //Check if the currently tested string exists within the array
        {
            if (splitExample[i] == '@') // Test if the first char of the currently tested substring is '@'
            {
                splitExample[i] = "@MMM"
            }
        }
        else
        {
            break; //Exit the loop if the tested string does not exist
        } 
    }
    string exampleOutput;
    foreach(string append in splitExample) //Iterate through the array and add the now modified substrings back together
    {
        exampleOutput = exampleOutput + " " + append;
    }