简体   繁体   English

如何在一个字符串中组合两个不同的字符串?

[英]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. 我不想提及console.readline部分。 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. 这里的问题是player是在类初始化时计算的。 So basically you're combining string.Empty and string.Empty . 因此,基本上,您正在组合string.Emptystring.Empty Player is not calculated before each use. Player不会在每次使用前计算。

So you could just do player = name + gender; 所以你可以做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. 在使用它之前,但是一个好的做法是使用限制在其使用范围内的变量。由于您的名字和性别在Main中使用,因此请使用局部变量。

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. 但是我认为没有temp变量,意图很明确。 If you need more complex formatting, you can also string.Format , it's cleaner than a bunch of + operators. 如果需要更复杂的格式,还可以使用string.Format ,它比一堆+运算符更干净。

 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. 您读取namegender但在获得其值之后再也不会将它们组合在一起,因此player仍然是一个空字符串。

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. 您还需要一个if语句将fe / male更改为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. 如果要合并没有定界符的字符串,则可以使用String.Concat(string firstString,string secondString) And you can use String.Join(string separator, string[] stringsToBeJoined) . 而且您可以使用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. 第一个参数是合并的单个字符串中字符串之间的分隔符,第二个参数是将要合并的字符串数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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