| | |
Replace String what is the best way
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
Hi to all, I'm working in a application that needs to replace a certain string from a file. I need to create like a dictionary, I'll explain my self.
1. I need to search the string 'class.type='
2. The string can be in the file like:
class.type=var; or class.type =var; or
class.type= var; or class.type = var; or
A.class.type=var; or B.class.type=var; or
You can see than can be with/without spaces, also the string 'var' can be any string(I put var for an example, also sometimes I have a string before the string I'm looking in the example I put A/B.
3. I need to find the index of the 'class.type=' and find the index until certain chars, I'll explain, for example in the following cases, I need to find until '{};'
{A.class.type=var;
}A.class.type=var;
;A.class.type=var;
4. Then I need to find the index of ';' from the string 'class.type='
5. In the end I will have two index.
The result:
Example 1:
Input:
if(a==1)A.class.type=var;
Output:
if(a==1){}
Example 2:
Input:
if(a==1)class.type=var;
Output:
if(a==1){}
I already did this but I think not in a elegant and efficient code, I read that there are regular expressions that can help me, but I never work with them.
I know is a lot of work, but I'll appreciate any suggestions to improve and write a efficient code.
Thanks in advanced
1. I need to search the string 'class.type='
2. The string can be in the file like:
class.type=var; or class.type =var; or
class.type= var; or class.type = var; or
A.class.type=var; or B.class.type=var; or
You can see than can be with/without spaces, also the string 'var' can be any string(I put var for an example, also sometimes I have a string before the string I'm looking in the example I put A/B.
3. I need to find the index of the 'class.type=' and find the index until certain chars, I'll explain, for example in the following cases, I need to find until '{};'
{A.class.type=var;
}A.class.type=var;
;A.class.type=var;
4. Then I need to find the index of ';' from the string 'class.type='
5. In the end I will have two index.
The result:
Example 1:
Input:
if(a==1)A.class.type=var;
Output:
if(a==1){}
Example 2:
Input:
if(a==1)class.type=var;
Output:
if(a==1){}
I already did this but I think not in a elegant and efficient code, I read that there are regular expressions that can help me, but I never work with them.
I know is a lot of work, but I'll appreciate any suggestions to improve and write a efficient code.
Thanks in advanced
Regular expressions are the way to go. Using them in C# is easy, but developing the actual regular expression is slightly complicated. There are two tools that I've used in the past to help me craft regular expressions, Regulazy and Regulator, both are made by Roy Osherove, and you can download them here: http://weblogs.asp.net/rosherove/pag...-osherove.aspx.
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
![]() |
Similar Threads
- Find and replace string function (C++)
- Find and replace the string (VB.NET)
- How to read a file char by char and replace it with string???? (C++)
- find and replace string in a text file (C)
- string replace (C++)
- replace string (PHP)
- Replace String (JSP)
Other Threads in the C# Forum
- Previous Thread: WebKit
- Next Thread: Network queuing system
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast c# c#gridviewcolumn cast check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox localization login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml





