How to cut a string?

Asked

Viewed 5,849 times

3

Example:

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

Cut to have up to X characters, and look like this:

nome = "Josénildo da Silva ";

In this case I cut 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.

2 answers

11


  • Thank you both, I couldn’t imagine it was that simple as rsrs (accustomed to C who is a terror kk). You responded a little faster than @Caique C. (according to the website here) so it’s the right answer.

  • Yeah, C doesn’t have the type string then you have to do everything in hand, in C# most of the operations are ready in type.

7

Use the method Substring.

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 count of characters that must be cut, that is, the above method will pick from position 0 until 20 of the string.

Browser other questions tagged

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