473,405 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 9239
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.