This question already has answers here:
Reading CSV files using C#
(12 answers)
Closed 7 years ago.
I have below mentioned CSV string which I need to split using commas .
Input:
A,"Rakesh,Gaur",B,"A,B",Z
OutPut:
A
Rakesh,Gaur
B
A,B
Z
You can't use string split or regular expressions. If you are not going to use a library that is already built, you have to keep track of whether or not you are in_quotes. but as you will find out after you start this: csv parsing is complex. You should use something that is already pre-built. As I recall from my days writing an app that heavily relied on csv, there are escape characters and such, that you will need to account for.
Either way the psuedo code is as follows:
Stack cells = m
in_quotes = false
foreach character in string:
if character != ',' && character != '"':
cells.Top = cells.Top + character
else if character == ',' && in_quotes:
cells.Top = cells.Top + character
else if character == ',':
cells.push("")
else if character == '"' && in_quotes:
in_quotes = false
else if character == '"':
in_quotes = true
I think you can do this using following steps:
string[] words = yourStringInput.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}
Related
This question already has answers here:
How to find and replace all occurrences of a substring in a string?
(9 answers)
Replace substring with another substring C++
(18 answers)
How to find and replace string?
(11 answers)
How do I replace all instances of a string with another string?
(6 answers)
Closed 6 months ago.
I'm using C++ and I have a problem. Instead of creating a new line it prints \n. My Code:
std::string text;
std::cout << text;
It prints:Hello\nWorld
It was supposed to read \n as a new line and print something like this:
"Hello
World"
So i've tried to use replace(text.begin(), text.end(), '\n', 'a') for testing purposes and nothing happened. It contiuned to print Hello\nWorld
std::replace() won't work in this situation. When called on a std::string, it can replace only single characters, but in your case your string actually contains 2 distinct characters '\' and 'n'. So you need to use std::string::find() and std::string::replace() instead, eg:
string::size_type index = 0;
while ((index = str.find("\\n", index)) != string::npos) {
str.replace(index, 2, "\n");
++index;
}
This question already has answers here:
Regex Match all characters between two strings
(16 answers)
Closed 4 years ago.
How to replace a string between two string in javascript
StartLine = `/*TESTSTART*/`;
Endline = `/*TESTEND*/`;
OriginalContent = `/*TESTSTART*/
testing
not
working
/*TESTEND*/`;
var e = OriginalContent .replace(/(StartLine)[\s\S]*?(Endline)/,' it's
working
fine');
OUTPUT = `/*TESTSTART*/
it's
working
fine
/*TESTEND*/`
1) How to check if the string contains / in regular exp?
2) if I stored sting in one variable, how can I use this variable in regular exp?
You can escape a / character with a backslash \ if you're using / to start your regular expression. But in this case, since you want to include the value of a variable in your regular expression, you should use a string to represent a regex instead, in which case there is no need to escape / but you should escape other special regex characters such as * with two backslashes, and you can simply concatenate the variable with the other string literals and variables to form the full regex:
StartLine = '/\\*TESTSTART\\*/';
Endline = '/\\*TESTEND\\*/';
...
var e = OriginalContent.replace(StartLine + '[\s\S]*?' + Endline, "it's
working
fine");
This question already has answers here:
How to replace all occurrences of a character in string?
(17 answers)
Replace part of a string with another string
(17 answers)
Closed 5 years ago.
I want to replace all occurrences of '$' with "$$". Currently I am using string::find to check if '$' is present in string or not. then I am using for loop and checking every character if it matches with '$'. If a character matches with $ then I am using string::replace to replace it.
Is there any other effective method to do this in c++? without traversing entire string with less complexity?
I don't know a standard function which replaces ALL occurrences of a certain string with another string. The replace-functions either replace all occurrences of a specific character by another character, or replace a single range of characters with another range.
So I think you cannot avoid iterating through the string on your own.
In your specific case, it might be easier, as you just have to insert an additional $ for every $-character you find. Note the special measure avoiding an endless loop, which would happen if one doubled also the $-value just inserted again and again:
int main() {
string s = "this $ should be doubled (i.e. $), then. But $$ has been doubled, too.";
auto it = s.begin();
while (it != s.end()) {
if (*it == '$') {
it = s.insert(it, '$');
it += 2;
}
else {
it++;
}
}
cout << s;
}
How to write a regular expression for a text field which accepts all characters except a comma (,) and do not accept a white space at both the ends? I have tried
[^,][\B ]
but no use
like 'product generic no' instead of 'product,generic,no' or ' product generic no '
I suggest a solution without regular expression. As you said you're using JS so the function is in JavaScript:
function isItInvalid(str) {
var last = str.length - 1;
return (last < 2 ||
str[0] == ' ' ||
str[last] == ' ' ||
str.indexOf(',') != -1);
}
EDIT: Just made it a bit more readable. It also checks if the string is at least 3 chars.
Something like below:
/^\S[^,]*\S$/
Using a Perl regular expression
/^\S[^,]*\S$/
This should work from 2 characters up, but fails in the edge case where the string has only one non-comma character. To cover that too:
/^((\S[^,]*\S)|([^\s,]))$/
Not sure if this is possible... but I need to find (and replace) all commas within strings, which I'm going to run on a PHP code file. i.e., something like "[^"]+,[^"]+" except that'll search on the wrong side of the strings too (the first quote is where a string ends, and the last one where it begins). I can run it multiple times to get all the commas, if necessary. I'm trying to use the Find-and-Replace feature in Komodo. This is a one-off job.
Well, here's my script so far, but it isn't working right. Worked on small test file, but on the full file its replacing commas outside of strings. Bah.
import sys, re
pattern = ','
replace = '~'
in_str = ''
out_str = ''
quote = None
in_file = open('infile.php', 'r')
out_file = open('outfile.php', 'w')
is_escaped = False # ...
while 1:
ch = in_file.read(1)
if not ch: break
if ch in ('"',"'"):
if quote is None:
quote = ch
elif quote == ch:
quote = None
out_file.write(out_str)
out_file.write(re.sub(pattern,replace,in_str))
in_str = ''
out_str = ''
if ch != quote and quote is not None:
in_str += ch
else:
out_str += ch
out_file.write(out_str)
out_file.write(in_str)
in_file.close()
out_file.close()
I take it your trying to find string literals in the PHP code (i.e. places in the code where someone has specified a string between quote marks: $somevar = "somevalue"; )
In this case, it may be easier to write a short piece of parsing code than a regex (since it will be complicated in the regex to distinguish the quote marks that begin a string literal from the quote marks that end it).
Some pseudocode:
inquote = false
while (!eof)
c = get_next_character()
if (c == QUOTE_MARK)
inquote = !inquote
if (c == COMMA)
if (inquote)
delete_current_character()