class phoneNumber
{
/*Returns phoneNum after spliting*/
public string splitPhone(string phoneNum)
{
if(phoneNum.Length > 9 && phoneNum.Substring(0,1)=="1")
{
phoneNum = phoneNum.Substring(1,9);
}
return phoneNum;
}
/*returns phone prefix*/
public string phonePrefix(string phoneNum)
{
if(phoneNum.Length > 7)
{
phoneNum.Substring(0,3);
}
return phoneNum;
}
/*returns phone suffix*/
public string phoneSuffix(string phoneNum)
{
if(phoneNum.Length > 7)
{
phoneNum.Substring(3,7);
}
return phoneNum;
}
}
}
}
First method splitPhone splits phoneNumber,second method phonePrefix returns phonePrefix,third method phoneSuffix returns phoneSuffix. How to replace Substring function in this methods with take and skip. Want to write this same code using take,skip in c sharp. please let me know how this could be done

Aniket NarvankarPosted Dec 3, 2021, 1:56 PM
Sonil KumarPosted Dec 3, 2021, 11:53 AM
Please use the namespace using System.Linq; to get the extension method for linq on String.
class phoneNumber
{
/*Returns phoneNum after spliting*/
public string splitPhone(string phoneNum)
{
if (phoneNum.Length > 9 && phoneNum.Take(1).First().ToString() == "1")
{
return phoneNum.Skip(1).Take(9).ToString();
}
return phoneNum;
}
/*returns phone prefix*/
public string phonePrefix(string phoneNum)
{
if (phoneNum.Length > 7)
{
phoneNum.Take(3);
}
return phoneNum;
}
/*returns phone suffix*/
public string phoneSuffix(string phoneNum)
{
if (phoneNum.Length > 7)
{
var kk = phoneNum.Skip(3).Take(4);
}
return phoneNum;
}
}
Lakshna SPosted Dec 3, 2021, 10:37 AM
Take() - The Take() method extracts the first n elements (where n is a parameter to the method) from the beginning of the target sequence and returns a new sequence containing only the elements taken.
Skip() - The Skip() method can be thought of as the exact opposite of the Take() method. Where the Take() method, returns a sequence containing the first n elements of the target sequence, the Skip() method "skips" over the first n elements in the sequence and returns a new sequence containing the remaining elements after the first n elements.
To split phoneNumber use, phoneNum.split(1).take(9);
To split phonePrefix use, phoneNum.take(4);
To split phoneSuffix use, phoneNum.skip(3).take(7);