How to delimit a string field in c#

Asked

Viewed 4,914 times

5

I have a string field for example address in the code. This address field receives a 60 character data.

What I want is to delimit the address size so that it only receives 30 characters and not above, even if it truncates the information.

It is possible?

  • 3

    What are you using? Windows Forms, WPF, etc?

  • try searching for Maxleng

  • I’m using Windowsforms with c#

  • This varies if you are using Winforms, WPF or MVC, for example. It also varies whether you intend to cut out a piece of information or tell the user which character limit has popped.

  • In reality I am generating a txt file where the columns will be fixed. So if I delimit the field, whatever size comes from the BD, it will load the data even truncated, since the columns have to be fixed.

2 answers

5

You probably want something that truncates the message.

const int MaxTamanhoEndereco = 30;

var endereco = "Rua dos Bobos, número Zero";
if (endereco .Length > MaxTamanhoEndereco )
    endereco = endereco .Substring(0, MaxTamanhoEndereco ); 
  • Rodrigo, can I work with the constant as if it were a variable in the course of the code? type const int testeID = 1; string testeID =Convert.Tostring(item.Cells["ID"].Value.Tostring());

  • To use the constant as a variable, it cannot be constant, but you can use a variable to limit the maximum length of the string.

  • I don’t understand... sorry..

  • When you declare something as constant, you cannot change the value anymore and so it is not possible to use as variable. In the example you passed, creates two variables with the same name, would also give problem.

  • then the only way to delimit the variable for example address is with const, that is, I want this variable string address only to receive 20 characters by the end of the processing? Or rather, I want it to be a fixed size of 20 by the end of processing.

  • no, you can use a variable... just don’t use the const on the front.

Show 1 more comment

3

You can do this in several ways, I will list them in the order of the most recommended for less recommendable:

1. Delimiting the maximum Textbox size

You can change the property MaxLength of the component TextBox to receive at most 30 characters.

2. Let the user type everything and let him know that he has crossed the limit

In this case, you just need to do a normal validation using the property .Length of string. Ex.:

if(textBox.Text.Length > 30)
{
    MessageBox.Show("Campo endereço estourou o limite de caracteres");
    return;
}

3. Let the user type all and cut a piece of information

I wouldn’t do that, no way. It’s bad practice.

string endereco = textBox.Text.Length > 30 ? textBox.Text.Substring(0, 30) : textBox.Text;


Edit:

I just misunderstood your question. If you just want to limit a field that comes from some data source to 30 characters, the correct is to use the method Substring() of string. This method takes two parameters of type int. The first defines which the position of the first character to be "cropped" (remember that the positions start from scratch), the second parameter is the amount of characters to be "cut out".

Take an example:

var enderecoCompleto = db.BuscarEnderecoCliente(); //endereço vindo do banco

string endereco = enderecoCompleto.Length > 30 
                      ? enderecoCompleto.Substring(0, 30) //comece no caracter 0 e pegue 30 caracteres
                      : enderecoDoBanco;
  • So jbueno, but the data comes directly from the Database through a query...

  • Ah, I hadn’t read the whole question, my fault. Do you have a problem using the last option?

  • I’ll edit the answer to fit your question better, I’ll be back in a few minutes.

  • in reality what I want is to leave fixed the field. it seems to me that leaving as Rodrigo reported would be the ideal... "const int Maxtamanhoendereco = 30";

  • then the only way to delimit the variable for example address is with const, that is, I want this variable string address only to receive 20 characters by the end of the processing?.

  • @Joeliasandrade I edited the answer.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.