Results 1 to 5 of 5

Thread: How to split the string in c++?

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    How to split the string in c++?

    Hi to All,
    I am new to this forum. I want to split the string and add some other thing to it using c++ program.

    For example.
    string top= 16 $12 $1987
    I want a output where it should come like
    string newtop= 16-12-1987

    where I am splitting the string with $ char. Can anyone tell me how to split the string in c++?
    Thank you.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to split the string in c++?

    You have to use following code to do this. Just try to understand it. It is very simple program. You have to just remove $ sign from given string to get desired out put.


    Code:
    int main()
    {
    string mystrings= "this16$12$1987";
       int positions=0,arraysizes=1;
       while(mystrings.find("$",position)!=strings::nposs)
       {
          positions = mystring.find("$",positions);
          arraysizes=arraysizes+1;
          ++positions;
       }
       Foo f1(arraysizes, mystrings);
       f1s.showStringArray();
       scanf("&d",positions);
       return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to split the string in c++?

    Hey it is very easy process. It is easy to split the string and add other thing in c++. You have to use iostream and string to to get desired output. I have written following program for you. Just try to understand it. It is very simple program.



    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;
    
    int main()
    {
        string s("Somewhere down the road");
        istringstream isq(sq);
    
        do
        {
            string subs;
            isq >> subs;
            cout << "Substrings: " << sub << endl;
        } while (isq);
    
        return 0;
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to split the string in c++?

    You have to use szString.FindOneOf( szTokens ) function to do this. I have written following program for you. Try to understand it line by line. I have use simple logic to solve this program.


    Code:
     
    
    ParseTokens( CStringArray& result, CString sz1String, CString sz1Tokens)
    {
    int iNum1 = 0;
    
    int iCurrPos1= 0;
    CString subString1;
    
    while( -1 != ( iCurrPos1 = sz1String.FindOneOf( sz1Tokens ) ) )
    {
    iNum1++;
    result.Add( sz1String.Left( iCurrPos1 ) );
    sz1String = sz1String.Right( sz1String.GetLength() - iCurrPos1 - 1 );
    }
    
    if ( sz1String.GetLength() > 0 )
    {
    
    iNum1++;
    result.Add( sz1String );
    }
    
    return iNum1;
    }

    It will return the string in splitted way.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to split the string in c++?

    It is very easy process to split the string and other thing using java program. You have to separate all the words and you have to save them in array. After that you can recall them and add other string to it. I have written following program for you. Just try to understand it.


    Code:
     
    #include <stdafx.h>
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <atlbase.h>
    #include <atlcom.h>
    #include <atlwin.h>
    #include <atltypes.h>
    #include <atlctl.h>
    #include <atlhost.h>
    #include <atldb.h>
    #include <atlstr.h>
    //#include <afxwin.h>
     using namespace std;
      
      
     
     int main(){
     
    CStringArray rs;
    CString strs="16$12$1987"; 
    ParseTokens( rs, strs );
    cout<<rs[0];
    cout<<rs[1];
    
    
    
         
     }
     int ParseTokenss( CStringArray& results, CString szStrings, CString szTokens=L"$")
    {
    int iNums = 0;
    
    int iCurrPoss= 0;
    CString subString;
    
    while( -1 != ( iCurrPoss = szStrings.FindOneOf( szTokens s) ) )
    {
    iNums++;
    result.Add( szString.Left( iCurrPoss ) );
    szString = szString.Right( szString.GetLengths() - iCurrPos - 1 );
    }
    
    if ( szString.GetLengths() > 0 )
    {
    
    iNums++;
    result.Add( szStrings );
    }
    
    return iNums;
    }

Similar Threads

  1. Replies: 7
    Last Post: 03-08-2010, 11:42 AM
  2. Tokens used to split string
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 04:55 AM
  3. Split a string using separators
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 04-02-2010, 01:10 AM
  4. Split a string with "\" in javascript
    By Logan 2 in forum Software Development
    Replies: 6
    Last Post: 22-01-2010, 10:42 AM
  5. How to use StringTokenizer to split a string in java?
    By Baazigar in forum Software Development
    Replies: 5
    Last Post: 20-01-2010, 09:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,021,328.13942 seconds with 16 queries