How to initialize a string variable to a length of 500?

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length I'll
store the path string in the variable.

1) Is there a better way.

2)If not, how do I initialize a String to have a length of 500 characters?

3)I believe the longest full path name is 460 characters (I'm not sure if
it needs a couple of spaces for nulls at the end) so really a initial length
of 460 would be long enough. Right?


Thanks
 
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length I'll
store the path string in the variable.

1) Is there a better way.

I would just save the array index or a ref to the shortest.
2)If not, how do I initialize a String to have a length of 500 characters?

There is a constructor that can do that.

But this is C# not C.

I can not see any reason why you would want to do that.
3)I believe the longest full path name is 460 characters (I'm not sure if
it needs a couple of spaces for nulls at the end) so really a initial length
of 460 would be long enough. Right?

I don't know the max path length.

But it will not have nul byte(s) at the end in C#.

Arne
 
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length I'll
store the path string in the variable.

1) Is there a better way.
Your approach would be fine for C, but it makes no sense for C#. Strings are
immutable, so there's no point to copying them. Just do

int minLength = int.MaxValue;
string shortestPath;
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < minLength) {
shortestPath = paths;
minLength = shortestPath.Length;
}
}

If you need a modified path rather than the original one, just replace
"paths" with the appropriate expression. This will create garbage
strings, but don't worry about this unless you can show that this is
important through profiling. The .NET garbage collector does a very good job.

If it turns out you do need to optimize this, you could use a StringBuilder
with a sufficient capacity. You can set .Length to 0 to clear it, and
..Append() to set its contents. Again, don't do this prematurely, as it
hardly improves readability.
 
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length
I'll store the path string in the variable.

1) Is there a better way.

I believe that you are thinking the C way, and not the C# way. In C#,
strings are IMMUTABLE, meaning that once you have constructed a string, its
value cannot be changed. Any change to a string involves constructing a new
string, and assigning its reference to the variable that was previously
pointing to the old string.

Your loop would not copy the shortest path into a string, but instead it
would copy the REFERENCE to the shortest path. Therefore, you don't need to
preassign any space for your copied path.

string[] arrayOfPaths = new string[n];
///... preload arrayOfPaths
string shortestPath = "";
foreach (string s in arrayOfPaths)
{
if (s.Length<shortestPath.Length)
shortestPath = s; //this copies a reference
}
//shortestPath contains now the shortest path, or an empty string if the
array was empty.

2)If not, how do I initialize a String to have a length of 500 characters?

You can do
string s=new string(' ',500);

but this is unnecessary in your specific case.
 
Hi,

Why do you not simple check the length property of every string

Be aware that this gives the length, the visible part of string can be
delimited by a null byte.

Cor
 
Jeroen Mostert said:
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length
I'll store the path string in the variable.

1) Is there a better way.
Your approach would be fine for C, but it makes no sense for C#. Strings
are immutable, so there's no point to copying them. Just do

int minLength = int.MaxValue;
string shortestPath;
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < minLength) {
shortestPath = paths;
minLength = shortestPath.Length;
}
}

I like the above since there is no magic number in it.
But is there some other reason it is better than below?

Thanks a lot


string shortestPath=new string(' ',500);
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < shortestPath.Length;) {
shortestPath = paths;
}
}
 
Arne Vajhøj said:
I would just save the array index or a ref to the shortest.


There is a constructor that can do that.

But this is C# not C.

I can not see any reason why you would want to do that.

I was shown by Alberto that I don't need to but I wanted the initial string
to be bigger than the real ones so that it would get replaced by one of them
in the loop.

Thanks
 
Alberto Poblacion said:
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length
I'll store the path string in the variable.

1) Is there a better way.

I believe that you are thinking the C way, and not the C# way. In C#,
strings are IMMUTABLE, meaning that once you have constructed a string,
its value cannot be changed. Any change to a string involves constructing
a new string, and assigning its reference to the variable that was
previously pointing to the old string.

Your loop would not copy the shortest path into a string, but instead
it would copy the REFERENCE to the shortest path. Therefore, you don't
need to preassign any space for your copied path.

string[] arrayOfPaths = new string[n];
///... preload arrayOfPaths
string shortestPath = "";
foreach (string s in arrayOfPaths)
{
if (s.Length<shortestPath.Length)
shortestPath = s; //this copies a reference
}
//shortestPath contains now the shortest path, or an empty string if the
array was empty.

When you enter the loop shortestPath.Length equals zero, doesn't it?
You will never find a lower value in s.Length.
That is why I wanted to do:
string shortestPath = new string(' ',500);

Am I missing something?

Thanks
 
Cor Ligthert said:
Hi,

Why do you not simple check the length property of every string

Be aware that this gives the length, the visible part of string can be
delimited by a null byte.

Cor

Back when I wrote c I always wondered if the 460 included the null or not.

I think 460 is the correct value and I think I used 460 for the size of the
buffer.

Can't really remember for sure.

Thanks


hanks
 
AAaron123 said:
I was shown by Alberto that I don't need to but I wanted the initial string
to be bigger than the real ones so that it would get replaced by one of them
in the loop.

It does not make sense in C#.

You can not change the content of a String.

You can only create a new String.

Arne
 
AAaron123 said:
That is why I wanted to do:
string shortestPath = new string(' ',500);

There are no point.

string s = null;
s = someotherstring;

