473,385 Members | 1,356 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.

There has to be a better way to work withat string chars

I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3. string firstChar = normalStateName.Substring(0, 1);
  4. string restOfName = normalStateName.Substring(1);
  5. string newName = "_" + firstChar.ToLower() + restOfName;
  6. return newName;
  7. }
  8.  
There must be a better way... is there?
Sep 21 '06 #1
33 1758
sklett,

I don't see why that is ugly. The code is pretty self-descriptive,
indicating exactly what you are doing.

AFAIK, there isn't a much better way to do this. You could use a
StringBuilder, and do this:

public static string MakeNetSuiteStateName(string normalStateName)
{
// The return value.
StringBuilder retVal = new StringBuilder("_" + normalStateName);

// Lower-case the second character.
retVal[1] = retval[1].ToLower();

// Return the string.
return retVal.ToString();
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }
  8.  

There must be a better way... is there?

Sep 21 '06 #2

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
|I need to take a string, make the first character lowercase and prepend an
| underscore character to it. Something like this:
| "California"
| "_california"
|
| Here is the (ugly) solution I cam up with:
|
Expand|Select|Wrap|Line Numbers
  1. | public static string MakeNetSuiteStateName(string normalStateName)
  2. | {
  3. |    string firstChar = normalStateName.Substring(0, 1);
  4. |    string restOfName = normalStateName.Substring(1);
  5. |    string newName = "_" + firstChar.ToLower() + restOfName;
  6. |    return newName;
  7. | }
|
| There must be a better way... is there?
|
|

public static string MakeNetSuiteStateName(string normalStateName)
{
return "_" + normalStateName.ToLower();
}

Willy.
Sep 21 '06 #3

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uu****************@TK2MSFTNGP04.phx.gbl...
>
"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
|I need to take a string, make the first character lowercase and prepend
an
| underscore character to it. Something like this:
| "California"
| "_california"
|
| Here is the (ugly) solution I cam up with:
|
Expand|Select|Wrap|Line Numbers
  1. | public static string MakeNetSuiteStateName(string normalStateName)
  2. | {
  3. |    string firstChar = normalStateName.Substring(0, 1);
  4. |    string restOfName = normalStateName.Substring(1);
  5. |    string newName = "_" + firstChar.ToLower() + restOfName;
  6. |    return newName;
  7. | }
|
| There must be a better way... is there?
|
|

public static string MakeNetSuiteStateName(string normalStateName)
{
return "_" + normalStateName.ToLower();
}

Willy.

Thanks Willy, but I only want to lower the first char in the normalStateName
string
Sep 21 '06 #4
Hi,

You can modify it slightly:

public static string MakeNetSuiteStateName(string normalStateName)
{
if (string.IsNullOrEmpty(normalStateName))
throw new ArgumentException("The specified string is null or empty.", "normalStateName");

// changes California to _california
// changes C to _c
return "_" + char.ToLower(normalStateName[0]) + normalStateName.Substring(1);
}

You could also use a RegEx instead, but I don't think it would provide any substantial benefit over your solution.

--
Dave Sexton

"sklett" <s@s.comwrote in message news:uQ**************@TK2MSFTNGP06.phx.gbl...
>I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }
  8.  

There must be a better way... is there?

Sep 21 '06 #5
Hi Nicholas,

I suppose it's not ugly, it just "feels" like it's taking more function
calls then it should. I suppose what I was looking for was a way to access
the chars in the string and change them, but the whole immutable thing isn't
cooperating ;0)

The StringBuilder solution is interesting. From a performance standpoint is
there much difference? I've never really evaluated the performance of
StringBuilder, I've heard it's a good choice for concatenation... but beyond
that I know very little.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
sklett,

I don't see why that is ugly. The code is pretty self-descriptive,
indicating exactly what you are doing.

AFAIK, there isn't a much better way to do this. You could use a
StringBuilder, and do this:

