CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Mar 2003
    Location
    Orange County, CA
    Posts
    99

    Question String functions

    I am used to programming primarily in C++, but I am currently working on a C# program. So I have a few questions regarding strings. Are there any equivalent C# functions that perform the same as the following C++ functions:

    //strspn checks string for any of the characters in strCharSet and returns the
    //number of occurances of the characters
    size_t strspn(const char *string, const char *strCharSet);

    //atof returns the double value of string
    double atof(const char *string);

    //isalnum checks to see if c is a letter or a digit
    int isalnum(int c);

    Any help will be greatly appreciated!





  2. #2
    Join Date
    Nov 2002
    Location
    Tatooine
    Posts
    155
    Hi,

    //strspn checks string for any of the characters in strCharSet and returns the
    //number of occurances of the characters
    size_t strspn(const char *string, const char *strCharSet);
    I don't know of a function like this, but the IndexOf method can process/find each occurance of a sub string.

    //atof returns the double value of string
    double atof(const char *string);
    You can try double.Parse(string) or Convert.ToInt32(string)

    //isalnum checks to see if c is a letter or a digit
    int isalnum(int c);
    There are some char methods that can test a character value like this; not sure if that will help char.IsDigit or char.IsLetter, etc....


    Hope this helps.
    That which does not kill us, only makes us stronger.

    MCSD .NET

  3. #3
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Exclamation just a small comment...

    You can try double.Parse(string) or Convert.ToInt32(string)
    Convert.ToDouble

  4. #4
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    Can i extend the String class.
    I want to add a few methods to my custom String class and get existing implementations from the String class.
    Regards,
    Preetham.
    "All you touch and all you see is all you'll ever be"

  5. #5
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    Originally posted by preetham
    Can i extend the String class.
    I want to add a few methods to my custom String class and get existing implementations from the String class.
    sure you can
    inherit the string class and add whatever methods you need.

  6. #6
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    i tried, it gave me a message saying String was a sealed class..
    Regards,
    Preetham.
    "All you touch and all you see is all you'll ever be"

  7. #7
    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    762
    Originally posted by preetham
    i tried, it gave me a message saying String was a sealed class..
    Post your code

  8. #8
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    using System;

    namespace System.CustomClasses
    {
    class CPPString : String
    {
    public CPPString()
    {
    }
    }

    class TestCustomString
    {
    public static void Main()
    {
    CPPString cs = new CPPString();
    }
    }

    }
    Regards,
    Preetham.
    "All you touch and all you see is all you'll ever be"

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    HSPC, you should check the docs before you say "sure you can"

    public sealed class String : IComparable, ICloneable,
    The String class is specifically designated to NOT be inherited from (for many a good reason).
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    HSPC, you should check the docs before you say "sure you can"
    sorry you are right TheCPUWizard..
    but he still can find a way around it...by making a class that contains a string as a provate member ..

    The String class is specifically designated to NOT be inherited from (for many a good reason).
    I'll be glad to know from you about these reasons

  11. #11
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    Originally posted by hspc
    sorry you are right TheCPUWizard..
    but he still can find a way around it...by making a class that contains a string as a provate member ..
    I did think of doing it the way u mentioned. However, how can i make the methods available to the private string, available to my class.


    I'll be glad to know from you about these reasons
    Me too.
    Regards,
    Preetham.
    "All you touch and all you see is all you'll ever be"

  12. #12
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    Originally posted by preetham
    how can i make the methods available to the private string, available to my class.
    private member variables are available to the containing class
    you'll need to expose the string's properties using public properties.

  13. #13
    Join Date
    Dec 2001
    Location
    Dallas, Tx, USA (originally fromIndia)
    Posts
    154
    must i write an method and property in my class for every method and property in the String class..even the overloaded ones.
    Is there another way.
    Regards,
    Preetham.
    "All you touch and all you see is all you'll ever be"

  14. #14
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    Originally posted by preetham
    must i write an method and property in my class for every method and property in the String class..even the overloaded ones.
    Is there another way.
    the easy and bad way is to make the string member variable public so you don't have to make many extra properties.

    then the caller can access the string directly :
    MyClass.MyInternalString.StringProperty

  15. #15
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125
    There were some good explainations on the "official dot-net boards" about this and other design issues.

    Boiling it down is the fact that strings are implemented as immutable objects. Once created their value can not and must not change. This was done for a number of reasons, but the two leading ones were performance and management.

    Now as soon as you let people derive from your classes it becomes very difficult to keep control of things. If you ever look at a reverse compile (or the MSIL) of the string class you will begi to see this. Rather than let people derive from the class and possibly (probably) do things it was not intended to do, it was marked as sealed.

    Remember that class inheritance is a specific topology. Every derived class needs to meet the "IS-A" test. If there are no configurable (translation virtual) metohds to the class, then it typically means the designer did not intend for it to be derived from. The careful developer will make this clear by marking the class as sealed.

    Conversely, when a class is not sealed, it can (should) mean that the developer intended it to be derived from. This implies that there is some general functionallity which can/should be extended for common usage.

    Hope this helps. Again the posts on the "other boards" went into way more detail....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured