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

ConvertLettersInString

gh
I have a string of 20 characters. I would like to replace all the
letters, between the 3rd and 17th letters, with a Z. Is there a
function for this?

Thanks
Nov 17 '05 #1
5 1007
No.

Depending upon what you want, you would have to code either:

string modified = original.Substring(0, 2) + "Z" + original.Length > 17
? original.Substring(17) : "";

or

StringBuilder sb = new StringBuilder(original.Substring(0,2));
for (int i = 2; i < Math.Min(17, original.Length); i++)
{
sb.Append("Z");
}
if (original.Length > 17)
{
sb.Append(original.Substring(17));
}
string modified = sb.ToString();

or something like that. :)

Nov 17 '05 #2
gh wrote:
I have a string of 20 characters. I would like to replace all the
letters, between the 3rd and 17th letters, with a Z. Is there a
function for this?

Thanks


Here's how I'd do it:

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace regex {
class Program {
static void Main(string[] args) {

string a = "abcdefghij1234567890"; // 20 chars.

string b = Regex.Replace(
a, @"^(\w{3})(.{14})(\w{3})$", "$1ZZZZZZZZZZZZZZ$3"
);

Console.WriteLine("before: " + a);
Console.WriteLine("after : " + b);

}
}
}
Nov 17 '05 #3
"gh" <gh@at.ne> wrote:
I have a string of 20 characters. I would like to replace
all the letters, between the 3rd and 17th letters, with a Z.
Is there a function for this?


Yes, you need to call the String.ReplaceThirdToSeventeenthWithZed()
method.

P.
Nov 17 '05 #4
SP

"Paul E Collins" <fi******************@CL4.org> wrote in message
news:dj**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
"gh" <gh@at.ne> wrote:
I have a string of 20 characters. I would like to replace
all the letters, between the 3rd and 17th letters, with a Z.
Is there a function for this?


Yes, you need to call the String.ReplaceThirdToSeventeenthWithZed()
method.


In the USA you need to use ReplaceThirdToSeventeenthWithZee() method
otherwise you will get an error 1776.

SP
Nov 17 '05 #5
Actually, if you want to use a StringBuilder, there is an overload of the
StringBuilder.Replace() method that takes an index and a count. Example:

StringBuilder sb = new StringBuilder("AAAAAAAAAAAAAAAAAAAA");
return sb.Replace(sb.ToString().Substring(3, 14),
"ZZZZZZZZZZZZZZ").ToString();
// returns "AAAZZZZZZZZZZZZZZAAA"

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Ambiguity has a certain quality to it.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11********************@g43g2000cwa.googlegrou ps.com...
No.

Depending upon what you want, you would have to code either:

string modified = original.Substring(0, 2) + "Z" + original.Length > 17
? original.Substring(17) : "";

or

StringBuilder sb = new StringBuilder(original.Substring(0,2));
for (int i = 2; i < Math.Min(17, original.Length); i++)
{
sb.Append("Z");
}
if (original.Length > 17)
{
sb.Append(original.Substring(17));
}
string modified = sb.ToString();

or something like that. :)

Nov 17 '05 #6

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

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.