最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Simple string.Split failing with StringSplitOptions.TrimEntries - Stack Overflow

programmeradmin2浏览0评论

This returns an array with only one element and thus throws an exception. But it works when I omit StringSplitOptions.TrimEntries.

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("a-b".Split('+', '-', StringSplitOptions.TrimEntries)[1]);
    }
}

Could it be that it is picking this overload:

public string[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None)

But since when in C# is char compatible with int?

This returns an array with only one element and thus throws an exception. But it works when I omit StringSplitOptions.TrimEntries.

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("a-b".Split('+', '-', StringSplitOptions.TrimEntries)[1]);
    }
}

Could it be that it is picking this overload:

public string[] Split(char separator, int count, StringSplitOptions options = StringSplitOptions.None)

But since when in C# is char compatible with int?

Share Improve this question edited yesterday Dale K 27.2k15 gold badges56 silver badges82 bronze badges asked yesterday codymanixcodymanix 29.5k21 gold badges97 silver badges153 bronze badges 4
  • 1 This question is similar to: Implicit type cast of char to int in C#. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Charlieface Commented yesterday
  • 1 JFYI - "a-b".Split(['+', '-'], StringSplitOptions.TrimEntries)[1] should do what is needed (if I understand correctly the goal). – Guru Stron Commented yesterday
  • And decompilation of your code to C# @sharplab.io – Guru Stron Commented yesterday
  • As a tip - in future if you are passing a variable number of parameters of a given type (i.e. params) and then after that if pass a different type of parameter that will be the exact issue you are facing. i.e. without looking at the docs I know your code isn't going to work since you are passing StringSplitOptions.TrimEntries after the params. – mjwills Commented yesterday
Add a comment  | 

2 Answers 2

Reset to default 6

since when in c# char is compatible with int?

char has been implicitly convertible to int in C# since version 1.

Could it be that it is picking this overload: ...

It has to be; it is the only overload that fits the method call. If this overload didn't exist, the code wouldn't compile.

I presume you expected this overload instead:

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

However, this option is not compatible with the provided arguments, because the params modifier is not included as part of the signature (params must come last in the argument list). Therefore the '+', '-' portion of the method call cannot be interpreted as an array here. Nor is there an overload using IEnumerable<char> that could be chosen.

If you wish to use this other overload, you must ensure the array is fully-constructed before the method call resolves.

Yes, a char is implicitly convertible to int

Demo

发布评论

评论列表(0)

  1. 暂无评论