Question about C# Strings, populating variable values within a string at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rezme
    New Member
    • Sep 2008
    • 2

    Question about C# Strings, populating variable values within a string at runtime

    Allow me to preface this with a disclaimer: I'm self taught in C#, basically I type code in from what I've found on the web, and try to get it to do what I want it to. I'm sure there's a lot of us out there, but I really know very little of the terminology. If I'm in error on something, please feel free to correct me.

    That being said, I've written an app that generates emails for my support job. Some of these emails are applicable to multiple applications.

    The email text is saved in txt files on the drive, and at runtime are selected from a list to be added to an email, depending on the support issue, and the application. What I'd like to do is have some of these be multi purpose emails, and have some sort of text in the email that represents a variable value to be specified at runtime. I've seen applications that allow you to specify variables in saved option text information that populates with information based on the computer name etc. (%computername% ) or something of that sort goes in the text, and at runtime it's replaced with "MYCOMPUTERNAME ".

    How would I go about adding this information to a string variable that has been populated from one of these text files, and what sort of escape character can be used in the text file to accomplish this. I'm sure there's some sort of slick term for this sort of activity, but not knowing the term I've been driven to make my first post on a forum such as this :)

    Thanks to whoever responds.
  • pootle
    New Member
    • Apr 2008
    • 68

    #2
    hi,

    First things first. Try not to use a string for complex processing. A string is immutable, which means every change results in a new string and this is very slow. Instead, look at using StringBuilder. The kind of substitution you want to do can be done in several ways. You can parse and replace using the methods of StringBuilder, but if i had this task, i would use a regular expression to make a substitution of a predefined string. You can find a lot of information about this on the net, try msdn looking at the Regex class.

    Hth

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Is this for C++ or C#?
      If C# move it to .NET Forum

      raghu

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        I am moving this to the .NET forum.

        Comment

        • DonBytes
          New Member
          • Aug 2008
          • 25

          #5
          If it's not to long strings you could write them prepared for String.Format when you load them up such as:

          Code:
          String sBody = "ComputerName: {0}\r\nError: {1}";
          and then use them as:
          Code:
          email.Body = String.Format(sBody, sComputerName, sError);

          Comment

          • rezme
            New Member
            • Sep 2008
            • 2

            #6
            I think stringbuilder and regex may be the way to go, but regex is pretty daunting. Is there a clear, concise tutorial out there that breaks down how regex works?

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Well, if I understand this question correctly, your best bet would be to use XML or some other method of storing data rather than flat text files. Separate the text from the other fields, that way you don't have to do any real parsing.

              Also, if you are wanting environmental variables like %computername% look into Environment.Get EnvironmentalVa riable().

              Hope that helps, and I didn't misunderstand your question.

              Comment

              Working...