简体   繁体   English

如何检查字符串的第一个字符是空格还是制表符?

[英]How can I check if the first character of my string is a space or tab character?

I asked a question about removing spaces and tabs. 我问了一个关于删除空格和制表符的问题。 Now I have something else needed. 现在我还需要其他东西。 I need to be able to check if the first character of a string is a space or tab. 我需要能够检查字符串的第一个字符是空格还是制表符。 Can anyone think of a good way to do this. 任何人都可以想到一个很好的方法来做到这一点。

Thanks, 谢谢,

The best way is to use the Char.IsWhiteSpace method: 最好的方法是使用Char.IsWhiteSpace方法:

// Normally, you would also want to check that the input is valid (e.g. not null)
var input = "blah";

var startsWithWhiteSpace = char.IsWhiteSpace(input, 0); // 0 = first character
if (startsWithWhiteSpace)
{
    // your code here
}

The method's documentation explicitly mentions what exactly is considered white space; 该方法的文档明确提到了什么是白色空间; if for some reason that list of characters does not suit your needs, you will have to do a more restrictive check manually. 如果由于某种原因,字符列表不符合您的需要,您将不得不手动进行更严格的检查。

You can check it like this: 你可以这样检查:

if( myString[0] == ' ' || myString[0] == '\t' ){
  //do your thing here
}

This will fail for emtpy and null strings, so you should probably make it a bit more secure like this: 对于emtpy和null字符串,这将失败,所以你应该让它更安全,如下所示:

if( !string.IsNullOrEmpty(myString) && (myString[0] == ' ' || myString[0] == '\t') ){
  //do your thing here
}

I would just go with something like: 我会选择以下内容:

if (str.StartsWith(" ") || str.StartsWith("\t")) {
    ...
}

or: 要么:

if ((str[0] == ' ') || (str[0] == "\t")) {
    ...
}

I actually prefer the former since you don't have to worry about problems with empty strings. 我实际上更喜欢前者,因为你不必担心空字符串的问题。

If you want to handle more complex case in future, you could use regular expressions, something like: 如果您希望将来处理更复杂的案例,可以使用正则表达式,例如:

if (Regex.IsMatch (str, @"^\s")) ...

This can be modified to handle arbitrarily complex cases, though it's a bit like killing flies with a thermo-nuclear warhead for your particular case. 这可以修改为处理任意复杂的情况,虽然这有点像为你的特定情况杀死带有热核弹头的苍蝇。

If you are not only interested in space and tab but in whitespace in general, use this: 如果您不仅对空间和制表符感兴趣,而且通常在空白中,请使用:

if(!string.IsNullOrEmpty(myString) && char.IsWhiteSpace(myString[0]))
    // It's a whitespace
if (text[0] == ' ' || text[0] == '\t')

And just for fun: 而且只是为了好玩:

if (Regex.IsMatch(input, @"^\s")) {
  // yep, starts with whitespace
}

Note that while many of the other answers will fail when given an empty string, this one will work, and it would be easily extensible to match more complex things than just whitespace at the beginning of the string. 请注意,虽然许多其他答案在给定空字符串时会失败,但是这个答案会起作用,并且它很容易扩展以匹配比字符串开头的空格更复杂的东西。 Some people find regexes to be overkill for simple checks, but others believe that if you want to do pattern matching on strings, you should use regexes. 有些人发现正则表达式对于简单检查来说是过度杀伤,但是其他人认为如果你想对字符串进行模式匹配,你应该使用正则表达式。

if(myString[0]==' ' || myString[0]=='\t') 
{
    //do something
}

You'll need to have the System.Linq namespace imported. 您需要导入System.Linq命名空间。

var firstToken = yourString.FirstOrDefault();
if (firstToken == ' ' || firstToken == '\t')
{
   // First character is a space or tab
}
else
{
   // First character is not a space or tab, or string is empty.
}

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

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