I need to replace the characters ; and linebreak to the field p.notas. I tried this but it doesn't replace the linebreaks.
var res = from p in miDataContext.clientes
orderby p.nombreCom
select
(p.nombreCom ?? "") + ";" +
(p.razon ?? "") + ";" +
(p.nif ?? "") + ";" +
((p.notas ?? "").Replace (";" ,"."))
.Replace(Environment.NewLine , ". ");
The problem with using Environment.NewLine in your Replace() call is that the newline character(s) in your environment my not necessarily match what is actually used in the string. Yours probably has \r\n (windows) but what is actually stored is just \n. That pattern is definitely not in the string in that case. You'll have to check for all newline variants.
Assuming this is all fine in LINQ to SQL, just add more replace calls for each of the newline types.
var res = from p in miDataContext.clientes
orderby p.nombreCom
select
(p.nombreCom ?? "") + ";" +
(p.razon ?? "") + ";" +
(p.nif ?? "") + ";" +
((p.notas ?? "").Replace(";", "."))
.Replace("\r\n", ". ")
.Replace("\n", ". ")
.Replace("\r", ". ");
Related
Its a c# code written in a SSIS script task component.
PS_script + #"""$compname = " "\" + Row.computername"\" +
"$appname =" + Row.applicationname + " $appvalue = " + Row.appvalue +
"";
I am tying the MS deploy and setting params coming from table driven. The above statement throws error as I am not able to pass double quotes in computername param.
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim
In C#, a literal string with the prefix #, "" will be replaced by ".
Example :
var str = #"$compname = """ + Row.computername + #"""";
If the variable Row.computername has the value SERV01, then the result will be :
$compname = "SERV01"
You can also escape a character with \. Then \" will be replaced by ".
Example :
var str = "$compname = \"" + Row.computername + "\"";
If the variable Row.computername has the value SERV01, then the result will be :
$compname = "SERV01"
In you case, you can also use string.Format to more lisibility :
string.Format(
#" $compname = ""{0}"" $appname = ""{1}"" $appvalue = ""{2}""",
Row.computername, Row.applicationname, Row.appvalue
);
Warning, with SSIS script task, you can't use string interpolation (the literal string prefix $).
I am trying to pass quotes in string. I am having a hard time formulating the code.
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = "Set-UserPhoto ";
format += "" + user + "";
format += " -PictureData ([System.IO.File]::ReadAllBytes(";
format += "" + path + #"";
format += ")";
The user and path are variables that needs to be inside single quotes for the AD Command. command. What I have isn't working.
User \" for " symbol or \' for '
format += "\"" + user + "\"";
First of all , use string.format for such tasks. Second you have to escape quotes ( but you dont need to escape single quotes).
Double quotes can be escaped by double quote or by backslash based on type of string literal you are using:
var s = #"something "" somethin else "; // double double quote here
var s2 = "something \" somethin else ";
Now, using string.format, your code will turn into:
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = string.format("Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(\"{1}\")", user, path);
or
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = string.format(#"Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(""{1}"")", user, path);
string format = "Set-UserPhoto "; format += "'" + user + "'"; format += " -PictureData ([System.IO.File]::ReadAllBytes("; format += "'" + path + #"'"; format += ")";
I would suggest using string interpolation within a here-string as follows, this will prevent you from having to use string concatenation and escaping.
$format = #"
Set-UserPhoto " + user + " -PictureData ([System.IO.File]::ReadAllBytes(" + path + ")"
"#
My SQL query retrieves one row of multiple columns. I have stored that in string with '|' separator to differentiate between columns.
And then prints that string in <textarea> field of .aspx page.
My question is there any way in which after that | separator next column comes in next line in textarea? Just like pressing Enter key?
Code:
dtOutput = Generix.getData("dbo.EventMsg E Left Join dbo.ATMStatusHistory A On E.Code=A.Fault", "Distinct E.Fault_Short_Name", "A.Code In (" + sFaultNumber + ") And IsNull(Fault_Short_Name,'')<>''", "", "", 1);
sOtherFaults = "";
foreach (DataRow drOutput in dtOutput.Rows)
{
foreach (DataColumn dcOutput in dtOutput.Columns)
{
sOtherFaults += ((sOtherFaults == "") ? "" : ":") + Convert.ToString(drOutput[dcOutput]);
}
}
sOutput += "|" + sOtherFaults + "|" + sClosedFault + "|" + sTemp + "|";
Response.Write(sOutput);
Try:
string.Join(#"|\r\n", sOutput, sOtherFaults, sClosedFault, sTemp);
I dont really understand what you mean but try this
sOutput += "|" + sOtherFaults + "|" + sClosedFault + "|" + sTemp + "|";
Response.Write(sOutput.Replace("|", "\r\n"));
Or
sOutput += "|" + sOtherFaults + "|" + sClosedFault + "|" + sTemp + "\r\n";
Response.Write(sOutput);
I think you'd be better off using a StringBuilder here, like this.
var responseString = new StringBuilder(sOutput);
resposeString.AppendLine("|");
foreach (DataRow drOutput in dtOutput.Rows)
{
foreach (DataColumn dcOutput in dtOutput.Columns)
{
resposeString.AppendFormat("{0}:", Convert.ToString(drOutput[dcOutput]));
}
}
// Remove last : delimiter
responseString.Remove(responseString.Length - 1, 1);
resposeString.AppendLine("|");
resposeString.Append(sClosedFault);
resposeString.AppendLine("|");
resposeString.Append(sTemp);
Response.Write(responseString.ToString());
Like String.Format this will save lots of intermediate string instantiations but, also fits nicely in the loop structure.
I had the following:
string Name = name.First + " " + name.Last;
This returns Tom Jones just fine.
In case name.First may be null or name.Last may be null, I have the following:
string SpeakerName = name.First ?? string.Empty + " " + name.Last ?? string.Empty;
What is strange is that it only returns Tom. Why is this and how can I fix it such that if null it defaults to empty string for either first or last name?
Because of the relative precedence of the ?? and + operators. Try this:
string SpeakerName = (name.First ?? "") + " " + (name.Last ?? "");
Your original example is evaluating as if it was:
string SpeakerName = name.First ?? ("" + " " + (name.Last ?? ""));
Also, read Jon's answer here: What is the operator precedence of C# null-coalescing (??) operator?
As he suggests there, this should work as well:
string SpeakerName = name.First + " " + name.Last;
Because that compiles to #L.B.'s answer below, minus the trim:
string SpeakerName = String.Format("{0} {1}", name.First, name.Last)
EDIT:
You also asked that first and last both == null makes the result an empty string. Generally, this is solved by calling .Trim() on the result, but that isn't exactly equivalent. For instance, you may for some reason want leading or trailing spaces if the names are not null, e.g. " Fred" + "Astair " => " Fred Astair ". We all assumed that you would want to trim these out. If you don't, then I'd suggest using a conditional:
string SpeakerName = name.First + " " + name.Last;
SpeakerName = SpeakerName == " " ? String.Empty : SpeakerName;
If you never want the leading or trailing spaces, just add a .Trim() as #L.B. did
string SpeakerName = String.Format("{0} {1}", name.First, name.Last).Trim();
string SpeakerName = name.First != null && name.Last != null
? string.Format("{0} {1}", name.First, name.Last)
: string.Empty;
string fullName = (name.First + " " + name.Last).Trim();
This works for either or both being null and will not return a string with leading, trailing, or only spaces.
Below I have two regex's that operate on some text:
assume key = "old" and value = "new"
text = Regex.Replace(text,
"\\." + change.Key + ",",
"." + change.Value + ","
);
text = Regex.Replace(text,
"\\." + change.Key + ";",
"." + change.Value + ";"
);
So, ".old," and ".old;" would change to ".new," and ".new;", respectively.
I'm sure this could be shortened to one regex. How can I do this so that the string only changes when the comma and semicolon are at the end of the variable? For example, I don't want ".oldQ" to change to ".newQ". Thanks!
.NET uses $ for backreferences:
text = Regex.Replace(text,
#"\." + change.Key + "([,;])",
"." + change.Value + "$1");
Out of my head:
text = Regex.Replace(text, #"\.(old|new),",#"\.\1;");
You want to just change the middle part, so:
text = Regex.Replace(text,
"\\." + change.Key + "(,|;)^", // mark a group using "()" for substitution...
"." + change.Value + "\1" // use the group ("\1")
);
I like using \b, like this:
text = Regex.Replace(text, #"\." + change.Key + #"\b", "." + change.Value);
It would match on keywords followed by other delimiters, not just "," and ";", but it may still work in your case.