473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

c++ splitting strings but keeping the delimiter

13
Hey i looked at some earlier discussions on this topic and they seem a bit complex i found this code example on another site -

Expand|Select|Wrap|Line Numbers
  1. char str[]   = "200.0.0.90-200.0.0.223";
  2.  
  3. char *first  = strtok(str, "-");
  4.  
  5. char *second = strtok(NULL, "\0");
  6.  
and i was wondering if it was possible to keep the delimiter in other words in this example the hyphen. Because in this example the hyphen is replace with a NULL and i need to keep the delimiter.
Thanks in advance, any help would be greatly appreciated.
Jun 30 '07 #1
6 2877
weaknessforcats
9,208 Expert Mod 8TB
Why can't you use an array of string objects??
Expand|Select|Wrap|Line Numbers
  1. string arr[2];
  2.          arr[1] ="200.0.0.90";
  3.          arr[2] ="200.0.0.223";
  4.  
char* strings are used in C. C++ programs should use STL string objects.
Jun 30 '07 #2
chezz
13
Let me explain it a bit better, i made a decimal to binary converter and with the final output if i used a small decimal number than i will get this 00000000001010 and i wanna chop of the zeros when it reaches one but keep the one.

Also i do use string bla; , for strings rather than char bla [8]; it just i'm trying to find a viable option for doing this.

Thanks in advanced
Jul 1 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Have you tried using a bitset<> and a string ??
Expand|Select|Wrap|Line Numbers
  1. unsigned int data = 10;
  2. bitset<32> bits(data);     //convert data to 32 bits
  3. string str = bits.to_string();  //convert the bits to char 1's and 0's
  4. while (str[0] == '0')        //remove leading '0'
  5. {
  6.      str.erase(0,1);
  7. }
  8. cout << str << endl;
  9.  
Jul 1 '07 #4
Or, you can just do this:

Expand|Select|Wrap|Line Numbers
  1. void chop(string& thisString)
  2. {
  3.     while (thisString[0] == '0')
  4.         thisString = thisString.substr(1, thisString.length());
  5. }
can then call it like this, if bla is your string:

chop(bla);
Jul 1 '07 #5
I thought of a method that would work faster, because it wouldn't involve a loop of function calls.

Expand|Select|Wrap|Line Numbers
  1. void binaryChop(string& thisString)
  2. {
  3.     thisString = thisString.substr(thisString.find(‘1’), thisString.length());
  4. }
this wouldn't even need to be a function.

sorry about the double post, but I couldn't edit my other one anymore.
Jul 1 '07 #6
chezz
13
Thanks for all the advice i will try them all and see which method works best, thanks again
Jul 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Mark Harrison | last post by:
What is the best way to process a text file of delimited strings? I've got a file where strings are quoted with at-signs, @like this@. At-signs in the string are represented as doubled @@. ...
3
by: Aaron Walker | last post by:
I have a feeling this going to end up being something so stupid, but right now I'm confused as hell. I'm trying to code a function, that given a string and a delimiter char, returns a vector of...
9
by: Dr. StrangeLove | last post by:
Greetings, Let say we want to split column 'list' in table lists into separate rows using the comma as the delimiter. Table lists id list 1 aa,bbb,c 2 e,f,gggg,hh 3 ii,kk 4 m
5
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!)...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
9
by: Fabian Steiner | last post by:
I often have to deal with strings like "PCI:2:3.0" or "PCI:3.4:0" and need the single numbers as tuple (2, 3, 0) or (3, 4, 0). Is there any simple way to achieve this? So far I am using regular...
8
by: ronrsr | last post by:
I'm trying to break up the result tuple into keyword phrases. The keyword phrases are separated by a ; -- the split function is not working the way I believe it should be. Can anyone see what I"m...
12
by: John | last post by:
Hi I have a multi-line address field which has each line separated by CRLF. How can I split this field into individual strings using crlf as separator? Thanks Regards
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.