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

K

kellygreer1

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
(e-mail address removed)
change nospam to yahoo
 
G

Guest

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);
}
 
J

Jon Skeet [C# MVP]

Sergey Poberezovskiy said:
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.
 
G

Guest

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) :)
 
D

Dave Sexton

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

Guest

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 :)
 
D

Dave Sexton

Hi Sergey,

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.
 

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