Jak usunąć wszystkie białe spacje z początku lub końca łańcucha?

Jak usunąć wszystkie białe spacje z początku i końca łańcucha?

Like so:

"hello" zwroty "hello"
"hello " zwroty "hello"
" hello " zwroty "hello"
" hello world " returns "hello world"

Author: archer, 2010-08-01

6 answers

String.Trim() przycina wszystkie białe spacje od początku i końca łańcucha:

"   A String   ".Trim() -> "A String"

String.TrimStart() przycina wszystkie białe spacje od początku łańcucha:

"   A String   ".TrimStart() -> "A String   "

String.TrimEnd() przycina wszystkie białe spacje od końca łańcucha:

"   A String   ".TrimEnd() -> "   A String"
 345
Author: Mau,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-20 14:12:17

Spójrz na Trim() który zwraca nowy łańcuch znaków z usuniętymi białymi spacjami od początku i końca łańcucha, który jest wywoływany.

 16
Author: Russ Cam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-01 12:09:50
string a = "   Hello   ";
string trimmed = a.Trim();

trimmed jest teraz "Hello"

 12
Author: adamse,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-01 12:10:10

Użyj funkcji String.Trim().

string foo = "   hello ";
string bar = foo.Trim();

Console.WriteLine(bar); // writes "hello"
 9
Author: Adam Robinson,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-01 12:09:59

Użyj metody String.Trim.

 7
Author: jwaliszko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-01 12:22:37

String.Trim() usuwa wszystkie białe znaki z początku i końca łańcucha. Aby usunąć białe znaki wewnątrz łańcucha lub znormalizować białe znaki, użyj wyrażenia regularnego.

 6
Author: tdammers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-08-01 12:10:30