Replacing marked values in a text file

  • Thread starter Thread starter Razzie
  • Start date Start date
R

Razzie

Hey all,

Let's say I have a textfile with 'marked' values in it, for example,
something like this:

Name......... *name*
Street......... *street*
Email.......... *email*

And say I have 1 or more objects with properties equal to the name of those
marked values, for example:

Person.name, Person.street, Person.email

well you get the idea :)
Now, I want to replace the * * values in the textfile with the properties of
the object. Of course this ain't a problem, however, I do NOT want the
following situation:

//Search the file for a * * string
switch(strToReplace)
{
case "*name*":
// replace strToReplace with Person.name
break;
case "*street*":
// replace strToReplace with Person.street
break;
// etc
}

this code just looks plain ugly to me and besides, if I get like 100+ * *
values in my (more than 1) textfiles, which is quite possible, this is a lot
of work too. I'm hoping for a very easy and clean way instead... is there a
way to call the corresponding property automatically when the names are
equal? Is there another way to do this? I've been looking for a solution
using design patterns but I couldn't find a good one, so I hope someone here
knows a good solution.

THanks a lot in advance,

Razzie
 
Use a regular expression to perform the search. Once regex matches take the
string fragment u need and then add the prefix u want. Using the regex
avoids u creating the switch statement

Regards,
 
Razzie said:
Hey all,

Let's say I have a textfile with 'marked' values in it, for example,
something like this:

Name......... *name*
Street......... *street*
Email.......... *email*

And say I have 1 or more objects with properties equal to the name of those
marked values, for example:

Person.name, Person.street, Person.email

well you get the idea :)
Now, I want to replace the * * values in the textfile with the properties of
the object. Of course this ain't a problem, however, I do NOT want the
following situation:

//Search the file for a * * string
switch(strToReplace)
{
case "*name*":
// replace strToReplace with Person.name
break;
case "*street*":
// replace strToReplace with Person.street
break;
// etc
}

this code just looks plain ugly to me and besides, if I get like 100+ * *
values in my (more than 1) textfiles, which is quite possible, this is a lot
of work too. I'm hoping for a very easy and clean way instead... is there a
way to call the corresponding property automatically when the names are
equal? Is there another way to do this? I've been looking for a solution
using design patterns but I couldn't find a good one, so I hope someone here
knows a good solution.

This is a solution tailor made for reflection.

You can use regular expressions or custom search code to find the markers.

Then one you get the property name from the marker, use reflection to
find the value of that property and replace the marker with that value.
 
Back
Top