1

I am Searching though the contents of a Excel Spreadsheet template at replacing all the variables represented by @Something.

I have all the variables names come up with code, i'm just looking for a way to replace the them in a string.

Example:

My name is @Name

I want it to become:

My name is John.


My Code explained:

I have an array holding a struct which holds:

TagPointer: this is my variable @name, TagValue: this would be the name John

Here i can get the index of where my variable is and i have the original String its Comp. I am just unsure how i can replace the @Something.

int testnum;
if (Comp != "null")
{
    if (Comp.IndexOf(ArrayNode[i].TagPointer) != -1)
    {
        testnum = Comp.IndexOf(ArrayNode[i].TagPointer);                          
    }
}
2
  • 4
    There's a replace function you can use to replace text. Is that what you are looking for? Commented Aug 6, 2012 at 8:10
  • Lol Voted every one up, and i got -2, haha No worry's thanks for letting me know about the .Replace function, its what i was looking for :) +1 to all Commented Aug 6, 2012 at 8:24

7 Answers 7

12

use string.Format(), string.Replace() there is less convenient

string value = "John";
string result = string.Format("My name is {0}", value);
Sign up to request clarification or add additional context in comments.

Comments

5

Unless I'm missing something, can't you use the string.replace function?

You could do something like:

foreach (var tag in ArrayNode)
{
  Comp = Comp.Replace(tag.TagPointer, tag.TagValue);
}

1 Comment

Not looking to change any values in my Array, But Comp.Replace(tag.TagPointer, tag.TagValue); seems to work fine :), Thank you for your answer :)
5

Have you ever seen the FormatWith extension?

With it you can write something like this:

Status.Text = "{UserName} last logged in at {LastLoginDate}".FormatWith(user);

Comments

4

Why not using ie

string name = "Giusepe";
string _tmp = "My name is @Name";
_tmp = _tmp.Replace("@Name", name);

Comments

2

You could iterate over your array and use the .Contains() to see which tag you have present and use the .Replace() to replace the tag with the actual value.

7 Comments

Wouldn't it be unneccesary to do Contains? There is no error message or exception when Replace doesn't find the tag.
Thanks i have looked into the .contains() already, i just did not know about .Replace, Thank you.
@Default: I think that if one uses the Contains method, one would be able to call break to avoid doing unnecessary loops.
Speed is important to me, as i'm searching though lots of Excel Cells.
@Pommy: Then I would recommend you modify the selected answer's response and make sure that if the string contains what you are after, you then call break.
|
1

Do you want this:

string a = "@Something";
a = a.Replace("@somethig", Value);

?

Comments

0

Years later, we now have string interpolation to help with this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.