public static string MakeNetSuiteStateName(string normalStateName)
{
// The return value.
StringBuilder retVal = new StringBuilder("_" + normalStateName);

// Lower-case the second character.
retVal[1] = retval[1].ToLower();

// Return the string.
return retVal.ToString();
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>>I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }

There must be a better way... is there?


Sep 21 '06 #6
sklett,

There is in the microsoft.visualbasic namespace a method for this.

But I would not even set a reference for this simple problem to that.
(Before you misunderstand it, that namespace is as any other real .net
namespace as they are in system.Net and can be used like that in C# as)

Cor
"sklett" <s@s.comschreef in bericht
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }
  8.  

There must be a better way... is there?

Sep 21 '06 #7
I think in this case, comparing a StringBuilder which is properly
allocated (for the exact number of characters you need) compared to a string
concatenation (which calculates the length of the buffer you need, then
performs a copy) is going to provide negligible results. I can't imagine
any one is horribly faster than another.

If anything, I think that the Substring operations are going to be a
little slower than the string buffer method. You have one concat (in the
string builder constructor call), one copy (from the input parameter of the
string builder) to the buffer in the string builder, a replacement (of the
character) and then another string generation (the call to ToString).

The calls to substring IMO are going to be a little more costly.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sklett" <s@s.comwrote in message
news:eH*************@TK2MSFTNGP05.phx.gbl...
Hi Nicholas,

I suppose it's not ugly, it just "feels" like it's taking more function
calls then it should. I suppose what I was looking for was a way to
access the chars in the string and change them, but the whole immutable
thing isn't cooperating ;0)

The StringBuilder solution is interesting. From a performance standpoint
is there much difference? I've never really evaluated the performance of
StringBuilder, I've heard it's a good choice for concatenation... but
beyond that I know very little.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2****************@TK2MSFTNGP03.phx.gbl...
>sklett,

I don't see why that is ugly. The code is pretty self-descriptive,
indicating exactly what you are doing.

AFAIK, there isn't a much better way to do this. You could use a
StringBuilder, and do this:

public static string MakeNetSuiteStateName(string normalStateName)
{
// The return value.
StringBuilder retVal = new StringBuilder("_" + normalStateName);

// Lower-case the second character.
retVal[1] = retval[1].ToLower();

// Return the string.
return retVal.ToString();
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>>>I need to take a string, make the first character lowercase and prepend
an underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }

There must be a better way... is there?



Sep 21 '06 #8
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
I think in this case, comparing a StringBuilder which is properly
allocated (for the exact number of characters you need) compared to a string
concatenation (which calculates the length of the buffer you need, then
performs a copy) is going to provide negligible results. I can't imagine
any one is horribly faster than another.

If anything, I think that the Substring operations are going to be a
little slower than the string buffer method. You have one concat (in the
string builder constructor call), one copy (from the input parameter of the
string builder) to the buffer in the string builder, a replacement (of the
character) and then another string generation (the call to ToString).

The calls to substring IMO are going to be a little more costly.
StringBuilder is probably not internally implemented in C#, but rather in
native code; thus, the allocating of memory and copying bytes will be
optimized as opposed to doing it with substrings in C#. Most C/C++ develops
likely understand this implicitly.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #9

"sklett" <s@s.comwrote in message
news:Oe**************@TK2MSFTNGP02.phx.gbl...
|
| "Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
| news:uu****************@TK2MSFTNGP04.phx.gbl...
| >
| "sklett" <s@s.comwrote in message
| news:uQ**************@TK2MSFTNGP06.phx.gbl...
| |I need to take a string, make the first character lowercase and prepend
| an
| | underscore character to it. Something like this:
| | "California"
| | "_california"
| |
| | Here is the (ugly) solution I cam up with:
| |
Expand|Select|Wrap|Line Numbers
  1. | | public static string MakeNetSuiteStateName(string normalStateName)
  2. | | {
  3. | |    string firstChar = normalStateName.Substring(0, 1);
  4. | |    string restOfName = normalStateName.Substring(1);
  5. | |    string newName = "_" + firstChar.ToLower() + restOfName;
  6. | |    return newName;
  7. | | }
  8. | | 
