473,403 Members | 2,270 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,403 software developers and data experts.

string length

I have a string assigned a value from a querystring

public string a = request.querystring[1]

how can i make sure string a is less than 250 char? without using if's
aaron
Nov 16 '05 #1
9 2334
Unless you're willing to count the ternary operator as different from an
if, then I can't seem to think of one.

I'm thinking of something like this:

(a.length < 250) ? a : a.Substring(0,250)

I know it's basically an if, but you can put it inside a conditional or
any other statement, if that's why you need to avoid ifs.

The other string functions seem to be too exception happy to do anything
to creative with. Same with StringBuilder.

Dave
Nov 16 '05 #2
public string a = Request.QueryString[2].Substring(0, 250);
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

"Aaron" <ku*****@yahoo.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
I have a string assigned a value from a querystring

public string a = request.querystring[1]

how can i make sure string a is less than 250 char? without using if's
aaron

Nov 16 '05 #3
Klaus H. Probst wrote:
public string a = Request.QueryString[2].Substring(0, 250);


.... if you're willing to trap the ArgumentOutOfRangeException for
strings that are under 250 characters long.
Nov 16 '05 #4

"Aaron" <ku*****@yahoo.com> wrote in message news:e1**************@TK2MSFTNGP10.phx.gbl...
I have a string assigned a value from a querystring

public string a = request.querystring[1]

how can i make sure string a is less than 250 char? without using if's
aaron


and in addition to what the others said, you can also limit the input:
make sure the textbox (if that is where the text comes from)
accepts no more than 250 chars. (MaxLength property)

Hans Kesting
Nov 16 '05 #5
I would use:
int lengthOrTruncated = Math.Min(Request.QueryString[1].Length, 250);
string a = Request.QueryString[1].Substring(0, lengthOrTruncated);

(or inline the temporary int variable).

Yanick

Aaron wrote:
I have a string assigned a value from a querystring

public string a = request.querystring[1]

how can i make sure string a is less than 250 char? without using if's
aaron

Nov 16 '05 #6
Then how about Substring(0,min(250,length))

"D Cameron" <da******@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Klaus H. Probst wrote:
public string a = Request.QueryString[2].Substring(0, 250);


... if you're willing to trap the ArgumentOutOfRangeException for
strings that are under 250 characters long.

Nov 16 '05 #7
> how can i make sure string a is less than 250 char? without using if's

With a lot of work, by instance through looping thru the string and add
everytime a char untill it reaches his end,

Comparing is older in computing than multiplying, what do you think the
computer does internally when you are not using that?

Just my thought,

Cor

Nov 16 '05 #8
> and in addition to what the others said, you can also limit the input:
make sure the textbox (if that is where the text comes from)
accepts no more than 250 chars. (MaxLength property)


Being a paranoid developer, I don't know that I would leave it at just that,
though. Setting the maxlength would cover the expected scenario (where a
user is typing into an edit box), but it would leave you open to accepting
strings longer than 250 if someone just constructs a query string and posts
it to a URL.

Now, in the world of unmanaged code, if your server-side code just trusts
the string to be shorter than 250, and someone sneaks a larger one in by
building and posting their own query string, your code may be open to a
buffer overrun attack.

In the managed world, as long as you are using safe code, I don't _think_
that you can get a real buffer overrun. However, it still seems kind of
dangerous to take for granted that a string passed from an external source
will always conform to your size rule. I'd be suspicious and check it.
Nov 16 '05 #9

"J.Marsch" <je****@ctcdeveloper.com> wrote in message news:O7**************@TK2MSFTNGP11.phx.gbl...
and in addition to what the others said, you can also limit the input:
make sure the textbox (if that is where the text comes from)
accepts no more than 250 chars. (MaxLength property)


Being a paranoid developer, I don't know that I would leave it at just that,
though. Setting the maxlength would cover the expected scenario (where a
user is typing into an edit box), but it would leave you open to accepting
strings longer than 250 if someone just constructs a query string and posts
it to a URL.

Now, in the world of unmanaged code, if your server-side code just trusts
the string to be shorter than 250, and someone sneaks a larger one in by
building and posting their own query string, your code may be open to a
buffer overrun attack.

In the managed world, as long as you are using safe code, I don't _think_
that you can get a real buffer overrun. However, it still seems kind of
dangerous to take for granted that a string passed from an external source
will always conform to your size rule. I'd be suspicious and check it.


true! As with validators, you should do both: client-side for quick feedback
and server-side for security.

Hans Kesting
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
2
by: José Joye | last post by:
Hello, I was wondering if there is a method that exists to replace multi-spaces within a string with single-space. eg: "12 3 4 56" --> "12 3 4 56" I think this could be done by...
20
by: Chris LaJoie | last post by:
I'm looking for some kind of simple string compression code I can use. I'm not looking for SharpZipLib. Their implimentation spans several classes and is very complex. I'm just looking for...
6
by: Webgour | last post by:
How to go from string "abc" to string "a|b|c"?
8
by: Jami Bradley | last post by:
Hi, I'm looking for an efficient way to do this, because I know it will be heavily used :-) I have a fixed width string and I need to substitute a substring of characters with new values. I...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
7
by: Eric | last post by:
Hi All, I need to XOR two same-length Strings against each other. I'm assuming that, in order to do so, I'll need to convert each String to a BitArray. Thus, my question is this: is there an...
23
by: comp.lang.tcl | last post by:
I have a TCL proc that needs to convert what might be a list into a string to read consider this: ]; # OUTPUTS Hello World which is fine for PHP ]; # OUTPUT {{-Hello}} World, which PHP...
10
by: Mo | last post by:
Hi, I am trying to write a code to build a string 768 characters long. This string is going to be written to a file which is then read by another application. The format of the string is already...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
agi2029
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,...

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.