What is a good one line method for doing a "length safe"
String.Substrin g?
The VB classes offer up the old Left function so that
string s = Microsoft.Visua lBasic.Left("ke lly",200)
// s will = "kelly" with no error
// but
string s2 = "kelly".Substri ng(0,200)
// results in
// ArgumentOutOfRa ngeException
// 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,20 0).Trim()
Thanks in advance,
Kelly Greer ke*********@nos pam.com
change nospam to yahoo 6 9256
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);
}
"kellygreer 1" wrote:
What is a good one line method for doing a "length safe"
String.Substrin g?
The VB classes offer up the old Left function so that
string s = Microsoft.Visua lBasic.Left("ke lly",200)
// s will = "kelly" with no error
// but
string s2 = "kelly".Substri ng(0,200)
// results in
// ArgumentOutOfRa ngeException
// 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,20 0).Trim()
Thanks in advance,
Kelly Greer ke*********@nos pam.com
change nospam to yahoo
Sergey Poberezovskiy <Se************ *****@discussio ns.microsoft.co m>
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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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************ *****@discussio ns.microsoft.co m>
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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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************ *****@discussio ns.microsoft.co mwrote
in message news:2D******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co m> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
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************ *****@discussio ns.microsoft.co mwrote
in message news:2D******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co m>
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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
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************ *****@discussio ns.microsoft.co mwrote
in message news:43******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co m> wrote in message news:2D******** *************** ***********@mic rosoft.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************ *****@discussio ns.microsoft.co m> 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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet If replying to the group, please do not mail me too
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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!
|
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 and that"
print a
|
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 SQL/result are shown below:
SQL:
select
length(substr(MY_COLUMN,36,8)) str_length,
dump(substr(MY_COLUMN,36,8)) INFO,
|
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
|
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 GIF and I have forgotten what
the main page was.
Apart from lining up the parts of the image, there was no discrepancy in
colours between the two formats. Is that what "Web-safe" means? I thought
that it just meant that the colours would be the...
| |
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 request.
Many thanks in advance.
|
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 because the length
of an embedded array instance does not match the declared length in the
layout
What does it mean?
|
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
string.length property). String's contents seem be teh same, so I think
there is some encoding difference (is this possibile?). The problem is that
I sign this string, so signature validation fails :(
What can be the problem?
|
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
values). The problem is that if I assign it to something other than an
integer, it complains and throws an exception:
MyArr = new SomeObject();
I understand the importance of the length property in ordered
lists...but it has no usage in...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |