473,401 Members | 2,127 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,401 software developers and data experts.

replace multiple strings at once

Hi All,

To replace some substrings in a string value, i use Regex.Replace method
mulptiple times like;

strmemo = "new Data for user; Name:@@Name , Surname:@@Surname,
Address:@@Address";
strmemo = Regex.Replace(strmemo,"@@Name", value1.ToString());
strmemo = Regex.Replace(strmemo,"@@Surname", value2.ToString());
strmemo = Regex.Replace(strmemo,"@@Address", value3.ToString());

are there any solutions to combine all these statements into just one.

Thanks....

Jun 27 '08 #1
3 10006
Ned White wrote:
To replace some substrings in a string value, i use Regex.Replace method
mulptiple times like;

strmemo = "new Data for user; Name:@@Name , Surname:@@Surname,
Address:@@Address";
strmemo = Regex.Replace(strmemo,"@@Name", value1.ToString());
strmemo = Regex.Replace(strmemo,"@@Surname", value2.ToString());
strmemo = Regex.Replace(strmemo,"@@Address", value3.ToString());

are there any solutions to combine all these statements into just one.
Not really.

You can:

strmemo = strmemo.Replace("@@Name",
value1.ToString())Replace("@@Surname",
value2.ToString()).Replace("@@Address", value3.ToString());

but it is still 3 replaces.

But why not:

strmemo = String.Format("new Data for user; Name:{0}, Surname:{1},
Address:{2}", value1.ToString(), value2.ToString(), value3.ToString());

it looks much better.

Arne

Jun 27 '08 #2
How about using a custom evaluator? Now all you need to do is add things
to the dictionary and they should work... For the record, you might also
want to handle escaping (i.e. how do yuo write @@Foo as a literal?), and
termintated tokens (i.e. you do you write a literal immediately after a
token - i.e. "@@Size" + "mm" (if you see what I mean) - @Size@mm would
be easier... (i.e. @Foo@ is a token for the argument Foo)

Marc

using System.Collections.Generic;
using System.Text.RegularExpressions;
static class Program
{
static readonly Regex regex = new Regex("(@@)([a-zA-z]+)",
RegexOptions.Compiled);
static void Main()
{
Dictionary<string, stringargs = new Dictionary<string, string>();
args.Add("Surname", "Smith");
args.Add("Name", "John");
args.Add("Address", "Somewhere");

string input = "new Data for user; Missing:@@Missing,
Name:@@Name , Surname:@@Surname, Address:@@Address";

string output = regex.Replace(input, delegate(Match match)
{
string value;
args.TryGetValue(match.Groups[2].Value, out value);
return value;
});
}
}
Jun 27 '08 #3
typo; should be a-zA-Z
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
4
by: spam | last post by:
Is there a well-known algorithm for replacing many substrings in a string? For example, I'd like to take the string "abc def ghi jkl mno pqr" and replace, say, every instance of "abc", "ghi", and...
22
by: Michael Nahas | last post by:
Antti & all interested, The draft description of my language to replace C is available at: http://nahas.is-a-geek.com/~mike/MyC.pdf I am a long time C programmer (I read the old testament...
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
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...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
3
by: teo | last post by:
I have a text. Inside the text the "hallo" word occurs five time. I need to replace "hallo" with "hallo world". Unfortunately I get this: hallo world world world world world
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 ...
5
by: buu | last post by:
I have an function that replaces some string from a huge text that I run very often... So, I wanted to speed it up... I was using String and StringBuilder. But, I was wandering should same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.