在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
歡迎來到我們關於 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
類別,我們可以建立一個自行車零件的清單並使用 查找
根據其 IDs 定位特定部分。我們來看以下例子:
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
,用來檢查自行車部件是否具有 "Chain Ring ID"。最後,我們使用我們定義的謂詞來調用 Find
以在自行車部件列表中進行搜尋,並將找到的部件的 ID 輸出到控制台。
您可能對我們 Find
方法中的 Predicate match 參數感到好奇。這是您定義 Find
方法返回元素的條件的地方。在我們的案例中,我們希望 Find
方法返回第一個與 "Chain Ring ID" 相匹配的元素。
如果沒有元素滿足您謂詞中定義的條件,Find
方法將返回一個預設值。例如,如果您在處理一個整數數組且您的謂詞未找到匹配項,Find
方法將返回 '0',這是 C# 中整數的預設值。
請注意,Find
函數在整個陣列、列表或集合中進行線性搜尋。這意味著它從第一個元素開始,依序檢查每個後續元素,直到找到第一個符合條件的元素為止。
在某些情況下,您可能希望找到最後一個符合條件的元素,而不是第一個。為此,C# 提供了 FindLast
函數。
FindIndex
和 FindLastIndex
就像 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
方法,如名字所述,檢索集合中滿足條件的所有元素。當你需要根據特定條件過濾元素時使用。FindAll
方法返回一個包含所有匹配元素的新清單。
以下是一個程式碼示例:
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# 查找知識可以應用的一個重要領域是使用 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 處理的世界。同樣,我們看到了如何運用我們的 Find
方法知識來提取和搜索 PDF 文檔中的內容的實際應用。
IronPDF 提供一個免費 試用,提供了絕佳的機會來探索其功能並確定它如何能夠使您的 C# 專案受益。如果您決定在試用期後繼續使用 IronPDF,授權價格從 $749 開始。