473,385 Members | 1,615 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,385 software developers and data experts.

Replace Nth in String?

I have this value

abcdef

And sometimes I want to replace "b" with "x" and sometimes I want to
replace "f" with "x" and sometimes I want to replace "e" with "x". I
want to replace characters in the string by their position, not their
ASCII content.

What is a common way to do that without using a regex?
Nov 15 '05 #1
4 3878
How about string.Replace():

string letters = "abcdef";

string newLetters = letters.Replace('b', 'x');

Joe
--
http://www.csharp-station.com

"bdwise" <br************@hotmail.com> wrote in message
news:59**************************@posting.google.c om...
I have this value

abcdef

And sometimes I want to replace "b" with "x" and sometimes I want to
replace "f" with "x" and sometimes I want to replace "e" with "x". I
want to replace characters in the string by their position, not their
ASCII content.

What is a common way to do that without using a regex?

Nov 15 '05 #2
"bdwise" <br************@hotmail.com> wrote in message
news:59**************************@posting.google.c om
I have this value

abcdef

And sometimes I want to replace "b" with "x" and sometimes I want to
replace "f" with "x" and sometimes I want to replace "e" with "x". I
want to replace characters in the string by their position, not their
ASCII content.


two ways I know of offhand

string s = "abcdef";
s = s.Substring(0,1) + "x" + s.Substring(2);

or use a System.Text.Stringbuilder object

System.Text.StringBuilder s=new System.Text.StringBuilder("abcde");
s.Remove(1,1);
s.Insert(1,"x");

Nov 15 '05 #3
In addition, you could always use the indexer too.

StringBuilder sb = new StringBuilder("abcdef");
sb[2]='x';
Good luck
Brian W
"Bill Styles" <no*****@nowhere.com> wrote in message
news:ON**************@TK2MSFTNGP11.phx.gbl...
"bdwise" <br************@hotmail.com> wrote in message
news:59**************************@posting.google.c om
I have this value

abcdef

And sometimes I want to replace "b" with "x" and sometimes I want to
replace "f" with "x" and sometimes I want to replace "e" with "x". I
want to replace characters in the string by their position, not their
ASCII content.


two ways I know of offhand

string s = "abcdef";
s = s.Substring(0,1) + "x" + s.Substring(2);

or use a System.Text.Stringbuilder object

System.Text.StringBuilder s=new System.Text.StringBuilder("abcde");
s.Remove(1,1);
s.Insert(1,"x");

Nov 15 '05 #4
"Brian W" <brianw@gold_death_2_spam_rush.com> wrote in message
news:OA**************@TK2MSFTNGP09.phx.gbl
In addition, you could always use the indexer too.

StringBuilder sb = new StringBuilder("abcdef");
sb[2]='x';


d'oh! I can't believe I missed that one especially since it's got to be
more efficient than the other options.
Nov 15 '05 #5

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

Similar topics

4
by: higabe | last post by:
Three questions 1) I have a string function that works perfectly but according to W3C.org web site is syntactically flawed because it contains the characters </ in sequence. So how am I...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
22
by: Phlip | last post by:
C++ers: Here's an open ended STL question. What's the smarmiest most templated way to use <string>, <algorithms> etc. to turn this: " able search baker search charlie " into this: " able...
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,...
9
by: Peter Row | last post by:
Hi, I know this has been asked before, but reading the threads it is still not entirely clear. Deciding which .Replace( ) to use when. Typically if I create a string in a loop I always use a...
4
by: Cor | last post by:
Hi Newsgroup, I have given an answer in this newsgroup about a "Replace". There came an answer on that I did not understand, so I have done some tests. I got the idea that someone said,...
5
by: djc | last post by:
I need to prepare a large text database field to display in an asp.net repeater control. Currently I am replacing all chr(13)'s with a "<br/>" and it works fine. However, now I also want to be able...
10
by: pamelafluente | last post by:
I need to replace all the occurences of a string within another string (or stringbuilder): Function ReplaceInsensitive(ByVal InputString As String, _ ByVal SubstringReplaced As String, _ ByVal...
5
by: V S Rawat | last post by:
I was trying to use back-to-back replace functions to convert a url: str1 = str.replace("%2F","/").replace("%3F","?").replace("%3D","=").replace("%2 6","&"); It didn't replace all 4 types of...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.