473,699 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ splitting strings but keeping the delimiter

13 New Member
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 2893
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
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 Recognized Expert Moderator Expert
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
phiefer3
67 New Member
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
phiefer3
67 New Member
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 New Member
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
1779
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 @@. What's the most efficient way to process this? Failing all else I will split the string into characters and use a FSM, but it seems that's not very pythonesqe. @rv@ 2 @db.locks@ @//depot/hello.txt@ @mh@ @mh@ 1 1 44 @pv@ 0 @db.changex@ 44 44 @mh@...
3
1669
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 the sub-strings. Here's what I have (I've thrown a main() in there for this mail). --- #include <iostream> #include <string>
9
14724
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
2965
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!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
20
3704
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 the data to assign each value to its own variable. So right now I am just saving the data to a txt file and when I look in the text file all the data is there. Not sure if this matters but when I open the text file in Word pad (Rich Text) It...
9
1212
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 expressions but I would like to avoid them ... Regards, Fabian Steiner
8
2442
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 doing wrong? bests, -rsr-
12
3040
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
3268
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 lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
0
8706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8630
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9199
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9055
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8899
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7786
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4638
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.