String.Trim(); 
Author Message
 String.Trim();

Hi,
I'm writing string.Trim() custom version for fun;
Here's the code of the source, It gives me Runtime error.

public static string Trim(string str_Ln)
{
  string hs_Res = null;
  string str = null;
  for ( int i = 0; i < str_Ln.Length; i++ )
  {
    string fm = str_Ln[i].ToString();
    if ( Compare(fm, " ") == true )
    {
    str = str_Ln.Substring(i, str_Ln.Length - i);
    char[] ch = str.ToCharArray();
    for ( int x = 0; x < ch.Length; x++ )
    {
      ch[x] = ' ';
    }
    str = new string(ch);
   if ( Compare(str, str_Ln.Substring(i, str_Ln.Length -
i)) == true )
   {
   for ( int k = 0; k < i; k++ )
   {
   hs_Res += str_Ln[k].ToString();
   }
   Console.WriteLine(str_Ln.Substring(0,i));
   return hs_Res;
   }
   else
   {
     return null;
   }
  }
 }
return null;

Quote:
}

Here's the Compare Method;

public static bool Compare(string frstStr, string scndStr)
{
char[] frst = frstStr.ToCharArray();
char[] scnd = scndStr.ToCharArray();
bool cRes = false;
for ( int i = 0; i < frst.Length; i++ )
{
for ( int k = 0; k < scnd.Length; k++ )
{
if ( i == k && (int)frst[i] == (int)scnd[k] )
{
cRes = true;

Quote:
}

else
{
cRes = false;

Quote:
}
}
}

Both methods are in Custom.Cstring namespace;
Any help will be appriciated in advance;
Thanks.

Dave,



Mon, 12 Jul 2004 06:32:53 GMT  
 String.Trim();
What line is the error on?

--
Jonathan Allen

.Net Wish List Site: http://jonathan.cloverlink.com/


Quote:
> Hi,
> I'm writing string.Trim() custom version for fun;
> Here's the code of the source, It gives me Runtime error.

> public static string Trim(string str_Ln)
> {
>   string hs_Res = null;
>   string str = null;
>   for ( int i = 0; i < str_Ln.Length; i++ )
>   {
>     string fm = str_Ln[i].ToString();
>     if ( Compare(fm, " ") == true )
>     {
>     str = str_Ln.Substring(i, str_Ln.Length - i);
>     char[] ch = str.ToCharArray();
>     for ( int x = 0; x < ch.Length; x++ )
>     {
>       ch[x] = ' ';
>     }
>     str = new string(ch);
>    if ( Compare(str, str_Ln.Substring(i, str_Ln.Length -
> i)) == true )
>    {
>    for ( int k = 0; k < i; k++ )
>    {
>    hs_Res += str_Ln[k].ToString();
>    }
>    Console.WriteLine(str_Ln.Substring(0,i));
>    return hs_Res;
>    }
>    else
>    {
>      return null;
>    }
>   }
>  }
> return null;
> }

> Here's the Compare Method;

> public static bool Compare(string frstStr, string scndStr)
> {
> char[] frst = frstStr.ToCharArray();
> char[] scnd = scndStr.ToCharArray();
> bool cRes = false;
> for ( int i = 0; i < frst.Length; i++ )
> {
> for ( int k = 0; k < scnd.Length; k++ )
> {
> if ( i == k && (int)frst[i] == (int)scnd[k] )
> {
> cRes = true;
> }
> else
> {
> cRes = false;
> }
> }
> }

> Both methods are in Custom.Cstring namespace;
> Any help will be appriciated in advance;
> Thanks.

> Dave,



Mon, 12 Jul 2004 08:43:39 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. String.Trim() behavior

2. String.Trim is this correct functionality

3. String Trim function

4. Trim a comma off end of string???

5. Help to trim spaces of a string

6. trim the string

7. Need help to trim spaces off a string

8. trimming a string

9. How to trim a string in a _variant_t

10. How to trim a String?

11. Right trim string function in C

12. trim for strings

 

 
Powered by phpBB® Forum Software