| |
| | There must be a better way... is there?
| |
| |
| >
| public static string MakeNetSuiteStateName(string normalStateName)
| {
| return "_" + normalStateName.ToLower();
| }
| >
| Willy.
| >
| >
|
| Thanks Willy, but I only want to lower the first char in the
normalStateName
| string
|

I see (didn't know could have more uppercase characters ;-)), in that case
keep your code, or use a StringBuilder like Nicholas suggested.
Willy.
Sep 21 '06 #10
Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one of
these three methods.

As for the implicit understanding statement. I guess that would be
subject to review as well =)

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:ll****************@textfe.usenetserver.com...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
> I think in this case, comparing a StringBuilder which is properly
allocated (for the exact number of characters you need) compared to a
string
concatenation (which calculates the length of the buffer you need, then
performs a copy) is going to provide negligible results. I can't imagine
any one is horribly faster than another.

If anything, I think that the Substring operations are going to be a
little slower than the string buffer method. You have one concat (in the
string builder constructor call), one copy (from the input parameter of
the
string builder) to the buffer in the string builder, a replacement (of
the
character) and then another string generation (the call to ToString).

The calls to substring IMO are going to be a little more costly.

StringBuilder is probably not internally implemented in C#, but rather in
native code; thus, the allocating of memory and copying bytes will be
optimized as opposed to doing it with substrings in C#. Most C/C++
develops
likely understand this implicitly.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1


Sep 21 '06 #11
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
The calls to substring IMO are going to be a little more costly.

StringBuilder is probably not internally implemented in C#, but rather in
native code; thus, the allocating of memory and copying bytes will be
optimized as opposed to doing it with substrings in C#. Most C/C++ develops
likely understand this implicitly.
Very, very little of StringBuilder is implemented directly in native
code, as far as I can tell with Reflector.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #12
Wow, I turn my head for 10 minutes and come back to a ton of posts, neat!

Thanks all for the info, nice little thread. It helped me out.
In case anyones wondering, I'm leaving my code as is for now, but am going
to play with stringBuilder some more to see what other uses I have for it!

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }
  8.  

There must be a better way... is there?

Sep 21 '06 #13
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one of
these three methods.
That is exactly what I was saying ... implemented in native code ... API is
native C.
As for the implicit understanding statement. I guess that would be
subject to review as well =)
No ... I think we agree ... API calls [and generally unsafe code as well] is
going to be compiled native code, such as compiled C [as is the case with the
Windows API].

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #14
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
The calls to substring IMO are going to be a little more costly.

StringBuilder is probably not internally implemented in C#, but rather in
native code; thus, the allocating of memory and copying bytes will be
optimized as opposed to doing it with substrings in C#. Most C/C++ develops
likely understand this implicitly.

Very, very little of StringBuilder is implemented directly in native
code, as far as I can tell with Reflector.
Reflector as in "reflection". As far as I am concerned, it would only have a
use in StringBuilder.AppendFormat(...). Otherwise, I expect calls like
memcpy, wstrcpy, etc (or equivalents that work with BSTR). My point is that
such manipulation is FAR faster in native code with direct memory access, so
implementation in this manner makes sense.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #15
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one of
these three methods.

That is exactly what I was saying ... implemented in native code ... API is
native C.
Well, which bit are you suggesting is in native code? Looking in
Reflector I can't see many calls to native methods... I dare say there
may be *some* bits which use native calls, but that's a long way from
what you originally said.
As for the implicit understanding statement. I guess that would be
subject to review as well =)

No ... I think we agree ... API calls [and generally unsafe code as well] is
going to be compiled native code, such as compiled C [as is the case with the
Windows API].
Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
to code which is still IL.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #16
To add more fuel to the fire, the calls that ultimately call unmanaged
code are Append and Replace. Insert and Remove are completely managed calls
(ultimately).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
>Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one
of
these three methods.

