String Extractors And Manipulators – The "Mid" Function (Part – III)
Although OOPS is the most powerful techniqueto manipulate data in a secure manner, the need to apply this powerwith caution arises due to the way classes are used or are going to beused by other programmers. The string class in Java takes input throughobjects of its type only. But in the above class, if the class were toreceive the string through the object and not through the method, thenthe design of the class becomes suspect as abstraction demands hidingunnecessary details. And by passing data through the object to theabove class, the programmer who uses the above class for the "mid"method, may need to know everything about the class which is burdeningthe programmer needlessly with unnecessary details.
As in other languages, this "mid" functionalso takes three parameters and returns an object. So does this "mid"method in C# which uses the indexer to strip the string into "chars"and returns the resultant string into an objec t of string class.Because of the indexer in C#, string extraction and manipulationbecomes easier than writing a "for" loop.
The code for the "mid" function is given under
using System;
public class a{
public string s1;
public string mid(int offset,int numcharstoread,string s){
int j=0;
for (i=offset ; i < numcharstoread ; i++){
char ch=s[i];
s1[j]=ch;
j++;
}
return s1;}
}
class b{
public static void Main(){
a a1=new a();
string s2;
s2=a.mid(1,2,"c-sharp");
Console.Write("{0}",s2);
}
}












No comments yet... Be the first to leave a reply!