Hi,

I am writing a program that needs to search particular strings in a text file. The strings are typed in a text box by the user and then when the text file is opened, using fileDialog, the program searches that file using the string in the text box. I have done this currently with the String->Contains function.

Here is the code I used currently

if(Form1::wordsearch->Text != "") {
String ^wrdsrch;
wrdsrch = Form1::wordsearch->Text;if (line->Contains(wrdsrch)) { ...

the last line in the if statement is the one that does the search. I want to make sure that the string is exactly and only exactly the same string in the document as the user put it. How do I write a piece of code that will match each letter for me and then if the letters don't match, go to the next word in the line and so on until an exact match has been found in that line of text. Then going on to the next line. I only need to know if the exact word is in a line of the document and then going on to the next line of that document.

This is in Visual C++ .net the express version from Microsoft.

Can anybody help me with this??

Thanks in advance,
Tony

Recommended Answers

All 4 Replies

Don't know if it helps, but I just wrote something very very simple in C# 2005 express, you may want to consider switching over to it of you want. If not maybe someone else can help you out in C++.

StreamReader sr = new StreamReader(@"File.txt", true);
 
String curString = "";
String totalString = "";
while (!sr.EndOfStream)

{[INDENT]curString = sr.ReadLine();[/INDENT]

[INDENT]if (curString.Contains(";"))[/INDENT]

[INDENT]{[INDENT]curString = curString.Replace(";", "Comment - ");[/INDENT]
}
if(curString.EndsWith("\\"))

{[INDENT]curString = curString.Replace("\\", "\n"); [/INDENT]
}
 
totalString += curString; 

[/INDENT]}
Member Avatar for iamthwee

> How do I write a piece of code that will match each letter for me and then if the letters don't match, go to the next word in the line and so on until an exact match has been found in that line of text.

And what have you tried?

> How do I write a piece of code that will match each letter for me and then if the letters don't match, go to the next word in the line and so on until an exact match has been found in that line of text.

And what have you tried?

I have only used the 'contains' function from the String class. I know that i can be done using a for loop and going through each charachter of the string at a time, i just don't know how to write it. That's what I need help with?

you can use the compare method

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.