Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete last part of String 3

Status
Not open for further replies.

Svanholm

Programmer
Jul 11, 2007
31
0
0
DK
Been searching forums, in vain so far, for solution to this simple problem:

I want to delete last part of a String e.g. the word "Delete" so it only prints "Del".

However, since the String is a user input I do not know the lenght of it so I cannot use: Left(Str, Len).


A simple coding would hopefully be:

Code:
 Dim Word, Result as String 

Word = Text1.Text

'Here goes the code that deletes, say, last 4 letters of Word. 

Text2.Text = Result

Suggestions?
 
Thx for your reply strongm

I did, in fact, investigate the Len function, but in vain.

However pondering deeper, after your post, I suddenly Heurekaed it:

Code:
 Dim Word, Result as String 

Word = Text1.Text

Result= Len(Word) - 4 

Text2.Text = Left(Word, Result)

I love logic when it is logical :)
 
Be aware that

Dim Word, Result as String

isn't doing what you think it is. In VB every variable has to be dimensioned explicitly, or it defaults to a Variant. In other words in your declaration Word is actually a Variant rather than the String that I suspect you intended it to be.
 
Wow!

That was new to me.

You are absolutely right, I wanted both variables to be strings.

So I guess it should write:

Dim Word As String, Result As String

or maybe even:

Dim Word As String
Dim Result As String

My program and code is probably too small to register any significant slow speed, but I guess it is quite important with larger programs.

Can you believe, I am chewing my way through two VB textbooks, and a bunch of exercises, and it never dawned to me?

Thank you, master, for this appendix exercise :)
 
Your code can also be condensed as follows:
Code:
Text2.Text = Left(Text1.Text, Len(Text1.Text) - 4)
 
While this code

Dim Word, Result as String

Word = Text1.Text

Result= Len(Word) - 4

Text2.Text = Left(Word, Result)

would work in Visual Basic (because of "weak" typing),
Result really should be an Integer type to be used correctly in string slicing functions.

but if you just want the first 3 characters of a string use

Mid(StringIn,Start,Length)

so for your example:

Word = Mid(Word,1,3) will return with Word as "Del"



Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
>Left

Multiple posts here have used this, but 'Left' returns a variant, and not a string. I'd advise using 'Left$'

>Result really should be an Integer

Why an Integer?
 
Noted, thanks.

However, I specifically want the end of a word to be deleted. I do not care about the beginning, cause I do not know which word the user chooses, as wrote previously in this thread.

To be absolutely concrete, I wish to remove file-extensions, e.g. "Test.txt", so I can use the name of the file "Test" in the Form.Caption.

Thus when user saves a file via Save As with CommonDialog1, I simply copy the CommonDialog1.filename, remove the extension and paste it into the current Form.Caption.

Maybe there is a shortcut, but this works for a beginner like me, who does not code at "improbability drive", yet.

 
You are aware, I presume, that nowadays file extensions can be more than three characters long?
 
Thanks strongm

I am aware of that, yes.

In the current situation, user is not allowed to decide the extension, it is automatically added in the coding.

Unless of course, user inputs "Test.txt", but then the actuael filename would be: "Test.txt.txt". Quite rare, I think. I mean, when I save a document og file, I never write the extension myself.

What I wanted, was just a cosmetic trim of the name in the Form.Caption so it did not contain the extension.

I may even choose to use no extension at all for the files that user saves. He is not going to use them for anything outside the program.

So far, on coding stage, I use .txt. It speeds my checking that the right lines are saved.

 
Stripping off file extensions regardless of length

filename = Left(filename, InStr(1, filename, ".") - 1)
or
filename = Left(filename, InStrRev(filename, ".") - 1)

the first will handle filenames with double extensions and remove everything after the "." the second version will handles filenames that could contain a "." and remove only the part after the last "."

Why an Integer?
Ok should be a Long type, all things being equal. [smile]


Chris.

Indifference will be the downfall of mankind, but who cares?
Woo Hoo! the cobblers kids get new shoes.
People Counting Systems

So long, and thanks for all the fish.
 
Very Cool ChrisHirst

Will check them out, and, hopefully, use them.

thanks a bunch

:)
 
<Stripping off file extensions regardless of length

You'd have to loop around your code to deal with multiple extensions in the filename.
 
Loop? - I think Chris's second variant (using InstrRev)handles that issue ...

 
Sorry, of course it does. That's what you get for posting messages when you're sleepy. Seems like a profound insight at the time... thanks strongm.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top