string s = new string(' ', 500);
s = someotherstring;

are functionally equivalent - the last one just allocate
an object you don't need and are therefore a runtime waste
and source code clutter.

Arne
 
Arne Vajhøj said:
It does not make sense in C#.

You can not change the content of a String.

You can only create a new String.

Arne
Instead of:
I'll store the path string in the variable.
Maybe I should have said:
I'll store a reference to the path string in the variable.
I didn't because it is always a reference that get stored.

Thanks for the interest
 
Instead of:
I'll store the path string in the variable.
Maybe I should have said:
I'll store a reference to the path string in the variable.
I didn't because it is always a reference that get stored.

But when you store a new ref, then it is irrelevant how
how long the object ref'ed previously are.

Arne
 
AAaron123 said:
s.Length here equal zero

More like an exception.
s.Length here does not equal zero


If there is an if on the length before the assignment the two sets above are
different.
Right?

Correct.

But I do not see the point.

You can store the length in an int or if you prefer
to use string length, then just start with the first
array element instead of creating a new string with
spaces.

Arne
 
AAaron123 said:
Jeroen Mostert said:
AAaron123 said:
I want to loop through an array of strings (full file paths) and find the
shortest one.

So I'd like to create a string variable of about 500 characters long and
each time I fine a file path string shorter than the variable's length
I'll store the path string in the variable.

1) Is there a better way.
Your approach would be fine for C, but it makes no sense for C#. Strings
are immutable, so there's no point to copying them. Just do

int minLength = int.MaxValue;
string shortestPath;
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < minLength) {
shortestPath = paths;
minLength = shortestPath.Length;
}
}

I like the above since there is no magic number in it.
But is there some other reason it is better than below?

Thanks a lot


string shortestPath=new string(' ',500);
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < shortestPath.Length;) {
shortestPath = paths;
}
}

There are two:

- No magic number, as you said;
- Doesn't allocate a string with length 500 only to destroy it later if a
shorter path is in fact found (and this is what happens; the runtime will
not "copy over" your preallocated string).

There are also obvious differences in how both pieces of code can be
optimized, but I'm not going to claim one will definitely perform better
than the other because I don't know the jitter that well. My gut tells me my
version is easier to optimize, but it's not impossible to keep
"shortestPath.Length" in a register, just not as easy. And gut feelings are
notoriously unreliable when it comes to performance, and we haven't
established that performance is even critical here, etcetera -- I'm just
mentioning this for completeness.

In any case, eliminating the magic number alone would be enough reason for
me. I hate MAX_PATH, and the next tool that says "Path too long" is going to
get punched in the face. Eliminating these constants from our code probably
won't help in that regard, but there's no reason to perpetuate this either.
 
Jeroen Mostert said:
AAaron123 said:
Jeroen Mostert said:
AAaron123 wrote:
I want to loop through an array of strings (full file paths) and find
the shortest one.

So I'd like to create a string variable of about 500 characters long
and each time I fine a file path string shorter than the variable's
length I'll store the path string in the variable.

1) Is there a better way.

Your approach would be fine for C, but it makes no sense for C#. Strings
are immutable, so there's no point to copying them. Just do

int minLength = int.MaxValue;
string shortestPath;
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < minLength) {
shortestPath = paths;
minLength = shortestPath.Length;
}
}

I like the above since there is no magic number in it.
But is there some other reason it is better than below?

Thanks a lot


string shortestPath=new string(' ',500);
for (int i = 0; i != paths.Length; ++i) {
if (paths.Length < shortestPath.Length;) {
shortestPath = paths;
}
}

There are two:

- No magic number, as you said;
- Doesn't allocate a string with length 500 only to destroy it later if a
shorter path is in fact found (and this is what happens; the runtime will
not "copy over" your preallocated string).

There are also obvious differences in how both pieces of code can be
optimized, but I'm not going to claim one will definitely perform better
than the other because I don't know the jitter that well. My gut tells me
my version is easier to optimize, but it's not impossible to keep
"shortestPath.Length" in a register, just not as easy. And gut feelings
are notoriously unreliable when it comes to performance, and we haven't
established that performance is even critical here, etcetera -- I'm just
mentioning this for completeness.

In any case, eliminating the magic number alone would be enough reason for
me. I hate MAX_PATH, and the next tool that says "Path too long" is going
to get punched in the face. Eliminating these constants from our code
probably won't help in that regard, but there's no reason to perpetuate
this either.

Thanks for the info. I have already inserted your code.
 
Arne Vajhøj said:
More like an exception.


Correct.

But I do not see the point.

You can store the length in an int or if you prefer
to use string length, then just start with the first
array element instead of creating a new string with
spaces.

Arne

Thanks, I'm all set now.
 
Michael Covington said:
Strings in C# do not have to be initialized to a particular length. Simply
do something like this:

string shortest = filenames[0];
foreach (string s in filenames)
if (s.Length() < shortest.Length())
shortest = s;

This is quick, simple code, but it looks at filenames[0] twice and once
compares it to itself. If that matters, you can do this, but I don't
guarantee it's faster:

string shortest = filenames[0];
for (int i=1; i<filenames.Count(); i++)
if (filenames.Length() < shortest.Length())
shortest = filenames;

In C#, strings are managed for you dynamically and you are actually
manipulating pointers, which are handled for you behind the scenes.


thanks a lot
 
Back
Top