C#将文本字符串转换为另一个字符串变量

时间:2017-01-03 16:35:43

标签: c# string

我有这段代码:

string str1 = "Button1";
string str2 = "Button2";
string str3 = str1 + " " + str2;

我想要的是复制str3"Button1 Button2")中的文字,以便

string str4 = "Button1 Button2";

为什么我想要这样的东西,你可能会问?这是因为我尝试开发这种方法:

    public void SearchNumpadNumbersOnMyApp(double valueRepoItemName)
    {   
        valueRepoItemName = Math.Abs(valueRepoItemName);        
        string repoItemName;
        string result = string.Format("{0:F1}", valueRepoItemName);
        int length = result.Length;         
        char[] arrayOfCharacters = result.ToCharArray();

        for (int i = 0; i < length; i++)
        {                   
        repoItemName = "Button" + arrayOfCharacters[i].ToString();

        // Query RepoItemInfo objects based on the repository item name 
        IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                    where ReferenceEquals(things.Name, repoItemName)
                    select things;
        // Create "unkown" adapter for the first found element and click it
        myQuery.First().CreateAdapter<Unknown>(true).Click();               
        }               
    }

当我将repoItemName传递给

where ReferenceEquals(things.Name, repoItemName)

我收到错误消息"Sequence contains no elements",当我尝试传递repoItemName字符串时会发生这种情况。这与传递

相同
where ReferenceEquals(things.Name, "Button" + arrayOfCharacters[i].ToString())

这就是我收到错误的原因。所以,我想要的是传递字符串的实际文本而不是它的引用。我想要它,例如,像这样:

where ReferenceEquals(things.Name, "Button5")

成为&#34; Button5&#34;用以下构建的字符串结构:

repoItemName = "Button" + arrayOfCharacters[i].ToString();

顺便说一句,我已经尝试过:

String.Copy();
String.Clone();

但似乎没有什么能做我真正想要的。

2 个答案:

答案 0 :(得分:1)

我的问题的解决方案是:

let someInt = 4
pvElf.text = "\(someInt)"

感谢所有帮助我的人:)

答案 1 :(得分:0)

您需要更改此

IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                where ReferenceEquals(things.Name, repoItemName)
                select things;

IEnumerable<RepoItemInfo> myQuery = from things in repo.FO.FLOW2FO.Container2.SelfInfo.Children
                where things.Name == repoItemName
                select things;

您想比较字符串的内容,而不是引用。 C#中的string是一个不可变的。所以

string str1 = "button1 button2";
string str2 = new string(str1.ToCharArray());

str1str2相等字符串,但这两个变量引用了string 的两个不同实例。 Thatswhy ReferenceEquals(str1, str2)将始终返回false