Results 1 to 2 of 2

Thread: Strings are CONFUSING ME

  1. #1

    Thread Starter
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134

    Strings are CONFUSING ME

    I know I can have char, but then there is string.h with the string function in it... all I want to know is how to do the equivalents of Mid$(), Left$(), Len(), and Right$() with C++. I've got a really old book and it doesnt really cover the basics.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  2. #2
    denniswrenn
    Guest
    if you use the string class located in string, there is a substr function.

    Code:
    #include <string>
    using namespace std;
    //substr(start, depth)
    int main()
    {
    string str = "hello";
                     // 0 1 2 3 4 
                     // h e  l  l  o
    string s = str.substr(1, 2);  //starts at position 1 (e) and goes three spaces.
    //s contains ell
    
    return(0);
    }
    There is also the length function belonging to the string class.

    Code:
    string str = "hello";
    int len = str.length(); //len contains 5
    If you are talking about using char* then you can get the length by using strlen() in string.h(cstring is the standard library name). As we all know you can access an individual character in an array of characters by using [ and ] (ex: pcBuf[5] returns the 5th element of the variable). From there you can make your own left, mid, and right functions, with almost no effort.

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