472,972 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,972 software developers and data experts.

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

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
6 9203
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: /dev/null | last post by:
Is there a function in php that you can pass a string headed for a mysql query and have it make it 'safe'? For example it would escape out the ' found in the string. Thanks!
1
by: Xah Lee | last post by:
# strings can be joined by +. print "this" + " that" # string can be multiplied print "this" *5 # substring extraction is done by appending a bracket # with begin and ending index a="this...
0
by: KENNY L. CHEN | last post by:
I have a table with a CHAR column which stored a COBOL redefined record. I tried to retrieve part of the field from the column with Oracle substr function. Here is one of example: The...
18
by: Aaron | last post by:
I have a string assigned a value from a querystring public string a = request.querystring how can i make sure string a is less than 250 char? without using if's aaron
8
by: Doug Laidlaw | last post by:
I tried to grab an image from a Web page the other day. It turned out that the page was made up of three horizontal bands, and part of the image was in each. One band was a JPEG, another was a...
3
by: Sam | last post by:
I want to divide character into 2 section. Example, 200412 divided into 2004 in A block and 12 in B block. Please advise how to use left(string, length) or right(string, length) for above...
2
by: Tom | last post by:
I'm getting this error when I try to pass a structure to a dll. An unhandled exception of type 'System.ArgumentException' occured in Test1.exe Additional Information: Type could not be marshaled...
4
by: Trapulo | last post by:
I've a webservice with a string parameter. I call this webservice passing a string that is 1129 chars length. On the webservice, the received string is 1146 length (I tested sizes with...
4
by: Kozman | last post by:
I have a problem where I need to use the literal "length" as a subscript in an associative array (I have no control over what is used as a subscript..."length" happens to be one of the uncontrolled...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.