473,237 Members | 1,200 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,237 software developers and data experts.

A string class that don't stretch

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,

Feb 6 '07 #1
7 2672
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
On Feb 5, 7:07 pm, craigkeniss...@hotmail.com wrote:
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,

Feb 6 '07 #2
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.
---------------------------------------
"Deckarep" <de******@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
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
On Feb 5, 7:07 pm, craigkeniss...@hotmail.com wrote:
>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,


Feb 6 '07 #3
Hi,

<cr************@hotmail.comwrote in message
news:11**********************@s48g2000cws.googlegr oups.com...

|
| 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.
--
Ignacio Machin
machin AT laceupsolutions com
Feb 6 '07 #4
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 ! :(

Feb 6 '07 #5
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

On Feb 6, 2:56 pm, craigkeniss...@hotmail.com wrote:
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 ! :(

Feb 6 '07 #6
Hi,

<cr************@hotmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com...
| 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 :)
--
Ignacio Machin
machin AT laceupsolutions com
Feb 7 '07 #7
On Feb 6, 2:56 pm, craigkeniss...@hotmail.com wrote:
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[i];
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

Feb 7 '07 #8

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

Similar topics

19
by: David Logan | last post by:
We need an additional function in the String class. We need the ability to suppress empty fields, so that we can more effectively parse. Right now, multiple whitespace characters create multiple...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ 3.5. - How do I convert a Number into a String with exactly 2 decimal places?...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
by: WP | last post by:
Hello! I need some help with my program...it's supposed to read infix expressions line by line from stdin and each expression should be divided into operands and operators and added to a vector of...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.