A string class that don't stretch

  • Thread starter Thread starter craigkenisston
  • Start date Start date
C

craigkenisston

I need an special string that have a fixed capacity and that
automatically(silently) trims extra space.

MySpecialString str = new MySpecialString(10);

str = "1234567890ABCDE"

Console.WriteLine(MySpecialString.ToString())
// output "1234567890" i.e. trimes the "ABCDE"

The stringbuilder has the capability of specify a capacity, but this
does nothing just to set an initial size.
If I put a string larger than its capacity it just grows.

So, is there any special class that does this?

If there isn't. Can I inherit from string and create a class that does
this?

Thanks in advance,
 
Let's see...

You can't inherit the String class because it's sealed but you can use
composition to enhace it.

This shouldn't be hard at all...this might be a good starting point:

class FixedString
{
private int m_iSize;
private String m_sInternal;

public FixedString(int size)
{
m_iSize = size;
}

public FixedString(String str)
{
m_sInternal = str;
m_iSize = str.Length;
}

public FixedString(String str, int size)
{
m_sInternal = str;
m_iSize = size;
}

public override String ToString()
{
if (m_iSize == 0)
return m_sInternal;

return m_sInternal.Substring(0, m_iSize);
}
public int Size
{
get { return m_iSize; }
set { m_iSize = value; }
}

}

Usage: FixedString myFixedString = new FixedString("ABCDEF");

Console.WriteLine( myFixedString.ToString() ); //just outputs the
full string

myFixedString.Size = 3;

Console.WriteLine( myFixedString.ToString() ); //outputs "ABC"

OR

FixedString myFixedString = new FixedString("ABCDEF", 4); //This adds
the string AND sets the size in one easy and convenient step
Console.WriteLine( myFixedString.ToString() ); //outputs "ABCD"


Depending on specifically what you want this may or may not work but
you get the idea. And there's always more than one way to skin a cat
so this way of doing it is just a start...I'm sure there's better or
more elegant solutions.

Good luck!

-Ralph
 
Please note that the String class is Immutable. So if you have a variable
defined as one string, and you change the string, it actually creates a
whole new string object.

Also, with the StringBuilder, you can specify the starting size.

Robin S.
---------------------------------------
 
Hi,


|
| If there isn't. Can I inherit from string and create a class that does
| this?

Well both string and StringBuilder are sealed so you cannot inherit from
them. you could always create your own class and just use a StringBuilder as
the storage.
 
May be I should tell my problem rather than my lame solution-wanna-be.

I have an application for which I don't have control on the user
input. It is data from the web, from which I have no control, there's
no "form" to input this data, but it is received as parameters.
I made the database with enough space, but since I can't control it
some time, the string overflows the column width and causes a database
error, as it should be.
(Please, don't ask me to change a bit on the database, I want a
solution on the application).

Is there an automated way to control this? May be there's an option
somewhere in the ado.net control that I don't know.
I just don't want to write :
mystring = mystring.substring(0, Math.Min(mystring.lengh,
the_column_size_in_the_table);

for every string, every where.

If I go to the custom class, which I had done already and btw, also
called it "fixedstring", I'd have to re-write the logic on string
assignments everywhere, too ! :(
 
Sounds like some weird requirements :)

Perhaps you can just extend whatever ado.net control that you are
using and capture the string data at a certain point, truncate it as
needed....then let it submit to the database.

This way you'd only be enhancing that control in one place and you are
done.

-Ralph
 
Hi,

| May be I should tell my problem rather than my lame solution-wanna-be.
|
| I have an application for which I don't have control on the user
| input. It is data from the web, from which I have no control, there's
| no "form" to input this data, but it is received as parameters.
| I made the database with enough space, but since I can't control it
| some time, the string overflows the column width and causes a database
| error, as it should be.

That is super common I think,
just check for the size of the column

if ( varString.Length > 50 )
myCommand.Parameters["StringColumn"] = varString.Substring( 0, 50);
else
myCommand.Parameters["StringColumn"] = varString;



| (Please, don't ask me to change a bit on the database, I want a
| solution on the application).

Not at all :)


With the above code you only have to modify one piece of code :)
 
May be I should tell my problem rather than my lame solution-wanna-be.

I have an application for which I don't have control on the user
input. It is data from the web, from which I have no control, there's
no "form" to input this data, but it is received as parameters.
I made the database with enough space, but since I can't control it
some time, the string overflows the column width and causes a database
error, as it should be.
(Please, don't ask me to change a bit on the database, I want a
solution on the application).

Is there an automated way to control this? May be there's an option
somewhere in the ado.net control that I don't know.
I just don't want to write :
mystring = mystring.substring(0, Math.Min(mystring.lengh,
the_column_size_in_the_table);

for every string, every where.

If I go to the custom class, which I had done already and btw, also
called it "fixedstring", I'd have to re-write the logic on string
assignments everywhere, too ! :(

Of course there is ;-). The dataset you are putting in has definition
of the column length it requires:

use these properties for your automation:

ds.Tables[0].Columns[stringColumnNamehere].MaxLength
ds.Tables[0].Columns[stringColumnNamehere].DataType

Here's a sample code:

string SourceString = "some value you read in"; //set for testing
purpose
string OutputString; //Need to be here for testing purpose
//Set length of string requirements
// only need to do this once
// Not sure why dataset doesn't have this after read in from table. I
think using
// FillSchema might do the trick (recalled from memory)
acDs.Tables[0].Columns[2].MaxLength = 100;
for (int i=0; i<acDs.Tables[0].Columns.Count; i++)
{
DataColumn col = acDs.Tables[0].Columns;
switch (col.DataType.FullName)
{
case "System.Int32":
case "System.Decimal":
//specific doe to handle numbers, if any
break;
case "System.DateTime":
//specific code to handles datetime, if any
break;
case "System.String":
if (col.MaxLength > 0)
OutputString = SourceString.Substring(0,
col.MaxLength <
SourceString.Length ? col.MaxLength :

SourceString.Length);
else
{
//Either throw an exception or don't take length into
consideration
OutputString = SourceString;
//throw new ArgumentException("String column length is not
defined", col.ColumnName);
}
break;
}
}

HTH,
Quoc Linh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top