473,387 Members | 1,504 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,387 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 2333
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
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,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.