在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
欢迎阅读我们的 C# 便捷 Find
函数教程。 您刚刚发现了一个可以简化编码过程的强大功能。 因此,无论您是经验丰富的程序员还是刚刚入门,本教程都将指导您掌握所有要素,让您轻松上手。
Find "的核心功能是查找集合、数组或列表中满足指定谓词的第一个元素。 你会问什么是谓词? 在编程中,谓词是一个函数,用于测试为集合中的元素定义的某些条件。
现在,让我们深入一个公共类示例。
public class BikePart
{
public string id { get; set; }
public override bool Equals(object obj)
{
if (obj == null
!(obj is BikePart))
return false;
return this.id == ((BikePart)obj).id;
}
public override int GetHashCode()
{
return this.id.GetHashCode();
}
public override string ToString()
{
return "BikePart ID: " + this.id;
}
}
public class BikePart
{
public string id { get; set; }
public override bool Equals(object obj)
{
if (obj == null
!(obj is BikePart))
return false;
return this.id == ((BikePart)obj).id;
}
public override int GetHashCode()
{
return this.id.GetHashCode();
}
public override string ToString()
{
return "BikePart ID: " + this.id;
}
}
Public Class BikePart
Public Property id() As String
Public Overrides Function Equals(ByVal obj As Object) As Boolean
If obj Is Nothing (Not (TypeOf obj Is BikePart)) Then
Return False
End If
Return Me.id = DirectCast(obj, BikePart).id
End Function
Public Overrides Function GetHashCode() As Integer
Return Me.id.GetHashCode()
End Function
Public Overrides Function ToString() As String
Return "BikePart ID: " & Me.id
End Function
End Class
在这段代码中,"BikePart "是我们的公共类,它包含一个公共字符串 ID,用于标识每个自行车零件。 我们重载了 ToString
方法,以便很好地打印自行车零件的 ID,我们还重载了 Equals
和 GetHashCode
方法,以便进行比较。
现在我们有了 "BikePart "类,可以创建一个自行车零件列表,并使用查找根据 ID 定位特定部件。 让我们来看看下面的例子:
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
BikePart chainRingPart = bikeParts.Find(findChainRingPredicate);
Console.WriteLine(chainRingPart.ToString());
}
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
BikePart chainRingPart = bikeParts.Find(findChainRingPredicate);
Console.WriteLine(chainRingPart.ToString());
}
Public Shared Sub Main()
Dim bikeParts As New List From {
New BikePart With {.id = "Chain Ring ID"},
New BikePart With {.id = "Crank Arm ID"},
New BikePart With {.id = "Regular Seat ID"},
New BikePart With {.id = "Banana Seat ID"}
}
Dim findChainRingPredicate As Predicate = Function(bp As BikePart)
Return bp.id = "Chain Ring ID"
End Function
Dim chainRingPart As BikePart = bikeParts.Find(findChainRingPredicate)
Console.WriteLine(chainRingPart.ToString())
End Sub
在这段代码中,我们实例化了四个具有唯一 ID 的 BikePart
对象。 接下来,我们创建一个谓词 findChainRingPredicate
来检查自行车零件是否具有 ID "链环 ID"。 最后,我们使用定义的谓词在自行车零件列表中调用 Find
,并将找到的零件 ID 打印到控制台。
您可能想知道我们的 "Find "方法中的谓词匹配参数。 在此定义 Find
方法返回元素的条件。 在我们的案例中,我们希望 Find
方法返回与 "链环 ID "匹配的第一个元素。
如果没有元素满足您在谓词中定义的条件,Find
方法将返回一个默认值。 例如,如果您正在处理一个整数数组,而您的谓词没有找到匹配,"Find "方法将返回 "0",即 C# 中整数的默认值。
必须注意的是,"Find "函数在整个数组、列表或集合中进行线性搜索。 这意味着它会从第一个元素开始,依次检查后面的每个元素,直到找到满足谓词的元素的首次出现。
在某些情况下,您可能希望找到满足谓词的最后一个元素,而不是第一个元素。 为此,C# 提供了 FindLast
函数。
正如 Find
可以帮助您找到符合指定谓词的元素的首次出现,C# 也提供了 FindIndex
和 FindLastIndex
方法,分别为您提供符合条件的第一个和最后一个元素的索引。
让我们举个例子:
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
new BikePart { id = "Chain Ring ID" }, // Added a second chain ring
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
int firstChainRingIndex = bikeParts.FindIndex(findChainRingPredicate);
int lastChainRingIndex = bikeParts.FindLastIndex(findChainRingPredicate);
Console.WriteLine($"First Chain Ring ID found at index: {firstChainRingIndex}");
Console.WriteLine($"Last Chain Ring ID found at index: {lastChainRingIndex}");
}
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
new BikePart { id = "Chain Ring ID" }, // Added a second chain ring
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
int firstChainRingIndex = bikeParts.FindIndex(findChainRingPredicate);
int lastChainRingIndex = bikeParts.FindLastIndex(findChainRingPredicate);
Console.WriteLine($"First Chain Ring ID found at index: {firstChainRingIndex}");
Console.WriteLine($"Last Chain Ring ID found at index: {lastChainRingIndex}");
}
Public Shared Sub Main()
Dim bikeParts As New List From {
New BikePart With {.id = "Chain Ring ID"},
New BikePart With {.id = "Crank Arm ID"},
New BikePart With {.id = "Regular Seat ID"},
New BikePart With {.id = "Banana Seat ID"},
New BikePart With {.id = "Chain Ring ID"}
}
Dim findChainRingPredicate As Predicate = Function(bp As BikePart)
Return bp.id = "Chain Ring ID"
End Function
Dim firstChainRingIndex As Integer = bikeParts.FindIndex(findChainRingPredicate)
Dim lastChainRingIndex As Integer = bikeParts.FindLastIndex(findChainRingPredicate)
Console.WriteLine($"First Chain Ring ID found at index: {firstChainRingIndex}")
Console.WriteLine($"Last Chain Ring ID found at index: {lastChainRingIndex}")
End Sub
FindAll "方法,顾名思义,检索集合中所有满足谓词的元素。 当您需要根据特定条件过滤元素时,就会用到它。 FindAll "方法返回一个包含所有匹配元素的新 List。
下面是一个代码示例:
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
new BikePart { id = "Chain Ring ID" }, // Added a second chain ring
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
List chainRings = bikeParts.FindAll(findChainRingPredicate);
Console.WriteLine($"Found {chainRings.Count} Chain Rings:");
foreach (BikePart chainRing in chainRings)
{
Console.WriteLine(chainRing.ToString());
}
}
public static void Main()
{
List bikeParts = new List
{
new BikePart { id = "Chain Ring ID" },
new BikePart { id = "Crank Arm ID" },
new BikePart { id = "Regular Seat ID" },
new BikePart { id = "Banana Seat ID" },
new BikePart { id = "Chain Ring ID" }, // Added a second chain ring
};
Predicate findChainRingPredicate = (BikePart bp) => { return bp.id == "Chain Ring ID"; };
List chainRings = bikeParts.FindAll(findChainRingPredicate);
Console.WriteLine($"Found {chainRings.Count} Chain Rings:");
foreach (BikePart chainRing in chainRings)
{
Console.WriteLine(chainRing.ToString());
}
}
Public Shared Sub Main()
Dim bikeParts As New List From {
New BikePart With {.id = "Chain Ring ID"},
New BikePart With {.id = "Crank Arm ID"},
New BikePart With {.id = "Regular Seat ID"},
New BikePart With {.id = "Banana Seat ID"},
New BikePart With {.id = "Chain Ring ID"}
}
Dim findChainRingPredicate As Predicate = Function(bp As BikePart)
Return bp.id = "Chain Ring ID"
End Function
Dim chainRings As List = bikeParts.FindAll(findChainRingPredicate)
Console.WriteLine($"Found {chainRings.Count} Chain Rings:")
For Each chainRing As BikePart In chainRings
Console.WriteLine(chainRing.ToString())
Next chainRing
End Sub
我们的 C# Find 知识可以利用的一个关键领域是使用 IronPDF 进行 PDF 内容操作,IronPDF 是一个功能强大的用于 PDF 处理的 C# 库。
假设我们正在处理一份包含各种自行车零件信息的 PDF 文档。 通常,我们需要在这些内容中找到特定部分。 这就是 IronPdf 和 C# Find 方法结合提供强大解决方案的地方。
首先,我们将使用IronPDF至从我们的 PDF 中提取文本然后,我们可以使用前面学习过的 Find
或 FindAll
方法在提取的文本中找到特定部分。
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile(@"C:\Users\Administrator\Desktop\bike.pdf");
string pdfText = pdf.ExtractAllText();
// Assuming our PDF text is split into lines
List pdfLines = pdfText.Split('\n').ToList();
Predicate findChainRingPredicate = (string line) => { return line.Contains("Chain Ring ID"); };
List chainRingLines = pdfLines.FindAll(findChainRingPredicate);
Console.WriteLine($"Found {chainRingLines.Count} lines mentioning 'Chain Ring ID':");
foreach (string line in chainRingLines)
{
Console.WriteLine(line);
}
using IronPdf;
PdfDocument pdf = PdfDocument.FromFile(@"C:\Users\Administrator\Desktop\bike.pdf");
string pdfText = pdf.ExtractAllText();
// Assuming our PDF text is split into lines
List pdfLines = pdfText.Split('\n').ToList();
Predicate findChainRingPredicate = (string line) => { return line.Contains("Chain Ring ID"); };
List chainRingLines = pdfLines.FindAll(findChainRingPredicate);
Console.WriteLine($"Found {chainRingLines.Count} lines mentioning 'Chain Ring ID':");
foreach (string line in chainRingLines)
{
Console.WriteLine(line);
}
Imports Microsoft.VisualBasic
Imports IronPdf
Private pdf As PdfDocument = PdfDocument.FromFile("C:\Users\Administrator\Desktop\bike.pdf")
Private pdfText As String = pdf.ExtractAllText()
' Assuming our PDF text is split into lines
Private pdfLines As List = pdfText.Split(ControlChars.Lf).ToList()
Private findChainRingPredicate As Predicate = Function(line As String)
Return line.Contains("Chain Ring ID")
End Function
Private chainRingLines As List = pdfLines.FindAll(findChainRingPredicate)
Console.WriteLine($"Found {chainRingLines.Count} lines mentioning 'Chain Ring ID':")
For Each line As String In chainRingLines
Console.WriteLine(line)
Next line
在这段代码中,我们加载了 PDF,提取了文本,将其分成几行,然后使用 "FindAll "查找所有提到 "Chain Ring ID "的行。
这是一个在实际场景中如何将 Find
方法与 IronPDF 结合使用的基本示例。 它展示了 C# 及其强大库的实用性和多功能性,有助于使您的编程任务更轻松、更高效。
在本教程中,我们深入研究了 C# Find
方法及其近似方法 FindIndex
、FindLastIndex
和 FindAll
。 我们探讨了这些工具的用途,探究了一些代码示例,并揭示了它们在哪些情况下最为有效。
我们还使用 IronPDF 库涉足 PDF 操作领域。 同样,在提取和搜索 PDF 文档中的内容时,我们看到了 "Find "方法知识的实际应用。
IronPDF 提供免费的试用 IronPDF此外,《C# .NET》还提供了一个绝佳的机会来探索其功能,并确定如何使您的 C# 项目受益。 如果您决定在试用后继续使用 IronPdf,许可证从 $749 起。