2017-06-21 55 views
-2

我有這樣的文字:我想找到在哪個位置都是特定的字母

「555」;」general」;」pfss」;」16」 
「444」;」compa」;」sss」;」5」 

在Visual Basic 2008 Express Edition的

我想找到在哪個位置都是;

的結果必然是:

5,15,22,32,40,46 
+1

好的,什麼是問題該解決方案?如果答案是*我不知道該怎麼做*,你需要做一些(更多)研究。這不是一個教程網站 – Plutonix

+1

我懷疑,通過獲得這些職位你想拆分領域,另一個原因不介意。在這種情況下,研究'拆分':) – Herb

+0

我建議看看[TextFieldParser](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v = vs.110 ).aspx)而不是使用分隔符的索引。 – Filburt

回答

0

沒有現有的方法,但你可以隨便寫一個。例如,作爲擴展名:

Imports System.Runtime.CompilerServices 

Module Extensions 

    <Extension()> 
    Public Function FindAllIndex(Of T)(ByVal items As IEnumerable(Of T), predicate As Func(Of T, Boolean)) As Int32() 
     Dim allIndexes As New List(Of Int32) 
     Dim index As Int32 = 0 

     For Each item As T In items 
      If predicate(item) Then 
       allIndexes.Add(index) 
      End If 
      index += 1 
     Next 

     Return allIndexes.ToArray() 
    End Function 

End Module 

用法:

Dim allIndexes as Int32() = text.FindAllIndex(Function(c) c = ";"c) 

此通用版本支持任何類型和條件。它在VS2008下工作。

0

我建議Linq,並處理您的文本中包含的回車/換行 - 至少如果您期望由您的期望輸出指定確切的索引。

Dim chars = text.Replace(Environment.NewLine, String.Empty).ToCharArray() 
Dim positions = Enumerable.Range(0, chars.Length).Where(Function(i) chars(i) = ";"c).ToArray() 

注意
這個答案是專爲所提到的輸出值 - 但它很可能不會幫你什麼都任務中,您在做什麼。


免責聲明:我翻譯從diesel的評論c# Array.FindAllIndexOf which FindAll IndexOf

相關問題