Searching text in ASP.net

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jty202

    Searching text in ASP.net

    I use a the function Instr() in VB6 to search for a search-string in String
    var. It returns the position of the found search-string.

    Is there such a funtion in ASP.net?


  • Stuart A Hill

    #2
    Re: Searching text in ASP.net

    Hi,

    You could try this:

    Dim findIt as String = "The String"
    Dim startPosition as Integer
    Dim position as integer = findIt.IndexOf( "String you are looking in",
    startPosition)

    Hope this helps.

    Stuart
    MCSD, MCT

    More Examples from MSDN:

    [Visual Basic]
    ' Sample for String.IndexOf( Char, Int32)
    Imports System

    Class Sample
    Public Shared Sub Main()

    Dim br1 As String =
    "0----+----1----+----2----+----3----+----4----+----5----+----6----+-"
    Dim br2 As String =
    "01234567890123 456789012345678 901234567890123 456789012345678 90123456"
    Dim str As String = "Now is the time for all good men to come to the
    aid of their party."
    Dim start As Integer
    Dim at As Integer

    Console.WriteLi ne()
    Console.WriteLi ne("All occurrences of 't' from position 0 to {0}.",
    str.Length - 1)
    Console.WriteLi ne("{1}{0}{2}{0 }{3}{0}", Environment.New Line, br1, br2,
    str)
    Console.Write(" The letter 't' occurs at position(s): ")

    at = 0
    start = 0
    While start < str.Length AndAlso at > - 1
    at = str.IndexOf("t" c, start)
    If at = - 1 Then
    Exit While
    End If
    Console.Write(" {0} ", at)
    start = at + 1
    End While
    Console.WriteLi ne()
    End Sub 'Main
    End Class 'Sample
    '
    'This example produces the following results:
    '
    'All occurrences of 't' from position 0 to 66.
    '0----+----1----+----2----+----3----+----4----+----5----+----6----+-
    '01234567890123 456789012345678 901234567890123 456789012345678 90123456
    'Now is the time for all good men to come to the aid of their party.
    '
    'The letter 't' occurs at position(s): 7 11 33 41 44 55 64
    '
    '

    [C#]
    // Sample for String.IndexOf( Char, Int32)
    using System;

    class Sample {
    public static void Main() {

    string br1 =
    "0----+----1----+----2----+----3----+----4----+----5----+----6----+-";
    string br2 =
    "01234567890123 456789012345678 901234567890123 456789012345678 90123456";
    string str = "Now is the time for all good men to come to the aid of
    their party.";
    int start;
    int at;

    Console.WriteLi ne();
    Console.WriteLi ne("All occurrences of 't' from position 0 to {0}.",
    str.Length-1);
    Console.WriteLi ne("{1}{0}{2}{0 }{3}{0}", Environment.New Line, br1, br2,
    str);
    Console.Write(" The letter 't' occurs at position(s): ");

    at = 0;
    start = 0;
    while((start < str.Length) && (at > -1))
    {
    at = str.IndexOf('t' , start);
    if (at == -1) break;
    Console.Write(" {0} ", at);
    start = at+1;
    }
    Console.WriteLi ne();
    }
    }
    /*
    This example produces the following results:

    All occurrences of 't' from position 0 to 66.
    0----+----1----+----2----+----3----+----4----+----5----+----6----+-
    012345678901234 567890123456789 012345678901234 567890123456789 0123456
    Now is the time for all good men to come to the aid of their party.

    The letter 't' occurs at position(s): 7 11 33 41 44 55 64


    "jty202" <jty202@gmail.c om> wrote in message
    news:e%232sNsN$ EHA.2104@TK2MSF TNGP14.phx.gbl. ..[color=blue]
    >I use a the function Instr() in VB6 to search for a search-string in String
    > var. It returns the position of the found search-string.
    >
    > Is there such a funtion in ASP.net?
    >
    >[/color]


    Comment

    • Matt Berther

      #3
      Re: Searching text in ASP.net

      Hello jty202,

      string s = "Foo";
      int pos = s.IndexOf("o");

      Its a function of the string object which is part of the .NET framework,
      not just ASP.NET.

      --
      Matt Berther
      Musings of an agile development manager and occasional code monkey

      [color=blue]
      > I use a the function Instr() in VB6 to search for a search-string in
      > String var. It returns the position of the found search-string.
      >
      > Is there such a funtion in ASP.net?
      >[/color]


      Comment

      • Cor Ligthert

        #4
        Re: Searching text in ASP.net

        jty,

        As far as I know is the Instr in VB6 the same as in VBNet
        (You can use it as well in C# when you set a reference to the
        Microsoft.Visua lBasic namespace)



        I hope this helps?

        Cor


        Comment

        Working...