That is exactly what I was saying ... implemented in native code ... API
is
native C.

Well, which bit are you suggesting is in native code? Looking in
Reflector I can't see many calls to native methods... I dare say there
may be *some* bits which use native calls, but that's a long way from
what you originally said.
As for the implicit understanding statement. I guess that would be
subject to review as well =)

No ... I think we agree ... API calls [and generally unsafe code as well]
is
going to be compiled native code, such as compiled C [as is the case with
the
Windows API].

Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
to code which is still IL.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 21 '06 #17
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
>Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one of
these three methods.

That is exactly what I was saying ... implemented in native code ... API is
native C.

Well, which bit are you suggesting is in native code? Looking in
Reflector I can't see many calls to native methods... I dare say there
may be *some* bits which use native calls, but that's a long way from
what you originally said.
I am indicating that the code in StringBuilder that creates the final output
string (ToString()) will use native code (via unsafe code, or perhaps Windows
or C API calls).
>
As for the implicit understanding statement. I guess that would be
subject to review as well =)

No ... I think we agree ... API calls [and generally unsafe code as well] is
going to be compiled native code, such as compiled C [as is the case with the
Windows API].

Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
to code which is still IL.
Unsafe means it is not proteced from memory issues. Any calls to native
compiled code or C/Windows API will be unsafe by nature.

My reference to C/C++ developers inherently understanding seems to have
stirred a few people up. It was NOT a dig at other programmers; it was simply
a statement that C/C++ developers understand programming using memory
managment and how to reference memory, free it, allocate it (both stack and heap
allocation) and how to move it around. It is clear what must happen and it
seems unlikely there would be a lot of gain using StringBuilder without such
calls made to native code to do this.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #18

