简体   繁体   中英

C# String text into another string variable

I have this code:

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

What I want is to copy the text from str3 ( "Button1 Button2" ) so that

string str4 = "Button1 Button2";

Why do I want such a thing, you may ask? It's because of this method that I'm trying to develop:

    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();               
        }               
    }

When I pass repoItemName to

where ReferenceEquals(things.Name, repoItemName)

I get the error message "Sequence contains no elements" , and this occurs when I try to pass repoItemName string. This is the same as passing

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

and this is the reason why I'm getting the error. So, what I want is to pass the actual text of the string and not its reference. I want it to be, for example, like this:

where ReferenceEquals(things.Name, "Button5")

Being "Button5" the string structure built with:

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

By the way, I already tried:

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

but nothing seems to do what I really want.

The solution to my problem was this:

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

Thanks to all of you guys that helped me :)

You need to change this

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

to

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

You want to compare the content of the strings, not the references. A string in C# is an immutable class . So

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

str1 and str2 are equal strings, but the two variables reference two different instances of string . Thatswhy ReferenceEquals(str1, str2) will always return false .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM