By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,611 Members | 1,642 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,611 IT Pros & Developers. It's quick & easy.

String Manipulation - Substring, VB function Left, "length safe" Substring

P: n/a
What is a good one line method for doing a "length safe"
String.Substring?

The VB classes offer up the old Left function so that
string s = Microsoft.VisualBasic.Left("kelly",200)
// s will = "kelly" with no error
// but
string s2 = "kelly".Substring(0,200)
// results in
// ArgumentOutOfRangeException
// Index and length must refer to a location within the string.

What is the most compact (one-line) method of doing the same thing with
Substring?
Maybe using Substring and Length and some other method...

Maybe I am just missing something.

Strange that the SUBSTRING in T-SQL is "length safe"...

Is the code below the best way of getting the same effect without the
VB Left?
//seems like a hack
string s = "this is some string"
string safeStr = s.PadRight(200).Substring(0,200).Trim()

Thanks in advance,
Kelly Greer
ke*********@nospam.com
change nospam to yahoo

Oct 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Kelly,
you could write a one line function and then use it anywhere in the project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}
"kellygreer1" wrote:
What is a good one line method for doing a "length safe"
String.Substring?

The VB classes offer up the old Left function so that
string s = Microsoft.VisualBasic.Left("kelly",200)
// s will = "kelly" with no error
// but
string s2 = "kelly".Substring(0,200)
// results in
// ArgumentOutOfRangeException
// Index and length must refer to a location within the string.

What is the most compact (one-line) method of doing the same thing with
Substring?
Maybe using Substring and Length and some other method...

Maybe I am just missing something.

Strange that the SUBSTRING in T-SQL is "length safe"...

Is the code below the best way of getting the same effect without the
VB Left?
//seems like a hack
string s = "this is some string"
string safeStr = s.PadRight(200).Substring(0,200).Trim()

Thanks in advance,
Kelly Greer
ke*********@nospam.com
change nospam to yahoo

Oct 28 '06 #2

P: n/a
Sergey Poberezovskiy <Se*****************@discussions.microsoft.com>
wrote:
Kelly,
you could write a one line function and then use it anywhere in the project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}
Or you could split the same thing into multiple lines to make it easier
to read :)

public static string Left(string value, int length)
{
if (value==null)
{
return null;
}

if (length >= value.Length)
{
return value;
}

return value.Substring (0, length);
}

I might use a conditional operator for the last part, but having two
conditional operators in the same statement almost never helps
readability.

--
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
Oct 29 '06 #3

P: n/a
Jon,

The original request was to have one-line expression. I would personally
write it differently (though not your way either - I always have one return
statement in a function) :-)

"Jon Skeet [C# MVP]" wrote:
Sergey Poberezovskiy <Se*****************@discussions.microsoft.com>
wrote:
Kelly,
you could write a one line function and then use it anywhere in the project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}

Or you could split the same thing into multiple lines to make it easier
to read :)

public static string Left(string value, int length)
{
if (value==null)
{
return null;
}

if (length >= value.Length)
{
return value;
}

return value.Substring (0, length);
}

I might use a conditional operator for the last part, but having two
conditional operators in the same statement almost never helps
readability.

--
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
Oct 29 '06 #4

P: n/a
Hi Sergey,

I think the original request was to have a one-line, in-line expression. I
doubt the OP cares if you use more than one line after encapsulation.

--
Dave Sexton

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:2D**********************************@microsof t.com...
Jon,

The original request was to have one-line expression. I would personally
write it differently (though not your way either - I always have one return
statement in a function) :-)

"Jon Skeet [C# MVP]" wrote:
>Sergey Poberezovskiy <Se*****************@discussions.microsoft.com>
wrote:
Kelly,
you could write a one line function and then use it anywhere in the
project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}

Or you could split the same thing into multiple lines to make it easier
to read :)

public static string Left(string value, int length)
{
if (value==null)
{
return null;
}

if (length >= value.Length)
{
return value;
}

return value.Substring (0, length);
}

I might use a conditional operator for the last part, but having two
conditional operators in the same statement almost never helps
readability.

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

Oct 29 '06 #5

P: n/a
Dave,
I just wrote an error proof function. One lin in-line implementation would
be if you remove check for null - and this will also be in line with VB Left
function :-)
"Dave Sexton" wrote:
Hi Sergey,

I think the original request was to have a one-line, in-line expression. I
doubt the OP cares if you use more than one line after encapsulation.

--
Dave Sexton

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:2D**********************************@microsof t.com...
Jon,

The original request was to have one-line expression. I would personally
write it differently (though not your way either - I always have one return
statement in a function) :-)

"Jon Skeet [C# MVP]" wrote:
Sergey Poberezovskiy <Se*****************@discussions.microsoft.com>
wrote:
Kelly,
you could write a one line function and then use it anywhere in the
project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}

Or you could split the same thing into multiple lines to make it easier
to read :)

public static string Left(string value, int length)
{
if (value==null)
{
return null;
}

if (length >= value.Length)
{
return value;
}

return value.Substring (0, length);
}

I might use a conditional operator for the last part, but having two
conditional operators in the same statement almost never helps
readability.

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


Oct 29 '06 #6

P: n/a
Hi Sergey,
The original request was to have one-line expression.
Your example and Jon's rewritten version, meant to increase readability, both
provide a one-line expression to the OP via encapsulation. I believe the OP
explicitly asked for one-line because of an assumption that responses would
all consist of many lines if one-line wasn't explicitly stated, without giving
any thought to writing a utility method. I could be wrong.

--
Dave Sexton

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.comwrot e
in message news:43**********************************@microsof t.com...
Dave,
I just wrote an error proof function. One lin in-line implementation would
be if you remove check for null - and this will also be in line with VB Left
function :-)
"Dave Sexton" wrote:
>Hi Sergey,

I think the original request was to have a one-line, in-line expression. I
doubt the OP cares if you use more than one line after encapsulation.

--
Dave Sexton

"Sergey Poberezovskiy" <Se*****************@discussions.microsoft.com>
wrote
in message news:2D**********************************@microsof t.com...
Jon,

The original request was to have one-line expression. I would personally
write it differently (though not your way either - I always have one
return
statement in a function) :-)

"Jon Skeet [C# MVP]" wrote:

Sergey Poberezovskiy <Se*****************@discussions.microsoft.com>
wrote:
Kelly,
you could write a one line function and then use it anywhere in the
project:
public string Left(string value, int length)
{
return (value == null) ? null : (value.Length <= length) ? value :
value.Substring(0, length);
}

Or you could split the same thing into multiple lines to make it easier
to read :)

public static string Left(string value, int length)
{
if (value==null)
{
return null;
}

if (length >= value.Length)
{
return value;
}

return value.Substring (0, length);
}

I might use a conditional operator for the last part, but having two
conditional operators in the same statement almost never helps
readability.

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



Oct 29 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.