简体   繁体   中英

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. Why?

. means "any character" in regular expressions. So don't split using a regex - split using 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(".");

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

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[] 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

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:

@"\."

Now, this case is somewhat simple for Regex matching; you could instead use String.Split() which will split based on the occurrence of one or more static strings or characters:

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...

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. Check out this page for common operators.

尝试

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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