C# 统一字符串替换

C# 统一字符串替换,c#,string,unity3d,filter,replace,C#,String,Unity3d,Filter,Replace,我对unity引擎有点问题。 我正在尝试合并一种从另一个字符串替换字符串的方法。 如果我使用一个字符串,按照您通常的方式进行设置,那么代码可以工作(例如string b=“whatever”); 但是,当我尝试使用列表中的变量时,输出没有得到修改(例如excludedWords[0])。 我曾尝试在excludedWords变量的所有位置将其转换为字符串,但仍然没有成功 有什么见解吗 using System.Collections.Generic; using UnityEngine; usi

我对unity引擎有点问题。 我正在尝试合并一种从另一个字符串替换字符串的方法。 如果我使用一个字符串,按照您通常的方式进行设置,那么代码可以工作(例如string b=“whatever”); 但是,当我尝试使用列表中的变量时,输出没有得到修改(例如excludedWords[0])。 我曾尝试在excludedWords变量的所有位置将其转换为字符串,但仍然没有成功

有什么见解吗

using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using TMPro;

public class FilterRex : MonoBehaviour
{
    List<string> excludedWords;
    [SerializeField]TextAsset excludedWordsFile;

    [SerializeField]TMP_InputField inputField;

    // Start is called before the first frame update
    void Start()
    {
        //this code accesses the words from the lists that need to be banned
        excludedWords = new List<string>();
        string[] lines = excludedWordsFile.text.Split('\n');
        foreach(string line in lines)
        {

            excludedWords.Add(line);
        }
        Debug.Log(excludedWords[0]);
        
    }
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Return))
        {
            PrepareTheWord();
        }
    }

    public void PrepareTheWord()
    {
            string innerWord = inputField.text.ToLower();
            string b = excludedWords[0].ToString());
            inputField.text = ReplaceTheWord(b, innerWord);
    }
    public string ReplaceTheWord(string _replace, string _from)
    {
        string output = _from.Replace(_replace, "");
        return output;
    }
}
使用System.Collections.Generic;
使用UnityEngine;
使用System.Linq;
使用TMPro;
公共类FilterRex:MonoBehavior
{
不包括单词的列表;
[SerializeField]TextAsset excludedWordsFile;
[SerializeField]TMP_InputField InputField;
//在第一帧更新之前调用Start
void Start()
{
//此代码访问列表中需要禁止的单词
excludedWords=新列表();
string[]line=excludedWordsFile.text.Split('\n');
foreach(行中的字符串行)
{
不包括单词。添加(行);
}
Debug.Log(不包括udedwords[0]);
}
无效更新()
{
if(Input.GetKeyDown(KeyCode.Return))
{
准备单词();
}
}
公共词汇
{
字符串innerWord=inputField.text.ToLower();
字符串b=excludedWords[0].ToString());
inputField.text=ReplaceTheWord(b,innerWord);
}
公共字符串替换单词(字符串_替换,字符串_从)
{
字符串输出=_from.Replace(_Replace,“”);
返回输出;
}
}

如果要在列表中对字符串值进行变异,则需要直接为其赋值:

excludedWords[0]=ReplaceTheWord(excludedWords[0],innerWord);
C#中的字符串是不可变的,因此如果要更改它们的值,必须重新分配给它们

编辑: 如果要替换输入文本中出现的排除词,请参见
PrepareTheWord
方法:

public void PrepareTheWord()
{
字符串innerWord=inputField.text.ToLower();
foreach(excludedWords中的字符串excludedWord)
{
innerWord=innerWord.Replace(不包括单词“”);
}
inputField.text=innerWord;
}

这不是最有效的方法,但它应该可以完成这项工作。

因此需要快速修复。我已经注意到,如果我创建一个数组,并键入该数组的元素,它就会工作。这不是最有效的方法,也不是最令人愉快的方法,但它将暂时起作用

另一方面。我不确定这是否是unity的问题,但我确定这不是PC的问题。我已经在三台不同的PC上测试了这段代码,从2019年开始,这三台PC都有不同的unity版本。他们的行为似乎都一样。 我相信我会发现这个问题,但目前,我将专注于其他更新。 我要感谢每一位阅读和回复我的询问的人,我希望你们都有一个美好的一天


我已经测试了unity 2018版本,仍然是同一版本,是用一个新项目制作的。

而不是使用
输入。GetKeyDown
我更愿意使用,例如,我已经测试了你的想法,我得到了一个ArgumentException:oldValue是空字符串。再一次,如果我直接写的话,请替换这个词(“我想要的词”,内心世界);将工作。除此之外,我不想更改excludedWords中的字符串。它们只是作为参考,我真的不知道你在问什么。您是否只想
b=ReplaceTheWord(b,innerWord);inputField.text=b?我想对excludedWords[0]进行检查。inputField中有一个由用户引入的单词。当我单击enter时,我正在运行此函数。此函数获取输入。换言之,我正在对照一份清单进行核对。如果列表中有一个单词可以在inputField中找到,那么我将其删除,只留下单词的其余部分。我刚刚注意到:string[]={“word1”,“word2”}与我创建的列表不相等:excludedWords[0]和excludedWords[1]。我现在正在考虑推迟执行,但我不确定是否是这样