简体   繁体   English

C#为什么我不能拆分字符串?

[英]C# Why i can not split the string?

 string myNumber = "3.44";

       Regex regex1 = new Regex(".");

       string[] substrings = regex1.Split(myNumber);

        foreach (var substring in substrings)
        {
            Console.WriteLine("The string is : {0} and the length is {1}",substring, substring.Length);
        }

        Console.ReadLine();

I tried to split the string by ".", but it the splits return 4 empty string. 我试图用“。”拆分字符串,但是拆分返回4个空字符串。 Why? 为什么?

. means "any character" in regular expressions. 表示正则表达式中的“任何字符”。 So don't split using a regex - split using String.Split : 所以不要使用正则表达式进行拆分 - 使用String.Split进行String.Split

string[] substrings = myNumber.Split('.');

If you really want to use a regex, you could use: 如果你真的想使用正则表达式,你可以使用:

Regex regex1 = new Regex(@"\.");

The @ makes it a verbatim string literal, to stop you from having to escape the backslash. @使它成为一个逐字的字符串文字,以阻止你不得不逃避反斜杠。 The backslash within the string itself is an escape for the dot within the regex parser. 字符串本身内的反斜杠是正则表达式解析器中点的转义。

最简单的解决方案是: string[] val = myNumber.Split('.');

. is a reserved character in regex. 是正则表达式中的保留字符。 if you literally want to match a period, try: 如果你真的想要匹配一段时间,请尝试:

Regex regex1 = new Regex(@"\.");

However, you're better off simply using myNumber.Split("."); 但是,你最好只使用myNumber.Split(".");

The dot matches a single character, without caring what that character is. 该点匹配单个字符,而不关心该字符是什么。 The only exception are newline characters. 唯一的例外是换行符。

Source: http://www.regular-expressions.info/dot.html 资料来源: http//www.regular-expressions.info/dot.html

Therefore your implying in your code to split the string at each character. 因此,您在代码中暗示要在每个字符处拆分字符串。

Use this instead. 请改用它。

string substr = num.Split('.');

Keep it simple, use String.Split() method; 保持简单,使用String.Split()方法;

string[] substrings = myNumber.Split('.'); 

It has an other overload which allows specifying split options: 它有另一个重载 ,允许指定拆分选项:

public string[] Split(
    char[] separator,
    StringSplitOptions options
)

You don't need regex you do that by using Split method of string object 你不需要正则表达式,你可以使用字符串对象的Split方法

string myNumber = "3.44";
String[] substrings = myNumber.Split(".");
foreach (var substring in substrings)
{
   Console.WriteLine("The string is : {0} and the length is {1}",substring, substring.Length);
}

Console.ReadLine();

In Regex patterns, the period character matches any single character. 在正则表达式模式中,句点字符匹配任何单个字符。 If you want the Regex to match the actual period character, you must escape it in the pattern, like so: 如果您希望Regex与实际句点字符匹配,则必须在模式中将其转义,如下所示:

@"\."

Now, this case is somewhat simple for Regex matching; 现在,这种情况对于Regex匹配来说有点简单; you could instead use String.Split() which will split based on the occurrence of one or more static strings or characters: 您可以改为使用String.Split(),它将根据一个或多个静态字符串或字符的出现进行拆分:

string[] substrings = myNumber.Split('.');

The period "." 时期 ”。” is being interpreted as any single character instead of a literal period. 被解释为任何单个字符而不是文字句点。

Instead of using regular expressions you could just do: 您可以这样做,而不是使用正则表达式:

string[] substrings = myNumber.Split(".");

try 尝试

Regex regex1 = new Regex(@"\.");

EDIT: Er... I guess under a minute after Jon Skeet is not too bad, anyway... 编辑:呃...我想在Jon Skeet不是太糟糕的一分钟之后,无论如何......

You'll want to place an escape character before the "." 你想在“。”之前放置一个转义字符。 - like this "\\\\." - 像这样 ”\\\\。”

"." “” in a regex matches any character, so if you pass 4 characters to a regex with only ".", it will return four empty strings. 在正则表达式匹配任何字符,所以如果你传递4个字符到只有“。”的正则表达式,它将返回四个空字符串。 Check out this page for common operators. 查看此页面以了解常见操作员。

尝试

 Regex regex1 = new Regex("[.]");

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

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