Results 1 to 13 of 13

Thread: C++ String Help

  1. #1
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    C++ String Help

    hey guys,
    Ok i have some coursework that i need to do so i cant give you my code or really ask for help cos thats cheating
    But all i would like to know is if i have a string that contains some text such as:
    "abcd 123 345"
    now i would like to take "abcd" and put it in another string and then "123" and put that in another string etc... I am assuming i can use the blank space as a delimiter but does anyone know how i could do this?

  2. #2
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    throw new ArgumentException (String, String, Exception)

  3. Received thanks from:

    crazyfool (22-10-2008)

  4. #3
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS

    Re: C++ String Help

    String.Split(' ')

    aw bugger, wrong language.

  5. #4
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ String Help

    cheers for that but me being new to c++ and having been at this for about 4 hours straight i dont get what char * pch; does

  6. #5
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ String Help

    If you're delimiting with whitespace, then you can also use input string streams

    Here is some noddy example code:

    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
      string myStr = "abcd 123 345";
      istringstream iss(myStr);
      string out1, out2, out3;
      iss >> out1 >> out2 >> out3;
      cout << out1 << endl;
      cout << out2 << endl;
      cout << out3 << endl;
      return 0;
    }
    Output is:

    abcd
    123
    345

  7. #6
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ String Help

    Quote Originally Posted by crazyfool View Post
    cheers for that but me being new to c++ and having been at this for about 4 hours straight i dont get what char * pch; does
    Use the string streams method if you're just delimiting with whitespace. It's more "C++" - you don't have to mess around with C-style strings (i.e. char * str)

  8. #7
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,164
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts

    Re: C++ String Help

    Am i the only one who dosen't like the streams been used in such a fashion, its hardly transparent as to whats going on, is going to be harder to maintain (what if you want to split on an array of chars?).

    etc.
    throw new ArgumentException (String, String, Exception)

  9. #8
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ String Help

    Quote Originally Posted by TheAnimus View Post
    Am i the only one who dosen't like the streams been used in such a fashion, its hardly transparent as to whats going on
    Eh? It's what input string streams are designed for. Plus it'll provide type conversion if he wants the numbers as an unsigned, etc. Seems pretty transparent to me.

    Is going to be harder to maintain (what if you want to split on an array of chars?).
    etc.
    As with all programming it's cost/benefit really. He wanted a simple easy to understand way of splitting up the given string delimited with whitespace. This is what I provided, without the added hassle of dealing with char * stuff that he clearly didn't understand. Is there really going to be anything to maintain in a small n00b C++ project?

  10. Received thanks from:

    crazyfool (22-10-2008)

  11. #9
    Senior Member crazyfool's Avatar
    Join Date
    Jan 2008
    Posts
    761
    Thanks
    77
    Thanked
    38 times in 38 posts
    • crazyfool's system
      • Motherboard:
      • Striker Extreme
      • CPU:
      • Q6600
      • Memory:
      • OCZ 6400 2 x 2GB
      • Storage:
      • Samsung Spinpoint 500GB
      • Graphics card(s):
      • BFG 8800 GT OC 512MB
      • PSU:
      • Coolermaster Real Power Pro 850w
      • Case:
      • Coolermaster CM-690
      • Operating System:
      • XP, Vista, Windows 7 & Ubuntu
      • Monitor(s):
      • Samsung 2232BW

    Re: C++ String Help

    hey not so much of the noob lol... and yeh Fraz' way was easier for me to get seeing as i've only been at this for a few days

  12. #10
    Registered+
    Join Date
    Jul 2008
    Location
    London
    Posts
    40
    Thanks
    0
    Thanked
    4 times in 4 posts

    Re: C++ String Help

    Quote Originally Posted by crazyfool View Post
    cheers for that but me being new to c++ and having been at this for about 4 hours straight i dont get what char * pch; does
    This is just declaring a variable named pch which is of type char *

    So a pointer to a char (hence the p). Nothing to do with multiplication

  13. #11
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ String Help

    Quote Originally Posted by crazyfool View Post
    cheers for that but me being new to c++ and having been at this for about 4 hours straight i dont get what char * pch; does
    (char *) is the type used by most old C-style strings. And the legacy libc functions in C++ reflect that. Which is also why std::string has (char *) overloaded functions and operators.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

  14. #12
    Get in the van. Fraz's Avatar
    Join Date
    Aug 2007
    Location
    Bristol
    Posts
    2,919
    Thanks
    284
    Thanked
    397 times in 231 posts
    • Fraz's system
      • Motherboard:
      • Gigabyte X58A-UD5
      • CPU:
      • Watercooled i7-980X @ 4.2 GHz
      • Memory:
      • 24GB Crucial DDR3-1333
      • Storage:
      • 240 GB Vertex2E + 2 TB of Disk
      • Graphics card(s):
      • Water-cooled Sapphire 7970 @ 1175/1625
      • PSU:
      • Enermax Modu87+
      • Case:
      • Corsair 700D
      • Operating System:
      • Linux Mint 12 / Windows 7
      • Monitor(s):
      • Dell 30" 3008WFP and two Dell 24" 2412M
      • Internet:
      • Virgin Media 60 Mbps

    Re: C++ String Help

    Err... I think he stopped needing our help about 3 weeks ago now guys...

  15. #13
    Gentoo Ricer
    Join Date
    Jan 2005
    Location
    Galway
    Posts
    11,048
    Thanks
    1,016
    Thanked
    944 times in 704 posts
    • aidanjt's system
      • Motherboard:
      • Asus Strix Z370-G
      • CPU:
      • Intel i7-8700K
      • Memory:
      • 2x8GB Corsiar LPX 3000C15
      • Storage:
      • 500GB Samsung 960 EVO
      • Graphics card(s):
      • EVGA GTX 970 SC ACX 2.0
      • PSU:
      • EVGA G3 750W
      • Case:
      • Fractal Design Define C Mini
      • Operating System:
      • Windows 10 Pro
      • Monitor(s):
      • Asus MG279Q
      • Internet:
      • 240mbps Virgin Cable

    Re: C++ String Help

    Quote Originally Posted by Fraz View Post
    Err... I think he stopped needing our help about 3 weeks ago now guys...
    Oops, didn't notice that the thread was resurrected.
    Quote Originally Posted by Agent View Post
    ...every time Creative bring out a new card range their advertising makes it sound like they have discovered a way to insert a thousand Chuck Norris super dwarfs in your ears...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. string out of bounds error
    By j.o.s.h.1408 in forum Software
    Replies: 1
    Last Post: 03-05-2007, 07:49 PM
  2. Executing a string in C++
    By mike_w in forum Software
    Replies: 4
    Last Post: 23-07-2006, 10:58 AM
  3. Just got my first 12 string
    By Alex in forum Consumer Electronics
    Replies: 8
    Last Post: 10-07-2004, 01:00 PM
  4. Zakky - Thanks for the string dude :)
    By Tumble in forum General Discussion
    Replies: 3
    Last Post: 03-06-2004, 03:01 PM
  5. String and rope......
    By Zak33 in forum General Discussion
    Replies: 17
    Last Post: 31-01-2004, 03:52 PM

Posting Permissions

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