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

S

sklett

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:
Code:
public static string MakeNetSuiteStateName(string normalStateName)
{
string firstChar = normalStateName.Substring(0, 1);
string restOfName = normalStateName.Substring(1);
string newName = "_" + firstChar.ToLower() + restOfName;
return newName;
}

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

Nicholas Paldino [.NET/C# MVP]

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.
 
W

Willy Denoyette [MVP]

|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:
|
Code:
| public static string MakeNetSuiteStateName(string normalStateName)
| {
|    string firstChar = normalStateName.Substring(0, 1);
|    string restOfName = normalStateName.Substring(1);
|    string newName = "_" + firstChar.ToLower() + restOfName;
|    return newName;
| }
|
|
| There must be a better way... is there?
|
|

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

Willy.
 
S

sklett

Willy Denoyette said:
|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:
|
Code:
| public static string MakeNetSuiteStateName(string normalStateName)
| {
|    string firstChar = normalStateName.Substring(0, 1);
|    string restOfName = normalStateName.Substring(1);
|    string newName = "_" + firstChar.ToLower() + restOfName;
|    return newName;
| }
|
|
| 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
 
S

sklett

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 said:
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]
- (e-mail address removed)

sklett said:
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:
Code:
public static string MakeNetSuiteStateName(string normalStateName)
{
string firstChar = normalStateName.Substring(0, 1);
string restOfName = normalStateName.Substring(1);
string newName = "_" + firstChar.ToLower() + restOfName;
return newName;
}

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

Dave Sexton

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.
 
C

Cor Ligthert [MVP]

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

sklett said:
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 said:
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]
- (e-mail address removed)

sklett said:
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:
Code:
public static string MakeNetSuiteStateName(string normalStateName)
{
string firstChar = normalStateName.Substring(0, 1);
string restOfName = normalStateName.Substring(1);
string newName = "_" + firstChar.ToLower() + restOfName;
return newName;
}

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

Thomas T. Veldhouse

Nicholas Paldino said:
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.
 
W

Willy Denoyette [MVP]

|
| | >
| > | > |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:
| > |
Code:
| > | public static string MakeNetSuiteStateName(string normalStateName)
| > | {
| > |    string firstChar = normalStateName.Substring(0, 1);
| > |    string restOfName = normalStateName.Substring(1);
| > |    string newName = "_" + firstChar.ToLower() + restOfName;
| > |    return newName;
| > | }
| > |
| > |
| > | 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.
 
N

Nicholas Paldino [.NET/C# MVP]

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 =)
 
J

Jon Skeet [C# MVP]

Thomas T. Veldhouse said:
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.
 
S

sklett

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!
 
T

Thomas T. Veldhouse

Nicholas Paldino said:
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].
 
T

Thomas T. Veldhouse

Jon Skeet said:
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.
 
J

Jon Skeet [C# MVP]

Thomas T. Veldhouse said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Jon Skeet said:
Thomas T. Veldhouse said:
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.
 
T

Thomas T. Veldhouse

Jon Skeet said:
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.
 
W

Willy Denoyette [MVP]

| >> > 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.
 
J

Jon Skeet [C# MVP]

Thomas T. Veldhouse said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top