EDIT: This is not a duplicate! The question in the link given is different. I'm not separating by spaces, I'm separating by anything other than characters in a given string. Therefor, the answers in the other thread don't work.  EDIT2: Please note that I'm not given the delimiters to separate by, I need to separate by anything OTHER THAN notOf.  In my program, that means cut up string named word into strings in a vector named words, which consist of and are separated by anything other than notOf. So notOf is sort of the opposite of a delimited in getline.  For example, "&amp;otherwise[taken=(and)-redacted-" should become "otherwise", "taken", "and" and "redacted".  I've tried writing and came up with what you see below, but it doesn't work and is also, I've been told, considerably ugly. Use your judgement whether it is worth fixing or writing anew.  #include &lt;string&gt; #include &lt;sstream&gt; #include &lt;vector&gt; #include &lt;iostream&gt;  using namespace std;  int main() {     string notOf = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789$'\"";     string word = "&amp;otherwise[taken=(and)-redacted-";     stringstream in(word);     vector&lt;string&gt; words;     if (word.find_first_not_of(notOf) == string::npos)         words.push_back(word);     else while (word.find_first_not_of(notOf) != string::npos)     {         int index = word.find_first_not_of(notOf);         string tok;         getline(in, tok, word[index]);         if (index != 0)             words.push_back(tok);         word = word.substr(index);     }     for (vector&lt;string&gt;::iterator it = words.begin(); it != words.end(); ++it)         cout &lt;&lt; *it &lt;&lt; endl;     system("pause");     return 0; }
2094
I'm trying to iterate over the words of a string.  The string can be assumed to be composed of words separated by whitespace.  Note that I'm not interested in C string functions or that kind of character manipulation/access. Also, please give precedence to elegance over efficiency in your answer.  The best solution I have right now is:  #include &lt;iostream&gt; #include &lt;sstream&gt; #include &lt;string&gt;  using namespace std;  int main() {     string s = "Somewhere down the road";     istringstream iss(s);      do     {         string subs;         iss &gt;&gt; subs;         cout &lt;&lt; "Substring: " &lt;&lt; subs &lt;&lt; endl;     } while (iss); }   Is there a more elegant way to do this?
7406
Java has a convenient split method:  String str = "The quick brown fox"; String[] results = str.split(" ");   Is there an easy way to do this in C++?
99957
I'm trying to parse a simple string in C++.  I know the string contains some text with a colon, followed immediately by a space, then a number.  I'd like to extract just the number part of the string.  I can't just tokenize on the space (using sstream and &lt;&lt;) because the text in front of the colon may or may not have spaces in it. Some example strings might be:  Total disk space: 9852465 Free disk space: 6243863 Sectors: 4095  I'd like to use the standard library, but if you have another solution you can post that too, since others with the same question might like to see different solutions.
78827
My brother recently started learning C++. He told me a problem he encountered while trying to validate input in a simple program. He had a text menu where the user entered an integer choice, if they entered an invalid choice, they would be asked to enter it again (do while loop). However, if the user entered a string instead of an int, the code would break. I read various questions on stackoverflow and told him to rewrite his code along the lines of:    #include&lt;iostream&gt; using namespace std;  int main() {     int a;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }     }while(true);     return 0; }   While this worked ok, I also tried a few other ideas: 1. Using a try catch block. It didn't work. I think this is because an exception is not raised due to bad input. 2. I tried if(! cin){//Do Something} which didn't work either. I haven't yet figured this one out. 3. Thirdly, I tried inputting a fixed length string and then parsing it. I would use atoi(). Is this standards compliant and portable? Should I write my own parsing function? 4. If write a class that uses cin, but dynamically does this kind of error detection, perhaps by determining the type of the input variable at runtime, would it have too much overhead? Is it even possible?  I would like to know what is the best way to do this kind of checking, what are the best practices?  I would like to add that while I am not new to writing C++ code, I am new to writing good standards compliant code. I am trying to unlearn bad practices and learn the right ones. I would be much obliged if answerers give a detailed explanation.  EDIT: I see that litb has answered one of my previous edits. I'll post that code here for reference.  #include&lt;iostream&gt; using namespace std;  int main() {     int a;     bool inputCompletionFlag = true;     do     {     cout&lt;&lt;"\nEnter a number:"     cin&gt;&gt;a;         if(cin.fail())         {             //Clear the fail state.             cin.clear();             //Ignore the rest of the wrong user input, till the end of the line.             cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(),\                                                     '\n');         }         else         {             inputCompletionFlag = false;         }     }while(!inputCompletionFlag);     return 0; }   This code fails on input like "1asdsdf". I didn't know how to fix it but litb has posted a great answer. :)
10985
gained a reputation of painting red the draft dissertations of his students, taking particular note of incidents when they omitted a non-breaking space that should have been included.  I have this list of places where you have to place non-breaking space:   before \cite before \ref before inlined equation   But, I am not sure if I got the rules right; is a non-breaking space mandatory  before all inline equations? How about numbers which occur in the text? And what if I refer to a program variable, or to program text, for example,  The 371 programmers who read, on 11 different occasions, the Java program in Figure~\ref{Program:Example}  noticed that it is peculiar since parameter \texttt{i} is never read by functions \texttt{f()} and \texttt{thisLongFuncgtionName()}...   Do I have to write The 371 programmers or The~371 programmers? on 11 different occasions or on~11 different occasions?  Do I need to write parameter~\texttt{i}? I think I should. What about functions~\texttt{f()}? And should I write and~\texttt{thisLongFuncgtionName()}? How about citations that use author, year convention?  In short, I think have an idea, but no exact definition of when you should add non-breaking space.
1424
I have the string   a.b.c.d   I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner.  (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop).
57185
I want to split a character string n by n. I know how to do that 1 by 1 :  \documentclass{article} \makeatletter  \def\s@plit&lt;#1#2&gt;{%   \ifx\empty#2%     #1%   \else     #1,\s@plit&lt;#2&gt;%   \fi}  \def\Split#1#2{\s@plit&lt;#2\empty&gt;} \makeatother  \begin{document} \Split{1}{123456789} \end{document}   It gives : 1,2,3,4,5,6,7,8,9  But I also want 1,23,45,67,89 or 123,456,789 ... depending from the first parameter.  EDIT  Work straight and reverse, even with foreach.        \documentclass{article}     \usepackage{tikz,xparse,xstring}  \DeclareDocumentCommand{\split}{smmo}{%     \xdef\Entree{#3}%     \let\Sortie\empty%     \IfBooleanTF #1     {% sans étoile         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\Sortie,\tmp}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleLeft{\Sortie}{1}[\Sortie]%     }     {% avec étoile dans l'ordre inverse         \loop         \StrRight{\Entree}{#2}[\tmp]%         \edef\Sortie{\tmp,\Sortie}%         \StrGobbleRight{\Entree}{#2}[\Entree]%         \unless\ifx\Entree\empty         \repeat         \StrGobbleRight{\Sortie}{1}[\Sortie]%     }     \IfNoValueTF{#4}{\Sortie}{\edef#4{\Sortie}}     }  \begin{document} \split{2}{6513782}  \split*{2}{6513782}  \split*{2}{6513782}[\bob]  \foreach \x in \bob {[\x] }  \end{document}
13318
How do I split a string with multiple separators in JavaScript? I'm trying to split on both commas and spaces, but AFAIK JavaScript's split() function only supports one separator.
107731
I'm learning Bash, and I want to replace space characters with other "non blank" characters. I'm using a for loop:  for f in *\ *; do mv "$f" "${f// /_}"; done   My question is, why are the double slash and the space in ${f// /_}? What does ${f// /_} do?
34577
When using \not together with wider symbols such as \Leftrightarrow the diagonal line produced by \not is placed too far to the left. How can the line be properly aligned? For example the following code \documentclass{article}  \begin{document}  \(\not\subset\)% Looks good  \(\not\approx\)% Looks good  \(\not\leftrightarrow\)% \not line is too far to the left  \(\not\Leftrightarrow\)% \not line is too far to the left  \end{document}  produces the following output where the last two symbols has got a misaligned diagonal line  Also, maybe the lines may need other changes than alignment to look good.
71565
When I want to have a space between lines I use \\~\\ or \newline  For example       Hello this is an example      \\~\\     Goodbye   I get the error there is no line to end here.  What am I doing wrong?
11843
Using \colon gives correct spacing for a map $F\colon C\to D$ in one direction. But writing $L\colon C\rightleftarrows D\colon R$ for a couple of maps in both directions (e.g. for an adjunction) gives disturbingly asymmetric spaces near first and second colons (like if D were a map from R, not the other way round). Probably, it should be fixed (e.g. some other command should be used)&nbsp;— but how?
39126
Is it me or are there no standard trim functions in the c or c++ library? is there any single function that acts as a trim? If not can anyone tell me Why trim is not part of the standard library? (i know trim is in boost)  My trim code is  std::string trim(const std::string &amp;str) {     size_t s = str.find_first_not_of(" \n\r\t");     size_t e = str.find_last_not_of (" \n\r\t");      if(( string::npos == s) || ( string::npos == e))         return "";     else         return str.substr(s, e-s+1); }   test: cout &lt;&lt; trim("  \n\r\r\n \r\n text here\nwith return   \n\r\r\n \r\n "); -edit- i mostly wanted to know why it wasnt in the standard library, BobbyShaftoe answer is great.
26480
I've been trying to find a way to filter a line that has the word "lemon" and "rice" in it. I know how to find "lemon"  or "rice"  but not the two of them. They don't need to be next to the other, just one the same line of text.
18554
I'm new to regular expressions and would appreciate your help.  I'm trying to put together an expression that will split the example string using all spaces that are not surrounded by single or double quotes. My last attempt looks like this: (?!") and isn't quite working. It's splitting on the space before the quote.  Example input:   This is a string that "will be" highlighted when your 'regular expression' matches something.   Desired output:  This is a string that will be highlighted when your regular expression matches something.   Note that "will be" and 'regular expression' retain the space between the words.
24166
It took me a while to word this best.  Problem 1:  Is there a way to find lines in a text file that have the same string at the beginning and the same strings at the end?   Beginning and end strings not necessarily equal to each other.  Text between the two strings is not consistant in number of characters or number of hyphens.  A hyphen is in between notable strings (which I added to my large text file).  For example:  File:    AAAAA-tjhut-ghyut-FILE1.txt   BBB-fhuj-dnbhg-frt76-FILE2.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   CCCC-fjuk-dryy-fhuj-FILE3.txt   Lines to detect:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   Problem 2:  Is there a way to remove all but one of the lines containing the duplicate strings at the beginning and end from above?  Before:    AAAAA-tjhut-ghyut-FILE1.txt   AAAAA-ghtujukr-hgute7gr-xhyutt-FILE1.txt   AAAAA-VGYT-4d7tt-xhyutt-FILE1.txt   After:    AAAAA-tjhut-ghyut-FILE1.txt   Big thanks for any advice.  Note: I think using the occurances of the first hyphen and last hyphen can be used to locate the duplicate strings.
28227
This should be easy, but I'm having a hard time finding the easiest solution.  I need an NSString that is equal to another string concatenated with itself a given number of times.  For a better explanation, consider the following python example:  &gt;&gt; original = "abc" "abc" &gt;&gt; times = 2 2 &gt;&gt; result = original * times "abcabc"   Any hints?    EDIT:  I was going to post a solution similar to the one by , after looking at this implementation from the OmniFrameworks:  // returns a string consisting of 'aLenght' spaces + (NSString *)spacesOfLength:(unsigned int)aLength; { static NSMutableString *spaces = nil; static NSLock *spacesLock; static unsigned int spacesLength;  if (!spaces) { spaces = [@"                " mutableCopy]; spacesLength = [spaces length];     spacesLock = [[NSLock alloc] init]; } if (spacesLength &lt; aLength) {     [spacesLock lock];     while (spacesLength &lt; aLength) {         [spaces appendString:spaces];         spacesLength += spacesLength;     }     [spacesLock unlock]; } return [spaces substringToIndex:aLength]; }   Code reproduced from the file:  Frameworks/OmniFoundation/OpenStepExtensions.subproj/NSString-OFExtensions.m   on the OpenExtensions framework from the  by .