How to truncate text string?

gkiril

Newcomer
Joined
Feb 15, 2004
Messages
21
All,

I have a textrange object. I would like to add the textrange.text to a textbox, but I would like to limit the textrange to 100 characters. If the text is over a hundred, I would like to include the last word closest to 100 and add "...". I got as far as:

If TxtRange.text.length > 100 Then

End if

I've just started researching this, but I posted here in case someone could provide a quick answer. Thanks.

-gk
 
Okay, I may have the truncation problem solved. How about this...

I have a textrange object, created by the user. It looks like this:

meone.....goodb

The first word should be someone and the last word should be goodbye with however many words in between. How can I fix this textrange to include the first and last words in their entirety? This is very general and should not be dependant on my code. However, I will let you know that the textrange object was declared as

Dim TxtRange2 As mshtml.IHTMLTxtRange = Iselect2.createRange().

Thanks.

-gk
 
I might be totally off the mark, but....

Couldn't you possibly do a string search from the left and capture everything infront of the "space" and do the same from the right.

Visual Basic:
Dim sr As String = "This is the big brown dog"
Dim fposition As Integer
Dim lposition As Integer
Dim fWord As String ' The first word
Dim lWord As String ' The last word

' Find the position of the first space
        fposition = InStr(sr, " ")
' Minus 1 from the number so that you just capture the first word not the space
        fposition = fposition - 1

' Capture the first word   
        fWord = Microsoft.VisualBasic.Left(sr, fposition)

' Find the position of the last space
        lposition = InStrRev(sr, " ")
' To find the number of characters from the right minus the value of lposition from total length
        Dim FromEnd As Integer
        FromEnd = sr.Length - lposition

' Capture the last word
        lWord = Microsoft.VisualBasic.Right(sr, FromEnd)

' Display the First word .... Last word
        MessageBox.Show(fWord & " ... " & lWord)

To find this information, I just did a google search on "Search String Character Visual Studio".

Hope this helps
 
I don't do VB anymore so I'm completely useless on what tools you have available, and I don't quite understand this TextRange you're using, but can you use a String.Split(' ') to get your truncated text into an array of strings for you to grab the first and last of?
 
Hello all, sorry for not responding... I've been away and another bit of my code had my attention.

The txtrange object is not the name of a text area or box, it is an Ihtmltxtrange object. I have the truncation problem solved, using .movetoend and doing some basic algebra to calculate x and y type values. This works pretty well.

I need to figure out a way to capture the first and last words of the selection, so I will be trying the solution given above in a day or two, but before that, I have discovered an interesting problem.

If I set a txtrange and retrieve the .htmltext, the retrieval process automatically appends the relevant closing tag, even if your selection did not include the closing tag to begin with. Very frustrating given that I would like to replace only the .htmltext selected. See, you cannot use a find and replace function like with the "Scripting.FileSystemObject" because the source string NEVER matches the text for which you are searching. Example... say you select the text "The quick brown" out of the sentence "The quick brown fox." The underlying HTML may look like this <tag>The quick brown fox.</tag>. Therefore, your selection (if you use txtrange.htmltext) should appear as <tag>The quick brown, but oh no, it actually appears as <tag>The quick brown</tag>. This is because the .htmltext property follows some kind of compliance rules that demans the text always be captured as valid HTML. Now, you can see why the selected text string never matches if you search for that string in the source document.

Okay, so I will use .text instead. After all, if you append tags in HTML, as long as they are complete and correct, you can insert them just about anywhere without negatively altering the document. Well, here's something interesting. Sometimes, the .text string is not recognized. Imagine it like this... you highlight some text on a webpage, again "The quick brown fox" for example, then you open that webpage in notepad and do a search for that very text. Only you get a message that the text cannot be found. So then, you manually find the text in the document. It looks the same so you even paste the original text you copied into the document on a line right above or below the source text. Again, they look identicle, character for character, but the find dialog in notepad doesn't recognize the text you copied from the page. That is very frustrating and seems to make using something like streamwriter very unreliable.

Therefore, if someone can recommend a way to search for a string in a text document and replace that string that they know is 100% effective, I would be grateful to hear of it.

I will post the results of my test of the code above when I have tested it.

Thanks all for looking.

-gk
 
Last edited:
Okay, in the short time since my last post, I have been playing with my code. I implemented txtrange.text.trim and that seems to have cleared up the problems with searching and finding the string in my text file. I would prefer to use txtrange.htmltext, but I know no way of preventing that closing tag from being appended to the selection. Damn it!!!

Anyway, the ONLY problem with txtrange.text is that if your selection breaks an element, for instance spanning "the quick <i>brown", it will not be recognized because, after all, it is only reading the text and not the underlying html.

Consequently, I am now turning my attention to the code above to see about truncating the text to the first element encounted.

As always, any words of advice are appreciated.

-gk
 
Back
Top