Results 1 to 3 of 3

Thread: [2008] Adding text to the start of each line

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2008
    Posts
    1

    [2008] Adding text to the start of each line

    Hello all,

    I am a computer technician for a local high school. Part of my job is to add new sites to the website filter, as teachers send the links to me. The way our filter works, instead of blocking google.com, I have to type in *\.google\.com*\.*

    That does get annoying after a while, as it's quite repetitive. I'm writing a small program to help me with that. There are two multiline text boxes and a button. I enter the URLs into the top text box (one URL per line), and than click the button, and ideally it will automatically add the *\. to the front, the \ in front of .com, and *\.* to the end of each line, and than display the outcome in the lower text box.

    I figured out how to add the \ in front of .com:

    DotComFiltered = StrReplace (OriginalList, ".", "\.")

    However, I can't figure out how to make visual basic add the *\. in front of each line, and the *\.* at the end of each line. Is that possible?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Adding text to the start of each line

    vb.net Code:
    1. Dim lines As String() = myTextBox.Lines
    2.  
    3. For index As Integer = 0 To lines.GetUpperBound(0)
    4.     lines(index) = String.Format("*\.{0}*\.*", _
    5.                                  lines(index).Replace(".com", _
    6.                                                       "\.com"))
    7. Next
    8.  
    9. myTextBox.Lines = lines
    Just be aware that that Replace will also affect words that begin with "com". You'd probably be better off using Regex.Replace but I'll leave that to you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member su ki's Avatar
    Join Date
    Oct 2007
    Posts
    354

    Re: [2008] Adding text to the start of each line

    hi Joey H
    jmcilhinney's solution is pretty good alternatively u may use following code for getting desired output

    do not forget to use following at top of ur code window
    vb Code:
    1. Imports System.Text.RegularExpressions

    call it using following syntax
    vb Code:
    1. txtFilter.Text=MakeFilter(txtFilter.Text)

    here the MakeFilter function which does desired for you

    vb Code:
    1. Public Function MakeFilter(ByVal sInput As String) As String
    2.        
    3.         ' here i assume that u r using filter rule in firewall for preventing url to navigate so .(dot)
    4.         ' must be escaped for it that's why i use following line
    5.         sInput = Regex.Replace(sInput, "\.", "\.", RegexOptions.Multiline )
    6.  
    7.        ' you may use following line if only .com is to be replaced use ignorecase option for avoiding lower upper case issue
    8.        ' uncomment it if required and comment the above one
    9.        ' sInput = Regex.Replace(sInput, "\.com", "\.com", RegexOptions.Multiline Or RegexOptions.IgnoreCase)
    10.        
    11.        ' Starting of line
    12.         sInput = Regex.Replace(sInput, "^", "*\.", RegexOptions.Multiline)
    13.  
    14.        ' End of line
    15.         sInput = Regex.Replace(sInput, "$", "*\.*" + vbNewLine, RegexOptions.Multiline)
    16.         Return sInput
    17.     End Function
    Last edited by su ki; Dec 24th, 2008 at 06:30 AM.

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