Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get just the first ten characters of a string in C# [duplicate]

Tags:

substring

c#

I have a string and I need to get just the first ten characters. Is there a way that I can do this easily.

I hope someone can show me.

like image 862
Hiromi Nagashima Avatar asked May 14 '11 14:05

Hiromi Nagashima


People also ask

How do you find the first 10 characters of a string?

To get the first 10 characters, use the substring() method. string res = str. Substring(0, 10);

How do I retrieve the first 5 characters from a string?

string str = yourStringVariable. Substring(0,5);

How do you find the first few letters of a string?

To get the first N characters of a string, we can also call the substring() method on the string, passing 0 and N as the first and second arguments respectively. For example, str. substring(0, 3) returns a new string containing the first 3 characters of str .


1 Answers

You can use the String.Substring method; e.g.:

string s = "Lots and lots of characters";
string firstTen = s.Substring(0, 10);
like image 68
Teoman Soygul Avatar answered Oct 26 '22 03:10

Teoman Soygul