Results 1 to 21 of 21

Thread: [RESOLVED] Extract word between "

Hybrid View

  1. #1
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Extract word between "

    csharp Code:
    1. public static void SomeMethod()
    2. {
    3.     const string findStr = @""";
    4.     List<string> words = GetAllWords(File.ReadAllText(@"Z:\quotes.txt"), findStr);
    5.     words.ForEach(Console.WriteLine);
    6. }
    7. static List<string> GetAllWords(string source, string findStr)
    8. {
    9.     List<string> words = new List<string>();
    10.     int pos, index = 0;
    11.     while ((pos = source.IndexOf(findStr, index, StringComparison.OrdinalIgnoreCase)) > -1)
    12.     {
    13.         index = pos + findStr.Length;
    14.         pos = source.IndexOf(findStr, index, StringComparison.OrdinalIgnoreCase);
    15.         if (pos > -1)
    16.         {
    17.             words.Add(source.Substring(index, pos - index));
    18.             index = pos + findStr.Length;
    19.         }
    20.     }
    21.     return words;
    22. }

    This is C# but the same principle still applies to VB.NET. (I had a C# project open at the time, but if you need me to translate this to VB.NET I can write an example together in VB.NET.)

    Here was the test paragraph I used:
    Lorem &quot;ipsum&quot; dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum &quot;iriure&quot; dolor &quot;in&quot; hendrerit &quot;in&quot; vulputate velit esse molestie consequat, vel &quot;illum&quot; dolore eu feugiat nulla facilisis at vero eros et accumsan et &quot;iusto&quot; odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor *** soluta nobis eleifend option congue nihil &quot;imperdiet&quot; doming &quot;id&quot; quod mazim placerat facer possim assum. Typi non habent claritatem &quot;insitam&quot;; est usus legentis &quot;in&quot; &quot;iis&quot; qui facit eorum claritatem. &quot;investigationes&quot; demonstraverunt lectores legere me lius quod &quot;ii&quot; legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes &quot;in&quot; futurum.&quot;
    Along with the results:
    Code:
    ipsum
    iriure
    in
    in
    illum
    iusto
    imperdiet
    id
    insitam
    in
    iis
    investigationes
    ii
    in
    Quick VB.NET conversion:
    vbnet Code:
    1. Public Sub SomeMethod()
    2.     Const findStr As String = "&quot;"
    3.     Dim words As List(Of String) = GetAllWords(File.ReadAllText("Z:\quotes.txt"), findStr)
    4.     words.ForEach(Sub(n) Console.WriteLine(n))
    5. End Sub
    6.  
    7. Private Function GetAllWords(source As String, findStr As String) As List(Of String)
    8.     Dim words As New List(Of String)
    9.     Dim index As Integer = 0
    10.     Dim pos As Integer = source.IndexOf(findStr, index, StringComparison.OrdinalIgnoreCase)
    11.     While pos > -1
    12.         index = pos + findStr.Length
    13.         pos = source.IndexOf(findStr, index, StringComparison.OrdinalIgnoreCase)
    14.         If pos > -1 Then
    15.             words.Add(source.Substring(index, pos - index))
    16.             index = pos + findStr.Length
    17.         End If
    18.         pos = source.IndexOf(findStr, index, StringComparison.OrdinalIgnoreCase)
    19.     End While
    20.     Return words
    21. End Function
    Last edited by AceInfinity; Nov 28th, 2013 at 04:35 PM.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width