473,320 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Multiple string replacements...

Samishii23
246 100+
I know this is simple. Have a small to large string. Replace a 'Key' within that string with what I want replaced... Heres a small example, then the issue I'm having in my head...

Expand|Select|Wrap|Line Numbers
  1. /* One replacement string */
  2. string replace = "Increases your attack power by ~R~ for every 180 armor value you have.";
  3. string[] replace_with = new string[] { "1","2","3","4","5" };
Based on user input, it would be either of the 5. But. Rather then storing 5 full string variables, I'd like to just replace the value in the string.
I know that this would be the best piece of code for the single replacement...
Expand|Select|Wrap|Line Numbers
  1. replace.Replace("~R~", replace_with[user_select]);
Heres my problem:
I will often have the case where I may need more then 1 Replacement in the string. How do I go about this? =\
Dec 3 '09 #1
2 2647
tlhintoq
3,525 Expert 2GB
Take a look at string.format

Expand|Select|Wrap|Line Numbers
  1. int factor = 3;
  2. int Points = 180;
  3. string Device = "Armor";
  4. string replace = string.format("Increases your attack power by {0}, for every {1} {2} value you have.", factor.ToString(), Points.ToString(), Device);
The placeholders {0}, {1}, {2} will be replaced with factor, Points and Device giving you a final string of:

"Increases your attack power by 3 for every 180 Armor value you have."
Dec 3 '09 #2
GaryTexmo
1,501 Expert 1GB
While I agree with tlhintoq's answer, it's worthwhile to point out that sometimes there are... issues... with string.Format. Don't get me wrong, it's my favourite thing in the world at times, but other times it gets you with formatting. In most simple cases (such as your case) it is exactly what you need. Sometimes, when you want to get fancy, you can confuse it (generally when you are trying to build replacement strings to be used by other replacement strings).

The alternative method is to just simply do something like...
Expand|Select|Wrap|Line Numbers
  1. string part1 = "You got ";
  2. string part2 = " points for hitting ";
  3. string part3 = " targets!";
  4. string final = part1 + points.ToString() + part2 + targetsHit.ToString() + part3;
Or something to that effect (ie, manually building the string). Again, tlhintoq's method is much easier to deal with and generally better in 99% of cases, but I wanted to point out the alternative since I have ran into it a few times :)
Dec 3 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
5
by: Roose | last post by:
How can I do a "".replace operation which tells me if anything was actually replaced? Right now I am doing something like: if searchTerm in text: text = text.replace( searchTerm, 'other' ) ...
7
by: Mindy Geac | last post by:
Is it possible to delete special caracters from a string and multiple spaces? When the input is : "a&*bbb cc/c d!d" I want the result : "abbb ccc dd" thnx. MJ <?php
1
by: Markus Ernst | last post by:
Sorry for the multipost - I forgot to crosspost and alt.php gets less attention than comp.lang.php... And I hope this will work with UTF-8. In order to make strings suitable for URLs in a UTF-8...
2
by: JS | last post by:
Is it possible to do multiple replaces in a string? Example: I'm trying to replace all of the spaces with "%20" and "#" with "%23", etc. I also need to replace the "?", "!", " ' ", etc. I know...
4
by: Neo Geshel | last post by:
Greetings I am using VB in my ASP.NET project that uses an admin web site to populate a database that provides content for a front end web site. I am looking for a way to use replace() to...
9
by: Crirus | last post by:
dim pp as string pp="{X=356, Y=256}{X=356, Y=311.2285}{X=311.2285, Y=356}{X=256, Y=356}{X=200.7715, Y=356}{X=156, Y=311.2285}{X=156, Y=256}{X=156, Y=200.7715}{X=200.7715, Y=156}{X=256,...
35
by: jacob navia | last post by:
Hi guys! I like C because is fun. So, I wrote this function for the lcc-win32 standard library: strrepl. I thought that with so many "C heads" around, maybe we could improve it in a...
5
by: int main(void) | last post by:
Hi all, Following is my attempt to write a string search and replace function. #include <stdio.h> #include <stdlib.h> #include <string.h>...
2
by: rengask | last post by:
I got the code to find and replace within an open text file. ------------------ Private Sub cmdFile_Click() Dim strTemp As String txtFile = "" dlg.FileName = "*.*" dlg.ShowOpen ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.