Is there a built in function for escaping quotes - c#

My signup page at http://nettpals.in/pages/signup, because, I'm creating an array of user names.
One users has kept his name as john'orielly, which is causing the array to break.
Like this:
var array=['joe','jook','john'orielly'];
Is there any way to escape these ' single quotes?
Code to populate the array:
public static string StringTokenizr(this List<string> list, NpStringTokenizrType type, string splitter = ",")
{
string s = null;
if (list.Count == 0) return null;
for (int i = 0; i < list.Count; i++)
{
if (type == NpStringTokenizrType.IntegerLike)
s += list[i] + splitter;
else s += "'" + list[i] + "'" + splitter;
}
return s.RemoveLast();// removes last comma
}

You can do it this way, assuming the names are stored in a generic list:
var array=[<%=string.Join(", ", arrNames.ConvertAll(name => string.Format("'{0}'", name.Replace("'", "\\'"))))%>];
Saw your edit just now so the above is one liner for the StringTokenizr you have.

You could try replacing the single quote ' with the html equivalent " this would then render on the page correctly and not break your array

Related

How to convert a C# list to a string with a fixed number of items per line using StringBuilder?

I am trying to convert a List<int> of several hundred integers to a string that, when printed, has a specified number of integers per line. I also put ", " between each integer in the output and put opening and closing brackets. The final line has fewer items than the previous lines if the List has a Count that is not a multiple of the number of items per line. For example, if the List had 49 integers and the method was called to put 11 items on each line, the output string, when printed, would look something like this:
[3397, 3398, 3401, 3403, 3409, 3415, 3418, 3419, 3421, 3427, 3431,
3437, 3439, 3442, 3443, 3446, 3453, 3455, 3459, 3466, 3473, 3481,
3482, 3487, 3489, 3493, 3494, 3497, 3503, 3505, 3506, 3513, 3518,
3521, 3523, 3543, 3545, 3551, 3554, 3561, 3563, 3566, 3569, 3574,
3578, 3579, 3587, 3589, 3595]
I already wrote a method that does this, and the output looks great, but I feel like my code may be suffering from spaghetti/jank. Is there a better way to do this, using fewer lines of code and using StringBuilder instead of string concatenation?
public static string MakeBigStringFromList(List<int> input,
int itemsPerLine)
{
int length = input.Count;
List<int>.Enumerator intEnumerator = input.GetEnumerator();
string bigReturnString = "[";
if (length > 0)
{
intEnumerator.MoveNext();
bigReturnString += intEnumerator.Current.ToString();
if (length > 1)
{
bigReturnString += ", ";
}
}
int firstTime = 1;
int i = 1;
while (i < length)
{
string line = "";
int j = 0 + firstTime;
if (i + itemsPerLine < length)
{
while (j < itemsPerLine)
{
intEnumerator.MoveNext();
line += intEnumerator.Current.ToString() + ", ";
i++;
j++;
}
bigReturnString += line + "\n";
firstTime = 0;
}
else
{
while (i < (length - 1))
{
intEnumerator.MoveNext();
line += intEnumerator.Current.ToString() + ", ";
i++;
}
intEnumerator.MoveNext();
line += intEnumerator.Current.ToString();
i++;
bigReturnString += line;
intEnumerator.Dispose();
}
}
bigReturnString += "]";
return bigReturnString;
}
Yes, you could make it a "little bit" shorter and more readable, for example with LINQ
var intsPerLine = list.Select((i, index) => (Integer: i, Index: index))
.GroupBy(x => x.Index / 11, x => x.Integer.ToString())
.Select(g => string.Join(", ", g) + ",");
string result = $"[{string.Join(Environment.NewLine, intsPerLine).TrimEnd(',')}]";
String.Join is using a StringBuilder under the hood, so this is not inefficient.
Explanation: Group all ints into packages of 11 with integer division, select the int as string:
GroupBy(x => x.Index / 11, x => x.Integer.ToString())
These 11 ints are concatendated with comma, so you get multiple strings:
Select(g => string.Join(", ", g) + ",")
Build the final string by using String.Join on these line-strings with Environment.NewLine. The last comma is removed and then wrap it in brackets:
$"[{string.Join(Environment.NewLine, intsPerLine).TrimEnd(',')}]
The result is same as yours(apart from that you use "\n" and i use Environment.NewLine).
Try this,
Convert the values from integer list to string list.
List<string> sLst = iLst.ConvertAll<string>(delegate (int i) { return
i.ToString(); });
Values can be printed capturing the list range by converting to an array and display it,
Console.WriteLine(String.Join(",", sLst.GetRange(j, 11).ToArray()));
Without using anything FANCY...
public static string MakeBigStringFromList(List<int> input, int itemsPerLine)
{
StringBuilder sb = new StringBuilder("[");
for(int i=0; i<input.Count; i++)
{
sb.Append(input[i]);
if ((i+1) != input.Count)
{
sb.Append(", ");
if ((i + 1) % itemsPerLine == 0)
{
sb.Append(Environment.NewLine);
}
}
}
sb.Append("]");
return sb.ToString();
}
If you are on .NET6, you can make use of the Chunk LINQ method:
string BuildString(IEnumerable<int> list, int countPerLine)
{
return "["
+ string.Join(Environment.NewLine,
list.Chunk(countPerLine).Select(c => string.Join(", ", c) + ","))
.TrimEnd(',')
+ "]";
}
Chunk will split the input into sub-sequences of at most countPerLine elements. Then string.Join(", ", c) + "," creates a line from each of the sub-sequences, and finally string.Join(Environment.NewLine, ...) combines these lines.

C#: Need to split a string into a string[] and keeping the delimiter (also a string) at the beginning of the string

I think I am too dumb to solve this problem...
I have some formulas which need to be "translated" from one syntax to another.
Let's say I have a formula that goes like that (it's a simple one, others have many "Ceilings" in it):
string formulaString = "If([Param1] = 0, 1, Ceiling([Param2] / 0.55) * [Param3])";
I need to replace "Ceiling()" with "Ceiling(; 1)" (basically, insert "; 1" before the ")").
My attempt is to split the fomulaString at "Ceiling(" so I am able to iterate through the string array and insert my string at the correct index (counting every "(" and ")" to get the right index)
What I have so far:
//splits correct, but loses "CEILING("
string[] parts = formulaString.Split(new[] { "CEILING(" }, StringSplitOptions.None);
//splits almost correct, "CEILING(" is in another group
string[] parts = Regex.Split(formulaString, #"(CEILING\()");
//splits almost every letter
string[] parts = Regex.Split(formulaString, #"(?=[(CEILING\()])");
When everything is done, I concat the string so I have my complete formula again.
What do I have to set as Regex pattern to achieve this sample? (Or any other method that will help me)
part1 = "If([Param1] = 0, 1, ";
part2 = "Ceiling([Param2] / 0.55) * [Param3])";
//part3 = next "CEILING(" in a longer formula and so on...
As I mention in a comment, you almost got it: (?=Ceiling). This is incomplete for your use case unfortunately.
I need to replace "Ceiling()" with "Ceiling(; 1)" (basically, insert "; 1" before the ")").
Depending on your regex engine (for example JS) this works:
string[] parts = Regex.Split(formulaString, #"(?<=Ceiling\([^)]*(?=\)))");
string modifiedFormula = String.join("; 1", parts);
The regex
(?<=Ceiling\([^)]*(?=\)))
(?<= ) Positive lookbehind
Ceiling\( Search for literal "Ceiling("
[^)] Match any char which is not ")" ..
* .. 0 or more times
(?=\)) Positive lookahead for ")", effectively making us stop before the ")"
This regex is a zero-assertion, therefore nothing is lost and it will cut your strings before the last ")" in every "Ceiling()".
This solution would break whenever you have nested "Ceiling()". Then your only solution would be writing your own parser for the same reasons why you can't parse markup with regex.
Regex.Replace(formulaString, #"(?<=Ceiling\()(.*?)(?=\))","$1; 1");
Note: This will not work for nested "Ceilings", but it does for Ceiling(), It will also not work fir Ceiling(AnotherFunc(x)). For that you need something like:
Regex.Replace(formulaString, #"(?<=Ceiling\()((.*\((?>[^()]+|(?1))*\))*|[^\)]*)(\))","$1; 1$3");
but I could not get that to work with .NET, only in JavaScript.
This is my solution:
private string ConvertCeiling(string formula)
{
int ceilingsCount = formula.CountOccurences("Ceiling(");
int startIndex = 0;
int bracketCounter;
for (int i = 0; i < ceilingsCount; i++)
{
startIndex = formula.IndexOf("Ceiling(", startIndex);
bracketCounter = 0;
for (int j = 0; j < formula.Length; j++)
{
if (j < startIndex) continue;
var c = formula[j];
if (c == '(')
{
bracketCounter++;
}
if (c == ')')
{
bracketCounter--;
if (bracketCounter == 0)
{
// found end
formula = formula.Insert(j, "; 1");
startIndex++;
break;
}
}
}
}
return formula;
}
And CountOccurence:
public static int CountOccurences(this string value, string parameter)
{
int counter = 0;
int startIndex = 0;
int indexOfCeiling;
do
{
indexOfCeiling = value.IndexOf(parameter, startIndex);
if (indexOfCeiling < 0)
{
break;
}
else
{
startIndex = indexOfCeiling + 1;
counter++;
}
} while (true);
return counter;
}

C# How to generate a new string based on multiple ranged index

Let's say I have a string like this one, left part is a word, right part is a collection of indices (single or range) used to reference furigana (phonetics) for kanjis in my word:
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす"
The pattern in detail:
word,<startIndex>(-<endIndex>):<furigana>
What would be the best way to achieve something like this (with a space in front of the kanji to mark which part is linked to the [furigana]):
子[こ]で 子[こ]にならぬ 時鳥[ほととぎす]
Edit: (thanks for your comments guys)
Here is what I wrote so far:
static void Main(string[] args)
{
string myString = "ABCDEF,1:test;3:test2";
//Split Kanjis / Indices
string[] tokens = myString.Split(',');
//Extract furigana indices
string[] indices = tokens[1].Split(';');
//Dictionnary to store furigana indices
Dictionary<string, string> furiganaIndices = new Dictionary<string, string>();
//Collect
foreach (string index in indices)
{
string[] splitIndex = index.Split(':');
furiganaIndices.Add(splitIndex[0], splitIndex[1]);
}
//Processing
string result = tokens[0] + ",";
for (int i = 0; i < tokens[0].Length; i++)
{
string currentIndex = i.ToString();
if (furiganaIndices.ContainsKey(currentIndex)) //add [furigana]
{
string currentFurigana = furiganaIndices[currentIndex].ToString();
result = result + " " + tokens[0].ElementAt(i) + string.Format("[{0}]", currentFurigana);
}
else //nothing to add
{
result = result + tokens[0].ElementAt(i);
}
}
File.AppendAllText(#"D:\test.txt", result + Environment.NewLine);
}
Result:
ABCDEF,A B[test]C D[test2]EF
I struggle to find a way to process ranged indices:
string myString = "ABCDEF,1:test;2-3:test2";
Result : ABCDEF,A B[test] CD[test2]EF
I don't have anything against manually manipulating strings per se. But given that you seem to have a regular pattern describing the inputs, it seems to me that a solution that uses regex would be more maintainable and readable. So with that in mind, here's an example program that takes that approach:
class Program
{
private const string _kinvalidFormatException = "Invalid format for edit specification";
private static readonly Regex
regex1 = new Regex(#"(?<word>[^,]+),(?<edit>(?:\d+)(?:-(?:\d+))?:(?:[^;]+);?)+", RegexOptions.Compiled),
regex2 = new Regex(#"(?<start>\d+)(?:-(?<end>\d+))?:(?<furigana>[^;]+);?", RegexOptions.Compiled);
static void Main(string[] args)
{
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす";
string result = EditString(myString);
}
private static string EditString(string myString)
{
Match editsMatch = regex1.Match(myString);
if (!editsMatch.Success)
{
throw new ArgumentException(_kinvalidFormatException);
}
int ichCur = 0;
string input = editsMatch.Groups["word"].Value;
StringBuilder text = new StringBuilder();
foreach (Capture capture in editsMatch.Groups["edit"].Captures)
{
Match oneEditMatch = regex2.Match(capture.Value);
if (!oneEditMatch.Success)
{
throw new ArgumentException(_kinvalidFormatException);
}
int start, end;
if (!int.TryParse(oneEditMatch.Groups["start"].Value, out start))
{
throw new ArgumentException(_kinvalidFormatException);
}
Group endGroup = oneEditMatch.Groups["end"];
if (endGroup.Success)
{
if (!int.TryParse(endGroup.Value, out end))
{
throw new ArgumentException(_kinvalidFormatException);
}
}
else
{
end = start;
}
text.Append(input.Substring(ichCur, start - ichCur));
if (text.Length > 0)
{
text.Append(' ');
}
ichCur = end + 1;
text.Append(input.Substring(start, ichCur - start));
text.Append(string.Format("[{0}]", oneEditMatch.Groups["furigana"]));
}
if (ichCur < input.Length)
{
text.Append(input.Substring(ichCur));
}
return text.ToString();
}
}
Notes:
This implementation assumes that the edit specifications will be listed in order and won't overlap. It makes no attempt to validate that part of the input; depending on where you are getting your input from you may want to add that. If it's valid for the specifications to be listed out of order, you can also extend the above to first store the edits in a list and sort the list by the start index before actually editing the string. (In similar fashion to the way the other proposed answer works; though, why they are using a dictionary instead of a simple list to store the individual edits, I have no idea…that seems arbitrarily complicated to me.)
I included basic input validation, throwing exceptions where failures occur in the pattern matching. A more user-friendly implementation would add more specific information to each exception, describing what part of the input actually was invalid.
The Regex class actually has a Replace() method, which allows for complete customization. The above could have been implemented that way, using Replace() and a MatchEvaluator to provide the replacement text, instead of just appending text to a StringBuilder. Which way to do it is mostly a matter of preference, though the MatchEvaluator might be preferred if you have a need for more flexible implementation options (i.e. if the exact format of the result can vary).
If you do choose to use the other proposed answer, I strongly recommend you use StringBuilder instead of simply concatenating onto the results variable. For short strings it won't matter much, but you should get into the habit of always using StringBuilder when you have a loop that is incrementally adding onto a string value, because for long string the performance implications of using concatenation can be very negative.
This should do it (and even handle ranged indices), based on the formatting of the input string you have-
using System;
using System.Collections.Generic;
public class stringParser
{
private struct IndexElements
{
public int start;
public int end;
public string value;
}
public static void Main()
{
//input string
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす";
int wordIndexSplit = myString.IndexOf(',');
string word = myString.Substring(0,wordIndexSplit);
string indices = myString.Substring(wordIndexSplit + 1);
string[] eachIndex = indices.Split(';');
Dictionary<int,IndexElements> index = new Dictionary<int,IndexElements>();
string[] elements;
IndexElements e;
int dash;
int n = 0;
int last = -1;
string results = "";
foreach (string s in eachIndex)
{
e = new IndexElements();
elements = s.Split(':');
if (elements[0].Contains("-"))
{
dash = elements[0].IndexOf('-');
e.start = int.Parse(elements[0].Substring(0,dash));
e.end = int.Parse(elements[0].Substring(dash + 1));
}
else
{
e.start = int.Parse(elements[0]);
e.end = e.start;
}
e.value = elements[1];
index.Add(n,e);
n++;
}
//this is the part that takes the "setup" from the parts above and forms the result string
//loop through each of the "indices" parsed above
for (int i = 0; i < index.Count; i++)
{
//if this is the first iteration through the loop, and the first "index" does not start
//at position 0, add the beginning characters before its start
if (last == -1 && index[i].start > 0)
{
results += word.Substring(0,index[i].start);
}
//if this is not the first iteration through the loop, and the previous iteration did
//not stop at the position directly before the start of the current iteration, add
//the intermediary chracters
else if (last != -1 && last + 1 != index[i].start)
{
results += word.Substring(last + 1,index[i].start - (last + 1));
}
//add the space before the "index" match, the actual match, and then the formatted "index"
results += " " + word.Substring(index[i].start,(index[i].end - index[i].start) + 1)
+ "[" + index[i].value + "]";
//remember the position of the ending for the next iteration
last = index[i].end;
}
//if the last "index" did not stop at the end of the input string, add the remaining characters
if (index[index.Keys.Count - 1].end + 1 < word.Length)
{
results += word.Substring(index[index.Keys.Count-1].end + 1);
}
//trimming spaces that may be left behind
results = results.Trim();
Console.WriteLine("INPUT - " + myString);
Console.WriteLine("OUTPUT - " + results);
Console.Read();
}
}
input - 子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす
output - 子[こ]で 子[こ]にならぬ 時鳥[ほととぎす]
Note that this should also work with characters the English alphabet if you wanted to use English instead-
input - iliketocodeverymuch,2:A;4-6:B;9-12:CDEFG
output - il i[A]k eto[B]co deve[CDEFG]rymuch

How to search for word in a string (just the word)?

I want to search a string for a word.
However, I don't want to get a result if the word searched is inside other word.
That is
I want this to return the number 7 (index of letter f):
findWord("Potato for you", "for")
but I want this to return -1 (i.e., not found)
findWord("Potato for you", "or")
If I use IndexOf, it will find the substring "or" inside the word "for".
Is there any simple way to do this?
char[] terminationCharacters = new char[] { '\n', '\t', ' ', '\r' };
//get array with each word to be taken into consideration
string[] words= s.Split(terminationCharacters, StringSplitOptions.RemoveEmptyEntries);
int indexOfWordInArray = Array.IndexOf(words, wordToFind);
int indexOfWordInS = 0;
for (int i = 0; i <= indexOfWordInArray; i++)
{
indexOfWordInS += words[i].Length;
}
return indexOfWordInS;
But this obviously may not work if there are multiple spaces between the words.
Is there any pre-built way to do this apparently simple thing, or should I just use a Regex?
You can use a regular expression:
var match = Regex.Match("Potato for you", #"\bfor\b");
if (match.Success)
{
int index = match.Index;
...
}
\b indicates a word boundary.
If you don't need the index but you just want to check if the word is in the string, you can use IsMatch, which returns a boolean, instead of Match.
If you're looking for the index, you can make a method like this. If you only want a bool whether or not it is in there, then the method would be a little simpler. More than likely, there could be a way to do this with a Regex easier, but they are not my forte.
I'll set it up as an extension method to make it easier to use.
public static int FindFullWord(this string search, string word)
{
if (search == word || search.StartsWith(word + " "))
{
return 0;
}
else if (search.EndsWith(" " + word))
{
return search.Length - word.Length;
}
else if (search.Contains(" " + word + " "))
{
return search.IndexOf(" " + word + " ") + 1;
}
else {
return -1;
}
}

CSV Validation in C# - Making sure each row has the same number of commas

I wish to implement a fairly simple CSV checker in my C#/ASP.NET application - my project automatically generates CSV's from GridView's for users, but I want to be able to quickly run through each line and see if they have the same amount of commas, and throw an exception if any differences occur. So far I have this, which does work but there are some issues I'll describe soon:
int? CommaCount = null;
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
String Str = null;
//This loops through all the headerrow cells and writes them to the stringbuilder
for (int k = 0; k <= (grd.Columns.Count - 1); k++)
{
sw.Write(grd.HeaderRow.Cells[k].Text + ",");
}
sw.WriteLine(",");
//This loops through all the main rows and writes them to the stringbuilder
for (int i = 0; i <= grd.Rows.Count - 1; i++)
{
StringBuilder RowString = new StringBuilder();
for (int j = 0; j <= grd.Columns.Count - 1; j++)
{
//We'll need to strip meaningless junk such as <br /> and
Str = grd.Rows[i].Cells[j].Text.ToString().Replace("<br />", "");
if (Str == " ")
{
Str = "";
}
Str = "\"" + Str + "\"" + ",";
RowString.Append(Str);
sw.Write(Str);
}
sw.WriteLine();
//The below code block ensures that each row contains the same number of commas, which is crucial
int RowCommaCount = CheckChar(RowString.ToString(), ',');
if (CommaCount == null)
{
CommaCount = RowCommaCount;
}
else
{
if (CommaCount!= RowCommaCount)
{
throw new Exception("CSV generated is corrupt - line " + i + " has " + RowCommaCount + " commas when it should have " + CommaCount);
}
}
}
sw.Close();
And my CheckChar method:
protected static int CheckChar(string Input, char CharToCheck)
{
int Counter = 0;
foreach (char StringChar in Input)
{
if (StringChar == CharToCheck)
{
Counter++;
}
}
return Counter;
}
Now my problem is, if a cell in the grid contains a comma, my check char method will still count these as delimiters so will return an error. As you can see in the code, I wrap all the values in " characters to 'escape' them. How simple would it be to ignore commas in values in my method? I assume I'll need to rewrite the method quite a lot.
var rx = new Regex("^ ( ( \"[^\"]*\" ) | ( (?!$)[^\",] )+ | (?<1>,) )* $", RegexOptions.ExplicitCapture | RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
var matches = rx.Matches("Hello,World,How,Are\nYou,Today,This,Is,\"A beautiful, world\",Hi!");
for (int i = 1; i < matches.Count; i++) {
if (matches[i].Groups[1].Captures.Count != matches[i - 1].Groups[1].Captures.Count) {
throw new Exception();
}
}
You could just use a regular expression that matches one item and count the number of matches in your line. An example of such a regex is the following:
var itemsRegex =
new Regex(#"(?<=(^|[\" + separator + #"]))((?<item>[^""\" + separator +
#"\n]*)|(?<item>""([^""]|"""")*""))(?=($|[\" + separator + #"]))");
Just do something like the following (assuming you don't want to have " inside your fields (otherwise these need some extra handling)):
protected static int CheckChar(string Input, char CharToCheck, char fieldDelimiter)
{
int Counter = 0;
bool inValue = false;
foreach (char StringChar in Input)
{
if (StringChar == fieldDelimiter)
inValue = !inValue;
else if (!inValue && StringChar == CharToCheck)
Counter++;
}
return Counter;
}
This will cause inValue to be true while inside fields. E.g. pass '"' as fieldDelimiter to ignore everything between "...". Just note that this won't handle escaped " (like "" or \"). You'd have to add such handling yourself.
Instead of checking the resulting string (the cake) you should check the fields (ingredients) before you concatenate (mix) them. That would give you the change to do something constructive (escaping/replacing) and throwing an exception only as a last resort.
In general, "," are legal in .csv fields, as long as the string fields are quoted. So internal "," should not be a problem, but the quotes may well be.

Categories