How to delete text up to a certain point?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bryn Moorhouse
    New Member
    • Feb 2011
    • 3

    How to delete text up to a certain point?

    Hi there,
    I am a beginner at programming, and wanted to know how to remove text. For example, I am trying to get the username of the logged on user (e.g. Bryn)into a variable. I did this with
    Code:
    variable = My.User.Name
    However this also brought the domain with it (e.g. aaabbbccc\Bryn) .
    Is there anyway of getting rid of the domain? The App will be used on many domains, so removing the first, say, 9 characters won't be any good.
  • kiseitai2
    New Member
    • Jul 2007
    • 93

    #2
    Well, your hint is in the fact that all occurrences of Domain\User share a back-slash. I think there's a Replace function that is native to VB, which should suffice. I've used it a couple of times before. When you type Replace () you have to provide the string, what you want to locate in the string (should be Domain\), and what you want to replace that part with (should be ""). You do need to get the domain separately and set a variable = to Replace() so you capture the output. I hope this helps.

    Comment

    • Bryn Moorhouse
      New Member
      • Feb 2011
      • 3

      #3
      Hi, thanks for the quick reply.
      I have located the replace feature but am still struggling a bit. As I posted in my first post, the domain will change, so I can't use hard coded text. It needs to change. For example, on my home computer the domain is AAA\User but my netbook is BBB\User1.
      Is there a way i use a *\ search or something like that?
      Code:
      username=Replace(My.User.Name,* & "\","")
      is what i got so far, but it didn't work. If I can't wildcard search for anything before the \ I could just remove the backslash.

      Comment

      • kiseitai2
        New Member
        • Jul 2007
        • 93

        #4
        I'm going to look into the User class later today for a quick method, but a manual way of searching would be to create two arrays of characters and add the Name string into one of the arrays and start a loop that will add characters to the other array until (with an if then statement) you find the '\' character. Then I'm not sure if you can use the array directly in the Replace function or will have to run another loop to pass each character in sequence to a string variable. If it does not help, I'll make a quick code snippet so you get an idea, but I do want you to try coding for yourself so you can practice. Also, if you don't know what an array is (I don't know how much you know), ask in the reply.

        Comment

        • Bryn Moorhouse
          New Member
          • Feb 2011
          • 3

          #5
          Hi,
          ok, thank you. Apologies, I am a bit of a noob. I'm at college and am learning quickly, but I am yet to learn arrays and things like that. I'm happy to try anything as I love programming, so I have no problem with trying to code stuff.
          Thanks
          Bryn

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            There should be an InStr() type function that will give you the position of the slash. Then you can use the Mid() function to get everything from the slash onwards.

            Comment

            • Kalen Viljoen
              New Member
              • Feb 2011
              • 12

              #7
              Yeah like rabbit said you can use:

              Variable = Right(My.User.N ame,InStr(My.Us er.Name, "\") - 1)

              Right Function, copys the text from the right of a string up to an integer position.

              InStr Function, finds the position of the backslash in the string.

              Or

              Variable = My.User.Name.Su bstring(My.User .Name.LastIndex Of("\") + 1)

              Which finds the last occurence of a character and returns everything after that.
              Last edited by Kalen Viljoen; Feb 25 '11, 11:43 AM. Reason: Added another option

              Comment

              Working...