简体   繁体   English

使用Func <string, bool> ,如何分割一个字符,然后计算一个函数中这些值的长度

[英]using Func<string, bool>, how can I split on a character then count the length of those values within a function

Essentially, I need to split a string like "aaaaa.bbbbbbbb.cccccc" on the . 本质上,我需要在上拆分一个字符串,例如"aaaaa.bbbbbbbb.cccccc" and then count the length of the split values using a function. 然后使用函数计算分割值的长度。

Func<string, bool> length = f => f.Split(".").Length > 1;

pretty much this but instead of counting the length of the split array, I need to count how many letters per entry of the array and see if they are over a certain length. 几乎可以做到这一点,但我不需要计算拆分数组的长度,而是需要计算数组每个条目的字母数,看看它们是否超过一定长度。

If you need a boolean answer, then it's either one of the following (depdending on if you want at least one substring with length > 1 or you want all of them: 如果您需要布尔回答,则可以是以下之一(取决于您是否希望至少一个长度大于1的子字符串,或者全部都需要:

    Func<string, bool> length1 = f => f.Split('.').Any(s => s.Length > 1);
    Func<string, bool> length2 = f => f.Split('.').All(s => s.Length > 1);

I think you are trying to do this: 我认为您正在尝试这样做:

string input = "aaaaa.bbbbbbbb.cccccc";
var parts = input.Split('.');
var lengths = parts.Select(e=>e.Count());

如果您按字母表示:aaaaa.bbbbbbbbbb.cccccc-> a = 5,b = 8,c = 6

Func<string, IEnumerable<int>> length = f => f.Split('.').Select(a => a.Length)

From what I understand, you need to take a string, split it only by the delimiter '.', and then count to see if each split value is greater than 1. 据我了解,您需要获取一个字符串, 仅用定界符'。'对其进行分割,然后计数以查看每个分割值是否大于1。

If so, you can use the following: 如果是这样,您可以使用以下方法:

Func<string, bool> length = str => str.Split('.').All(s => s.Length > 1);

This will first split the string by your delimiter, then iterate on all of the values to check if they are greater than 1. 这将首先通过定界符分割字符串,然后迭代所有值以检查它们是否大于1。

Quick test case: 快速测试案例:

string test1 = "aaa.b.ccccccc";
string test2 = "aaaaaa.bbb.c";
string test3 = "aaa.bbb.ccccc";

Console.WriteLine(length(test1)); // false, as b is 1, not greater
Console.WriteLine(length(test2)); // false, for similar reasons
Console.WriteLine(length(test3)); // true, all are greater

what about this [Example reference] : [参考示例]呢

Func<string, List<int>> length = f => f.Split('.').Select(x=>x.Length).ToList();

And call the method like this: 并调用如下方法:

string inputStr = "aaaaa.bbbbbbbb.cccccc";
Console.WriteLine(String.Join(",",length(inputStr))); // prints 5,8,6

Please note: the second parameter in the Func denotes the return type, Here in the example I used it as List<int> that's why I added .ToList() at the end of the code. 请注意: Func的第二个参数表示返回类型,在此示例中,我将其用作List<int> ,这就是为什么在代码末尾添加.ToList()的原因。 You can change the return types accordingly. 您可以相应地更改返回类型。 If you are ok with IEnumerable<int> then 如果您对IEnumerable<int>那么

Func<string, IEnumerable<int>> length = f => f.Split('.').Select(x=>x.Length) 

is enough. 足够。

Another way using a func delegate: 使用func委托的另一种方法:

Func<string, Tuple<string[], int[]>> length = (str) => {
    string[] stringParts = str.Split('.');
    int[] countLetters = stringParts.Select(s => s.Length).ToArray();
    return new Tuple<string[], int[]>(stringParts, countLetters);
};

string input = "aaaaa.bbbbbbbb.cccccc";

var res = length(input);
string[] strs = res.Item1;
int[] countLetter = res.Item2;


for (int i = 0; i < strs.Length; i++)
{
    Console.WriteLine(strs[i]);
    Console.WriteLine(countLetter[i]);
}

Output: 输出:

aaaaa
5
bbbbbbbb
8
cccccc
6

An extension method would perhaps be an easier way to do this. 扩展方法可能是一种更简单的方法。

How about you write a method that returns list of strings that above the length you specify, something like this: 如何编写返回大于指定长度的字符串列表的方法,如下所示:

IEnumerable<string> GetSplittedAboveLimit(string inputString,int limit)
{
   var splitted = inputString.Split(".");
   foreach(var input in splitted)
   {
      if(input.Length > limit)
      {
          yield return input; 
      }
   }
}

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

相关问题 如何将这些字符串转换为bool? - How can I convert those string to bool? 如何转换此功能 <SampleExpression,IEnumerator<string> ,bool &gt;&gt;到Func <SampleExpression,bool> &gt; - How do i convert this Func<SampleExpression,IEnumerator<string>,bool>> to Func<SampleExpression,bool>> 如何根据字符长度拆分字符串 - how to split string according to character length 如何使用Task输出字符串值 <bool> C#中的功能 - How can out string values with Task<bool> function in C# 如何使用正则表达式拆分字符串以返回值列表? - How can I split a string using regex to return a list of values? 如何使用Func <T,bool> 传入对象? - How can I used Func<T,bool> that passed in object? 如何访问Func目标的值 <Client, bool> ? - How can I access the value of the target of a Func<Client, bool>? 如何从Expression <Func <MyClass,string >>动态创建Expression <Func <MyClass,bool >>谓词? - How do I dynamically create an Expression<Func<MyClass, bool>> predicate from Expression<Func<MyClass, string>>? 我可以动态创建一个表达式 <Func<T, bool> &gt;谓词,但我如何创建表达式 <Func<T1,T2,bool> &gt; - I can dynamically create an Expression<Func<T, bool>> predicate ,but how do i create Expression<Func<T1,T2,bool>> 如何在括号内用逗号分隔字符串,而不是用双引号分割字符串 - How to split string by comma within parenthesis but not those on double quote
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM