Unity | common knowledge of dealing with strings (continuously updated)

1. Usage of escape characters and @

1. Conventional usage

We have a line now, but I have requirements on its format and so on

Example: There is no perfect road, and all roads lead to Rome.

I want to type into:

There is always a way out,

All roads lead to Rome.

Before the line break, the writing method is:

string s = "天无绝人之路,条条道路通罗马";

 If you want to change the line, the writing method is:

string s = "天无绝人之路,\n条条道路通罗马";

Because the default in the computer is to break the line when \n is encountered, so it will break the line.

These very useful escape characters are:

 If you don't understand, there is a detailed link:

What are escape characters? What are the escape characters? Why use escape characters? _ Programmer! = Programmer's Blog - CSDN Blog

2. The first usage of @

However, there is a problem with this, if you write a link

string s = "D:\nice";

The system has a look, there is a \n here, let's change the line , then the ending you received is

D:

ice

So what do we do when we encounter this situation? Then I need to tell the computer, don’t mess around with the following characters, you can treat me as characters normally

You only need to add @ in front, and the computer can get it normally, written as:

string s =@"D:\nice";

3. The second usage of @

or this example

Example: There is no perfect road, and all roads lead to Rome.

I want to type into:

There is always a way out,

All roads lead to Rome.

I also have a wrapper:

string s = "天无绝人之路,"
           +"条条道路通罗马";

However, this is very troublesome, constantly typing double quotation marks and plus signs, very annoying

So we have to tell the computer, the following, I change the line and you change it for me, don't mess around

You can then write:

string s = @"天无绝人之路,
             条条道路通罗马";

4. The third usage of @

Everyone knows that we can't use keywords as names

int int =5;
int string =6;

But, I am perverted, I will use it! ! !

Well you can! !

int @int = 5;
int @string = 6;

Two, string and others together

example:

int age = 6;
float higt = 156;
string s = "我今年" + age + "岁," + "身高" + higt + "cm";

However, this is very troublesome, constantly typing double quotation marks and plus signs, very annoying

We just need to add a $ in front to wrap int and the like with {} .

int age = 6;
float higt = 156;
string s = $"我今年{age}岁,身高{higt}cm";

Guess you like

Origin blog.csdn.net/weixin_49427945/article/details/130125311