364,112 Members | 2289 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to delete text up to a certain point?

Bryn Moorhouse
P: 3
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
Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 6 '11 #1
Share this Question
Share on Google+
6 Replies


kiseitai2
P: 45
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.
Feb 6 '11 #2

Bryn Moorhouse
P: 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?
Expand|Select|Wrap|Line Numbers
  1. 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.
Feb 6 '11 #3

kiseitai2
P: 45
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.
Feb 6 '11 #4

Bryn Moorhouse
P: 3
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
Feb 6 '11 #5

Rabbit
Expert Mod 5K+
P: 6,668
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.
Feb 6 '11 #6

Kalen Viljoen
P: 12
Yeah like rabbit said you can use:

Variable = Right(My.User.Name,InStr(My.User.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.Substring(My.User.Name.LastIndexOf("\ ") + 1)

Which finds the last occurence of a character and returns everything after that.
Feb 25 '11 #7

Post your reply

Help answer this question



Didn't find the answer to your Visual Basic .NET question?

You can also browse similar questions: Visual Basic .NET