"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:l6*****************@textfe.usenetserver.com.. .
| Jon Skeet [C# MVP] <sk***@pobox.comwrote:
| Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| The calls to substring IMO are going to be a little more costly.
| >>
| >StringBuilder is probably not internally implemented in C#, but rather
in
| >native code; thus, the allocating of memory and copying bytes will be
| >optimized as opposed to doing it with substrings in C#. Most C/C++
develops
| >likely understand this implicitly.
| >
| Very, very little of StringBuilder is implemented directly in native
| code, as far as I can tell with Reflector.
| >
|
| Reflector as in "reflection". As far as I am concerned, it would only
have a
| use in StringBuilder.AppendFormat(...). Otherwise, I expect calls like
| memcpy, wstrcpy, etc (or equivalents that work with BSTR). My point is
that
| such manipulation is FAR faster in native code with direct memory access,
so
| implementation in this manner makes sense.
|
| --
| Thomas T. Veldhouse
| Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
|
|

No, V2 FCL StringBuilder is almost completely implemented in C# using unsafe
code constructs for performance critical path's. It doesn't even rely upon
the CRT. All there is is a couple of internal calls (the runtime), for
instance to allocate the initial string.
But all this is moot as mscorlib is native code (ngen'd) anyway.

Willy.

Sep 21 '06 #19
Willy Denoyette [MVP] <wi*************@telenet.bewrote:
>
"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:l6*****************@textfe.usenetserver.com.. .
| Jon Skeet [C# MVP] <sk***@pobox.comwrote:
| Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| The calls to substring IMO are going to be a little more costly.
| >>
| >StringBuilder is probably not internally implemented in C#, but rather
in
| >native code; thus, the allocating of memory and copying bytes will be
| >optimized as opposed to doing it with substrings in C#. Most C/C++
develops
| >likely understand this implicitly.
| >
| Very, very little of StringBuilder is implemented directly in native
| code, as far as I can tell with Reflector.
| >
|
| Reflector as in "reflection". As far as I am concerned, it would only
have a
| use in StringBuilder.AppendFormat(...). Otherwise, I expect calls like
| memcpy, wstrcpy, etc (or equivalents that work with BSTR). My point is
that
| such manipulation is FAR faster in native code with direct memory access,
so
| implementation in this manner makes sense.
|
| --
| Thomas T. Veldhouse
| Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
|
|

No, V2 FCL StringBuilder is almost completely implemented in C# using unsafe
code constructs for performance critical path's. It doesn't even rely upon
the CRT. All there is is a couple of internal calls (the runtime), for
instance to allocate the initial string.
But all this is moot as mscorlib is native code (ngen'd) anyway.
Bingo. If you have access, care to share the code? If there are calls into
mscorlib, I think we are very likely agreeing.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
Sep 21 '06 #20
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
Very, very little of StringBuilder is implemented directly in native
code, as far as I can tell with Reflector.

Reflector as in "reflection".
No, not really. Reflector as in "showing you the IL implementation".
Have you never used it?
As far as I am concerned, it would only have a use in
StringBuilder.AppendFormat(...). Otherwise, I expect calls like
memcpy, wstrcpy, etc (or equivalents that work with BSTR). My point
is that such manipulation is FAR faster in native code with direct
memory access, so implementation in this manner makes sense.
And my point is that very little of StringBuilder is implemented with
native code.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #21
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
Well, which bit are you suggesting is in native code? Looking in
Reflector I can't see many calls to native methods... I dare say there
may be *some* bits which use native calls, but that's a long way from
what you originally said.

I am indicating that the code in StringBuilder that creates the final output
string (ToString()) will use native code (via unsafe code, or perhaps Windows
or C API calls).
You really need to understand the difference between native code and
unsafe code. They're completely different.

Anyway, I see no evidence that StringBuilder.ToString uses native code
any more than calls to String.Substring does. Both use
FastAllocateString eventually, which *is* an external call, but that's
about it as far as I can tell. If you believe there's more to it than
that, it would be nice if you'd present some evidence.
Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
to code which is still IL.

Unsafe means it is not proteced from memory issues. Any calls to native
compiled code or C/Windows API will be unsafe by nature.
No, that will be unmanaged. Not unsafe in the normal understanding of
it in the context of .NET.
My reference to C/C++ developers inherently understanding seems to have
stirred a few people up.
Not at all - your incorrect guess at the implementation of
StringBuilder was what stirred me up.
It was NOT a dig at other programmers; it was simply
a statement that C/C++ developers understand programming using memory
managment and how to reference memory, free it, allocate it (both stack and heap
allocation) and how to move it around. It is clear what must happen and it
seems unlikely there would be a lot of gain using StringBuilder without such
calls made to native code to do this.
That's where you're wrong. The principal reason for using StringBuilder
is to avoid making copies of strings unnecessarily. See
http://www.pobox.com/~skeet/csharp/stringbuilder.html for more of an
explanation. The optimisation is to do with how much copying is
required, not how fast each copy is.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #22
Agreed, for Replace which performs an internal call, but not for Append (at
least not that I see an internal call or a call to native code libraries).
StringBuilder (like some other methods) was completely re-written for V2,
favoring safety over speed, that means not relying on native CRT libraries
(using PInvoke) which could compromise this safety, so all you will see are
some internal calls, that's all.

Willy.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
| To add more fuel to the fire, the calls that ultimately call unmanaged
| code are Append and Replace. Insert and Remove are completely managed
calls
| (ultimately).
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard.caspershouse.com
|
| "Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
| news:MP************************@msnews.microsoft.c om...
| Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| >Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
| Thomas,
| >
| In that case, you would be wrong. Stringbuilder is a combination
of
| managed code, unsafe code (still managed) and calls to API functions.
| Depending on the operation, the strings are copied or modified with
one
| of
| these three methods.
| >>
| >That is exactly what I was saying ... implemented in native code ...
API
| >is
| >native C.
| >
| Well, which bit are you suggesting is in native code? Looking in
| Reflector I can't see many calls to native methods... I dare say there
| may be *some* bits which use native calls, but that's a long way from
| what you originally said.
| >
| As for the implicit understanding statement. I guess that would
be
| subject to review as well =)
| >
| >>
| >No ... I think we agree ... API calls [and generally unsafe code as
well]
| >is
| >going to be compiled native code, such as compiled C [as is the case
with
| >the
| >Windows API].
| >
| Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
| to code which is still IL.
| >
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
|
|
Sep 21 '06 #23
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
No, V2 FCL StringBuilder is almost completely implemented in C# using unsafe
code constructs for performance critical path's. It doesn't even rely upon
the CRT. All there is is a couple of internal calls (the runtime), for
instance to allocate the initial string.
But all this is moot as mscorlib is native code (ngen'd) anyway.

Bingo. If you have access, care to share the code? If there are calls into
mscorlib, I think we are very likely agreeing.
I think you misunderstood Willy's post. mscorlib is the assembly where
String and StringBuilder live - but they're both implemented (almost
entirely) in IL. Now, they're pre-compiled to native code, but that's
just really just another type of JIT - it's not like .NET code is ever
interpreted. The majority of the code is still managed code, and could
well be implemented in C#, contrary to your original guess.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #24

"Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
news:UG*****************@textfe.usenetserver.com.. .
| Willy Denoyette [MVP] <wi*************@telenet.bewrote:
| >
| "Thomas T. Veldhouse" <ve*****@yahoo.comwrote in message
| news:l6*****************@textfe.usenetserver.com.. .
| | Jon Skeet [C# MVP] <sk***@pobox.comwrote:
| | Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| | The calls to substring IMO are going to be a little more
costly.
| | >>
| | >StringBuilder is probably not internally implemented in C#, but
rather
| in
| | >native code; thus, the allocating of memory and copying bytes will
be
| | >optimized as opposed to doing it with substrings in C#. Most C/C++
| develops
| | >likely understand this implicitly.
| | >
| | Very, very little of StringBuilder is implemented directly in native
| | code, as far as I can tell with Reflector.
| | >
| |
| | Reflector as in "reflection". As far as I am concerned, it would only
| have a
| | use in StringBuilder.AppendFormat(...). Otherwise, I expect calls
like
| | memcpy, wstrcpy, etc (or equivalents that work with BSTR). My point
is
| that
| | such manipulation is FAR faster in native code with direct memory
access,
| so
| | implementation in this manner makes sense.
| |
| | --
| | Thomas T. Veldhouse
| | Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1
| |
| |
| >
| No, V2 FCL StringBuilder is almost completely implemented in C# using
unsafe
| code constructs for performance critical path's. It doesn't even rely
upon
| the CRT. All there is is a couple of internal calls (the runtime), for
| instance to allocate the initial string.
| But all this is moot as mscorlib is native code (ngen'd) anyway.
| >
|
| Bingo. If you have access, care to share the code? If there are calls
into
| mscorlib, I think we are very likely agreeing.
StringBuilder does not call into mscorlib, mscorlib is the assembly that
holds StringBuilder (and all the core framework classes), it is compiled to
native code (ngen'd) at framework install time.
So if you want to see the code take a look at it using reflector or ildasm.
Note that most assemblies are pre-compiled (ngen'd) into native code since
V2.

Willy.

Sep 21 '06 #25

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
| Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| No, V2 FCL StringBuilder is almost completely implemented in C# using
unsafe
| code constructs for performance critical path's. It doesn't even rely
upon
| the CRT. All there is is a couple of internal calls (the runtime), for
| instance to allocate the initial string.
| But all this is moot as mscorlib is native code (ngen'd) anyway.
| >
| Bingo. If you have access, care to share the code? If there are calls
into
| mscorlib, I think we are very likely agreeing.
|
| I think you misunderstood Willy's post. mscorlib is the assembly where
| String and StringBuilder live - but they're both implemented (almost
| entirely) in IL. Now, they're pre-compiled to native code, but that's
| just really just another type of JIT - it's not like .NET code is ever
| interpreted. The majority of the code is still managed code, and could
| well be implemented in C#, contrary to your original guess.
|
| --

That's correct, I find it amusing when people say '... running managed code
... ", at program execution time everything that get's executed is native
code, we still don't have CPU's that can run IL ;-)

Willy.
Sep 21 '06 #26
Willy,

You are right, I saw the call to string.wstrcpy and I just assumed it
was a P/Invoke call. That being said, of the four main operations, Replace
is the only one I see which is an internal call.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:u1**************@TK2MSFTNGP02.phx.gbl...
Agreed, for Replace which performs an internal call, but not for Append
(at
least not that I see an internal call or a call to native code libraries).
StringBuilder (like some other methods) was completely re-written for V2,
favoring safety over speed, that means not relying on native CRT libraries
(using PInvoke) which could compromise this safety, so all you will see
are
some internal calls, that's all.

Willy.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in
message news:%2****************@TK2MSFTNGP04.phx.gbl...
| To add more fuel to the fire, the calls that ultimately call
unmanaged
| code are Append and Replace. Insert and Remove are completely managed
calls
| (ultimately).
|
| --
| - Nicholas Paldino [.NET/C# MVP]
| - mv*@spam.guard.caspershouse.com
|
| "Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
| news:MP************************@msnews.microsoft.c om...
| Thomas T. Veldhouse <ve*****@yahoo.comwrote:
| >Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com>
wrote:
| Thomas,
| >
| In that case, you would be wrong. Stringbuilder is a
combination
of
| managed code, unsafe code (still managed) and calls to API
functions.
| Depending on the operation, the strings are copied or modified with
one
| of
| these three methods.
| >>
| >That is exactly what I was saying ... implemented in native code ...
API
| >is
| >native C.
| >
| Well, which bit are you suggesting is in native code? Looking in
| Reflector I can't see many calls to native methods... I dare say there
| may be *some* bits which use native calls, but that's a long way from
| what you originally said.
| >
| As for the implicit understanding statement. I guess that would
be
| subject to review as well =)
| >
| >>
| >No ... I think we agree ... API calls [and generally unsafe code as
well]
| >is
| >going to be compiled native code, such as compiled C [as is the case
with
| >the
| >Windows API].
| >
| Unsafe code isn't the same as unmanaged code. Normally "unsafe" refers
| to code which is still IL.
| >
| --
| Jon Skeet - <sk***@pobox.com>
| http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
| If replying to the group, please do not mail me too
|
|


Sep 21 '06 #27
I am bamboozled that nobody has mentioned using
System.GlobalizationTextInfo.ToTitleCase
(http://msdn2.microsoft.com/en-us/lib...itlecase.aspx).

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer

A watched clock never boils.

"sklett" <s@s.comwrote in message
news:O6**************@TK2MSFTNGP04.phx.gbl...
Wow, I turn my head for 10 minutes and come back to a ton of posts, neat!

Thanks all for the info, nice little thread. It helped me out.
In case anyones wondering, I'm leaving my code as is for now, but am going
to play with stringBuilder some more to see what other uses I have for it!

"sklett" <s@s.comwrote in message
news:uQ**************@TK2MSFTNGP06.phx.gbl...
>>I need to take a string, make the first character lowercase and prepend an
underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }

There must be a better way... is there?


Sep 21 '06 #28
Kevin Spencer <uc*@ftc.govwrote:
I am bamboozled that nobody has mentioned using
System.GlobalizationTextInfo.ToTitleCase
(http://msdn2.microsoft.com/en-us/lib...itlecase.aspx).
That goes the wrong way round though. The idea is to only change the
first character, but to *lowercase*, not uppercase.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 21 '06 #29

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
| Willy,
|
| You are right, I saw the call to string.wstrcpy and I just assumed it
| was a P/Invoke call. That being said, of the four main operations,
Replace
| is the only one I see which is an internal call.
|
|

Nicholas ,

True, wstrcpy is one of the methods (there are others) that is a
(simplified) copy of the safe CRT counterpart.

Willy.

Sep 21 '06 #30
Hi Kevin,

Well it doesn't address the problem and anyway the docs say that its behavior is not guaranteed and that it might change in the
future!

--
Dave Sexton

"Kevin Spencer" <uc*@ftc.govwrote in message news:OT**************@TK2MSFTNGP03.phx.gbl...
>I am bamboozled that nobody has mentioned using System.GlobalizationTextInfo.ToTitleCase
(http://msdn2.microsoft.com/en-us/lib...itlecase.aspx).

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer

A watched clock never boils.

"sklett" <s@s.comwrote in message news:O6**************@TK2MSFTNGP04.phx.gbl...
>Wow, I turn my head for 10 minutes and come back to a ton of posts, neat!

Thanks all for the info, nice little thread. It helped me out.
In case anyones wondering, I'm leaving my code as is for now, but am going to play with stringBuilder some more to see what other
uses I have for it!

"sklett" <s@s.comwrote in message news:uQ**************@TK2MSFTNGP06.phx.gbl...
>>>I need to take a string, make the first character lowercase and prepend an underscore character to it. Something like this:
"California"
"_california"

Here is the (ugly) solution I cam up with:
Expand|Select|Wrap|Line Numbers
  1. public static string MakeNetSuiteStateName(string normalStateName)
  2. {
  3.    string firstChar = normalStateName.Substring(0, 1);
  4.    string restOfName = normalStateName.Substring(1);
  5.    string newName = "_" + firstChar.ToLower() + restOfName;
  6.    return newName;
  7. }

There must be a better way... is there?



Sep 21 '06 #31
Darn! I read most of the messages several times, but what I expected to see
must have affected my perception. Never mind!

--
:-P,

Kevin Spencer
Microsoft MVP
Software Composer

A watched clock never boils.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Kevin Spencer <uc*@ftc.govwrote:
>I am bamboozled that nobody has mentioned using
System.GlobalizationTextInfo.ToTitleCase
(http://msdn2.microsoft.com/en-us/lib...itlecase.aspx).

That goes the wrong way round though. The idea is to only change the
first character, but to *lowercase*, not uppercase.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Sep 22 '06 #32

"Thomas T. Veldhouse" <ve*****@yahoo.comschrieb im Newsbeitrag
news:U0***************@textfe.usenetserver.com...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.comwrote:
>Thomas,

In that case, you would be wrong. Stringbuilder is a combination of
managed code, unsafe code (still managed) and calls to API functions.
Depending on the operation, the strings are copied or modified with one
of
these three methods.

That is exactly what I was saying ... implemented in native code ... API
is
native C.
> As for the implicit understanding statement. I guess that would be
subject to review as well =)

No ... I think we agree ... API calls [and generally unsafe code as well]
is
going to be compiled native code, such as compiled C [as is the case with
the
Windows API].
managed code will be native code aswell, isn't it?
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1


Sep 22 '06 #33
Christof Nordiek wrote:
No ... I think we agree ... API calls [and generally unsafe code as well]
is going to be compiled native code, such as compiled C [as is the case with
the Windows API].
managed code will be native code aswell, isn't it?
By the time it's executed, yes.

Jon

Sep 22 '06 #34

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

Similar topics

0
by: Daniel | last post by:
Is this a bug in .net file io? is there a work around? when writing a large file with using(StreamWriter ... if the network drive of the share is removed then the file is never closed. I tried...
3
by: baibaichen | last post by:
hi i want to output strings with indent space, the code looks like: std::set<std::string> files; std::set<std::string> iterator b = files.begin(); std::set<std::string> iterator e =...
1
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: 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...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.