How to cut a string?

3

Example:

nome = "Josénildo da Silva Ramos do Carmo";

Cut to have up to X characters, thus:

nome = "Josénildo da Silva ";

In the case I cut it to 20 characters.

How do I do this in C #? I only know in C (which is vector). In C # I could not find how.

    
asked by anonymous 14.07.2015 / 01:35

2 answers

10

It's quite simple:

nome = nome.Substring(0, 20);

In this case you are picking up 20 characters from the character at position 0.

Method documentation . From here you can see in the documentation all methods that type String allows. It is good to give a decoration in everything to know what already exists.

See running on dotNetFiddle .

    
14.07.2015 / 01:38
6

Use the Substring method.

var nome = "Josénildo da Silva Ramos do Carmo";
var novoNome = nome.Substring(0, 20);

The first parameter indicates the initial index and the second is the character count that must be cut, ie the above method will pick up from position 0 to 20 of the string.

    
14.07.2015 / 01:38