CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Real newby question - string manipulation

  1. #1
    Join Date
    Dec 2008
    Posts
    3

    Real newby question - string manipulation

    Hey,
    Thats a real basic one...
    I have a char*, and i wishes to cut it.
    example:
    i want a function to do: f("ha blah shmil")->"blah shmil"

    Is there a good short way to do so?

  2. #2
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Real newby question - string manipulation

    why you don't use C++ like String and substr() function?

    Code:
    std::string str;
      std::string str2 = "ha blah shmil";
      str = str2.substr(3,10);
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  3. #3
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Real newby question - string manipulation

    another way:

    Code:
    #include <cstdlib>
    #include <string>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
    	string str = "ha blah shmil";
    	const char *p = str.substr(3).c_str(); // cut from b to end
    	cout << p << endl;
    	
    	 //system("PAUSE");
    	return 0;
    }
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  4. #4
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Real newby question - string manipulation

    if you want to spilit your string (e.g. by space) you can use this code:

    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    using namespace std;
    
    int main ()
    {
      char * cstr, *p;
    
      string str ("ha blah shmil");
    
      cstr = new char [str.size()+1];
      strcpy (cstr, str.c_str());
    
      // cstr now contains a c-string copy of str
    
      p=strtok (cstr," ");
      while (p!=NULL)
      {
        cout << p << endl;
        p=strtok(NULL," ");
      }
    
      delete[] cstr;  
      return 0;
    }
    please mention that string::c_str Generates a null-terminated sequence of characters (c-string) with the same content as the string object and returns it as a pointer to an array of characters.

    A terminating null character is automatically appended.

    The returned array points to an internal location with the required storage space for this sequence of characters plus its terminating null-character, but the values in this array should not be modified in the program and are only granted to remain unchanged until the next call to a non-constant member function of the string object.
    Last edited by toraj58; December 14th, 2008 at 09:40 AM.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  5. #5
    Join Date
    Dec 2008
    Posts
    3

    Re: Real newby question - string manipulation

    Thanks.. That all worked

  6. #6
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: Real newby question - string manipulation

    your welcome.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)