---
title: "How do I locate a particular word in a text file"  
description: "How do I locate a particular word in a text file"  
author: "marcel ethan"  
published: 2014-08-27  
updated: 2014-08-27  
canonical: https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file  
category: "asp.net"  
tags: ["asp.net", ".net", "c#", "text file", "particular word in a text file", "locate a particular word", "locate a particular word in a text file", "find the particular word in text file"]  
reading_time: 2 minutes  

---

# How do I locate a particular word in a text file

I am sending mails (in [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) ,c#), having a [template](https://www.mindstick.com/blog/282/creating-custom-template-in-joomla) in [text file](https://www.mindstick.com/forum/12828/converting-a-text-file-to-an-array-in-java) (.txt) like below

[User Name](https://www.mindstick.com/interview/33836/how-to-check-current-user-name-of-database-in-sql-server) :<User Name>

Address : <Address>.

I used to [replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) the words within the angle brackets in the text file using the below code

[StreamReader](https://www.mindstick.com/forum/161576/explain-the-difference-between-streamreader-and-file-readalllines) sr;

sr = File.OpenText(HttpContext.Current.Server.MapPath(txt));

copy = sr.ReadToEnd();

sr.[Close](https://yourviews.mindstick.com/story/1687/rubbing-hands-close-up-benefits)(); //close the reader

copy = copy.Replace(word.ToUpper(),"#" + word.ToUpper()); //[remove](https://yourviews.mindstick.com/story/4554/8-harmful-weeds-to-remove-from-garden) the word specified UC

//save new copy into existing text file

FileInfo newText = new FileInfo(HttpContext.Current.Server.MapPath(txt));

[StreamWriter](https://www.mindstick.com/interview/34097/how-can-you-write-to-a-file-using-streamwriter-with-async-await) newCopy = newText.CreateText();

newCopy.WriteLine(copy);

newCopy.Write(newCopy.NewLine);

newCopy.Close();

Now I have a new [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic),

the user will be adding new words within an angle, say for eg, they will be adding <Salary>.

In that case i have to read out and find the word <Salary>.

In other words, I have to find all the words, that are located with the angle brackets (<>).

How do I do that?

## Replies

### Reply by Sumit Kesarwani

Hi,

You should place some of your objects into using blocks. Something like this:

```
using(StreamReader sr =File.OpenText(HttpContext.Current.Server.MapPath(txt))){    copy =sr.ReadToEnd();} // reader is closed by the end of the using block//remove the word specified UC copy = copy.Replace(word.ToUpper(), "#" +word.ToUpper());    //save new copy into existing text file FileInfo newText = new
FileInfo(HttpContext.Current.Server.MapPath(txt));using(var newCopy = newText.CreateText()){   
newCopy.WriteLine(copy);   
newCopy.Write(newCopy.NewLine);}
```

The using block ensures that resources are cleaned up even if an exception is thrown.


---

Original Source: https://www.mindstick.com/forum/2174/how-do-i-locate-a-particular-word-in-a-text-file

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
