简体   繁体   English

如何在字符串开头删除空格?

[英]How do I remove whitespace at the beginning of my string?

Let's say I have following string: string test = " False"; 假设我有以下字符串: string test = " False";

I can't loop the characters of the string because I need to do this for 1744 strings, then it would be a lot of work. 我不能循环字符串的字符,因为我需要对1744个字符串执行此操作,这将需要大量工作。

Do you know if there is a method from Microsoft that I can use to delete this whitespace? 您知道我是否可以使用Microsoft的方法删除此空白吗?

Like this: string test2 = test.DeleteFirstWhitespace(); 像这样: string test2 = test.DeleteFirstWhitespace();

Thanks 谢谢

您可以在C#中使用TrimStart

string test2 = test.TrimStart()

As everyone else has pointed out, there is a trim function. 正如其他人指出的那样,有一个修整功能。 Make sure to remember that a string is immutable, so when you call test.Trim(), it will not modify the test variable, it will return a new string: 确保记住一个字符串是不可变的,因此当您调用test.Trim()时,它不会修改测试变量,它将返回一个新字符串:


string trimmed = test.Trim();
// or
string trimmed = test.TrimStart();

如何使用trim()函数:

string test = test.trim();

test.trim();
此方法从字符串的开头和结尾删除空格。

You can use: 您可以使用:

All these have additional overloads which let you specify an array of custom characters also to be removed. 所有这些都有额外的重载,您可以通过它们指定要删除的自定义字符数组。

Speaking of Java : String.trim() . 说到JavaString.trim() Here is the link to the API documentation: String.trim() 这是API文档的链接: String.trim()

In java: 在Java中:

Use trim() to remove the whitespaces around a String: 使用trim()删除字符串周围的空格:

String test2 = test.trim();

Returns a copy of the string, with leading and trailing whitespace omitted. 返回字符串的副本,省略前导和尾随空格。

I can also use the method TrimStart() 我也可以使用TrimStart()方法

like this: 像这样:

string test2 = test.TrimStart(' ');

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

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