How to return part of a string in C #?

I need to return a maximum of 9 digits of the next line abc. How to do it?

public string test()
{
  string abc="asdhfjsdfkjhfiovjalksdhafbvklxkjszjhd";
  return abc;
}
+4
source share
4 answers

Use String.Substring:

public string test()
{
  string abc="asdhfjsdfkjhfiovjalksdhafbvklxkjszjhd";
  return abc.Substring(0, 9);
}
+5
source
public string test()
{
  string abc="asdhfjsdfkjhfiovjalksdhafbvklxkjszjhd";
  return abc.Substring(0,9);
}
+5
source

You can use Substringas others answered

Besides

public string test()
{
  string abc = "asdhfjsdfkjhfiovjalksdhafbvklxkjszjhd";
        abc = new string(abc.Take(9).ToArray());
}
+4
source

http://www.dotnetperls.com/substring

you can use this:

public string test()
{
  string abc="asdhfjsdfkjhfiovjalksdhafbvklxkjszjhd";
  abc = abc.Substring(0, 9);
  return abc;
}
+3
source

All Articles