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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with finding a specific text inside a string

Status
Not open for further replies.

jpack23

Programmer
Dec 15, 2004
82
0
0
US
I have a string and want to find a specific text inside that string and shade it using vb 2008

the string could be of varying lengths.

code example:

strComplete = "I have an apple, banana, and an orange"

'I want to shade "apple, banana"

strTextToHightlight = "apple,banana"

code here to find apple, banana and shade it

'I want strComplete to read exactly the same only "apple, banana" will be shaded. ( i will settle for bolded)

Thank you very much for your help

joe
 

Is this a school assignment?

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
What are the defined set of words you want shaded?
 
@jebenson. Is that a serious question or are you trying to be funny? Not school assignment, thanks for asking, just trying to learn


@pellet - not going to be a predefined set of words. Each time the app is run its probably going to be a different set of words in a different spot in the entire text.

What I am trying to do is create two variable. One variable holds the enitre string (strComplete). the other variable hold just the piece of text I need to shade(strtexttohighlight). I use this second variable to compare.

I know you can use microsoft string functions to find and replace...but I can figure out how to find just the peice of text(strtexttohighlight) and replace is with the same piece only shaded. User would like this text to stand out from the rest, so bold and italics would work too but they perfer it to be shaded.

Maybe I am describing it all wrong. using the example in my original post, if I had a paragrapy 4 sentences long that contained the word "apple" I want to search that entire paragraph, fine the word "apple" and shade it, so when the paragraph is printed that word is shaded. The word may not always be "apple" is could be "banana" or it could be "apples, banana, and orange" (meaning is could have dashes or comma or periods in it).

thanks again for the help
 
No, I'm not trying to be funny at all. The rules of this forum state that we do not provide answers for school work questions. I asked because your question looked suspiciously like many, many school work questions I have seen before.

As for your question, look into the RichTextBox control and its Find method. Here's a link that might help you get started:




I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jpack23,

jebenson was NOT kidding.
[blue]
Promoting, selling, recruiting and student posting
are not allowed in the forums.
Posting Policies

[/blue]
is posted below, as you will notice.


A STRING does not have properties.

What OBJECT are you manipulating?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
@jebenson - sorry, my bad, didnt read all the rules. Thanks for the link.

@skip - suppose my problem starts with trying to manipulate a string.

I get the string from at dataset

strComplete = dtsCompleteText.tables(0).Rows(0).Item("fldCompleteText).to string

can I manipulate that dataset item?

Thanks again and my apologize for assuming jebenson was poking fun at me



 


the InStr() function.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
@skip

Ok so instr() gives me the start and I can get the lenght of the text to shade or bold.

whats next. Can I rebuild the string and when it gets to the start position of the text to be shaded, shade it for lets say 10 characters and then return finish?
 
Do i need to do the shading or bolding after I build the word document

I took the entire string and inserted it into a bookmark

TextIntoBookMarkInsert(InsertText:=strInsertText, wordDoc:=objwordDoc, _
wordStyle:="", bookmarkName:=bkmrkBodyText)


Friend Shared Sub TextIntoBookMarkInsert(ByVal InsertText As String, ByRef wordDoc As Document, _
ByVal wordStyle As String, ByVal bookmarkName As String)




If wordDoc.Bookmarks.Exists(bookmarkName) = True Then
objBookmark = wordDoc.Bookmarks(bookmarkName)
Else
Exit Sub
End If

objBookmark.Range.Select()
With wordDoc.Application.Selection
.Text = InsertText

If Len(Trim$(wordStyle)) > 0 Then
.Style = wordDoc.Styles(wordStyle)
End If
End With


now can I search though the text for certain words and shade or bold them?
 



AFTER you place your text in the Word document, use the Characters object to reference portions of a range to format.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top