简体   繁体   中英

How to combine two different strings in one string?

I have checked most of the questions might contain my solution but I couldn't find any. Or maybe I didn't understand. So, here is my question:

I want to combine two strings in one and keep using it alone.

My strings:

    static string name = ""; (for example: John or Jane)
    static string gender = ""; (for example: Mr. or Mrs.)

and I want to combine these two in one like this:

    static string player = gender+name;

    Console.writeline("Hello "+player);

and I want to see it in my console like this:

 Hello Mr.John or Hello Mrs.Jane

I didn't want to mention console.readline parts. There will be entries where I will type name and gender. Thanks

EDIT:

This is what I did (Sorry, it take too long):

    static string name = "";
    static string gender = "";
    static string player = name + gender;
    static void Main(string[] args)
    {

        Console.WriteLine("Welcome. What is your name?");
        name = Console.ReadLine();
        Console.WriteLine("Sex?\n-Male\n-Female");
        gender = Console.ReadLine();
        Console.WriteLine("Press Enter to continue");
        Console.ReadLine();
        Console.WriteLine("Welcome"+player);

        Console.ReadLine();
    }

These result like "Welcome __ "

The problem here is that player is calculated at the class initialization. So basically you're combining string.Empty and string.Empty . Player is not calculated before each use.

So you could just do player = name + gender; before using it, but a good practice is to use variables restrained to the scope they're used in. Since you're name and gender are used in Main, use local variables.

static void Main(string[] args)
{
    string name;
    string gender;

    Console.WriteLine("Welcome. What is your name?");
    name = Console.ReadLine();

    Console.WriteLine("Sex?\n-Male\n-Female");
    gender = Console.ReadLine();

    Console.WriteLine("Press Enter to continue");
    Console.ReadLine();

    Console.WriteLine("Welcome " + gender + name);

    Console.ReadLine();
}

If you prefer, you can also do

string player = gender + name;
Console.WriteLine("Welcome " + player);

but I think the intent was clear enough without the temp variable. If you need more complex formatting, you can also string.Format , it's cleaner than a bunch of + operators.

 Console.WriteLine(string.Format("Welcome, {0} {1}!", gender, name));

You read name and gender but you never combine them after getting their value, so player still remains an empty string.

Do this, instead:

player = gender + name;
Console.WriteLine("Welcome "+player);
Console.ReadLine();
player = gender+name;
Console.WriteLine("Welcome"+player);

This will set the strings to what they have been read as. You would also need an if statement to change fe/male to mr/s.

The problem is that player should be a function, not a string.

String Player()
{
     return gender + name;
}

This has to be outside your main function.

You can use String.Concat(string firstString,string secondString) , if you want to merge strings with no delimiter. And you can use String.Join(string separator, string[] stringsToBeJoined) . The first parameter is the separator between strings in the merged single string and the second parameter is the array of strings which will be merged.

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