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

String conversion

Hi,

Is there any in-built converter which will convert a string like

markrae

into

markrae

Child's play to write a function to do it, but I just wondered if it already
exists within the .NET Framework.

Any assistance gratefully received.

Mark Rae
Nov 16 '05 #1
6 1371
Hi,

No AFAIk, but as you said before it's VERY easy to program this.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

Is there any in-built converter which will convert a string like

markrae

into

markrae

Child's play to write a function to do it, but I just wondered if it already exists within the .NET Framework.

Any assistance gratefully received.

Mark Rae

Nov 16 '05 #2
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uA**************@TK2MSFTNGP10.phx.gbl...
No AFAIk, but as you said before it's VERY easy to program this.


This is how I did it...

public string encodeEvent(string pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Length; intPos++)
{
strReturn += "&#" +
((int)Encoding.ASCII.GetBytes(pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}
Nov 16 '05 #3
On Wed, 28 Jul 2004 14:38:26 +0100, "Mark Rae"
<ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:uA**************@TK2MSFTNGP10.phx.gbl...
No AFAIk, but as you said before it's VERY easy to program this.


This is how I did it...

public string encodeEvent(string pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Length; intPos++)
{
strReturn += "&#" +
((int)Encoding.ASCII.GetBytes(pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}

A more efficient method:
string ToOrdinals(string str)
{
StringBuilder sb = new StringBuilder();

foreach (char c in str)
{
sb.Append("&#");
sb.Append((int)c);
sb.Append(";");
}
return sb.ToString();
}
Nov 16 '05 #4
"Austin Ehlers" <li*******************@ku.edu> wrote in message
news:hk********************************@4ax.com...
A more efficient method:


In what way is it more efficient...?
Nov 16 '05 #5
Mark Rae <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote:
"Austin Ehlers" <li*******************@ku.edu> wrote in message
news:hk********************************@4ax.com...
A more efficient method:


In what way is it more efficient...?


In just about every possible way, to be honest.

It doesn't create extra intermediate strings for no good reason. It
also doesn't use Encoding.ASCII for no particularly good reason. (Both
will give bad results for non-ASCII data.) I've only just noticed
you're encoding the whole string for *every* iteration!

Here's a little benchmark encoding a string of 10,000 characters 100
times with each method:

using System;
using System.Text;

class Test
{
static void Main()
{
string test = new string('a', 10000);

if (Mark(test) != Austin(test))
{
throw new Exception
("Can't test - results aren't the same");
}

{
DateTime start = DateTime.Now;
for (int i=0; i < 100; i++)
{
Mark(test);
}
DateTime end = DateTime.Now;
Console.WriteLine ("Mark: {0}", end-start);
}

{
DateTime start = DateTime.Now;
for (int i=0; i < 100; i++)
{
Austin(test);
}
DateTime end = DateTime.Now;
Console.WriteLine ("Austin: {0}", end-start);
}
}

public static string Mark(string pstrEvent)
{
string strReturn = "";
for(int intPos = 0; intPos < pstrEvent.Length; intPos++)
{
strReturn += "&#" +
((int)Encoding.ASCII.GetBytes(pstrEvent)[intPos]).ToString() + ";";
}
return strReturn;
}

public static string Austin(string str)
{
StringBuilder sb = new StringBuilder();

foreach (char c in str)
{
sb.Append("&#");
sb.Append((int)c);
sb.Append(";");
}
return sb.ToString();
}
}

And the results:
Mark: 00:02:34.7968750
Austin: 00:00:00.7031250

For this particular data, Austin's version is about 220 times faster
than yours. For longer strings, that would go up - you'd be creating
more (and longer) intermediate strings, and you'd be encoding a longer
string every iteration.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
And the results:
Mark: 00:02:34.7968750
Austin: 00:00:00.7031250

For this particular data, Austin's version is about 220 times faster
than yours.


Well, that's good enough for me! Thanks very much. I'm now using Austin's
version instead of mine.
Nov 16 '05 #7

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

Similar topics

10
by: Marcin Kalicinski | last post by:
Why string literals are regarded as char * not as const char *? (1) void f(char *); (2) void f(const char *); f("foo") will call version (1) of function f. I understand that the exact type...
2
by: Thomas Matthews | last post by:
Hi, I'm working with Borland C++ Builder 6.2. My project uses the std::string class. However, Borland in its infinite wisdom has its own string class, AnsiString. To make my life easier, I...
12
by: ABeck | last post by:
Hello List, I have ar more or less academical question. Can there arise runtime errors in a program, if the include of <string.h> has been forgotten? If all the arguments to the functions of...
6
by: Marco Herrn | last post by:
Hi, I need to serialize an object into a string representation to store it into a database. So the SOAPFormatter seems to be the right formatter for this purpose. Now I have the problem that...
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
4
by: Russell Warren | last post by:
I've got a case where I want to convert binary blocks of data (various ctypes objects) to base64 strings. The conversion calls in the base64 module expect strings as input, so right now I'm...
10
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem...
5
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
10
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.