.NET String Assignment Removes Characters [closed] - c#

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
Never seen anything like this before. I'm doing a very simple DataReader value assignment.
story.Byline = _r["Byline"].ToString();
But the values of _r["Byline"].ToString() and story.Byline are different after the assignment. Here's the output from the Immediate window:
story.Byline
"Associated Press - FIRST LASTAssociated Press - FIRST LAST"
_r["Byline"].ToString()
"Associated Press - FIRST LAST<br />Associated Press - FIRST LAST"
Why is <br /> being removed?

Calling reader["x"].ToString() you actually calls x' type possibly overridden method ToString().
If you're sure that it's string, use reader.GetString(reader.GetOrdinal("x"))

Well, this is a bit embarrassing:
public string Byline
{
get { return !_elements.ContainsKey("Byline") ? "" : (string)_elements["Byline"]; }
set
{
string _buf = Functions.StripTags(value);
_elements["Byline"] = _buf;
}
}
Incorrect assumptions FTL. Can this question be deleted?

Related

C# remove text after comma [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
How can I remove specific text from a string?
for example I have this string:
string file = "43 , 2000-12-12 003203";
I need to remove the text after the comma, including comma, so I can have as a final result:
string file = "43";
thank you,
string file = "43 , 2000-12-12 003203";
string number = file.Split(',')[0].Trim();
You can do this:
string output = file.Substring(0, file.IndexOf(',')).Trim();
However, that might fail if the string doesn't contain a comma. To be safer:
int index = file.IndexOf(',');
string output = index > 0 ? file.Substring(0, index).Trim() : file;
You can also use Split as others have suggested, but this overload would provide better performance, since it stops evaluating the string after the first comma is found:
string output = file.Split(new[] { ',' }, 2)[0].Trim();
Possibly by using Split?
file.Split(',')[0].Trim();

C# read a text file and search for string [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In C# how can I open the text file , search for string "mystring" and if string is there then set variable Vara = 1 else Vara= 0 .
Thanks in advance
Quick & dirty using System.IO.File.ReadAllText:
int Vara = File.ReadAllText(path).Contains("mystring") ? 1 : 0;
won't do all your exercise to leave you some fun, but here a way to start:
to get all text of a file into a string variable try this:
using System.IO;
string fileContent = File.ReadAllText(#"C:\file.txt");
then here to check if your string is inside:
bool present = fileContent.IndexOf("mystring") >= 0;

Retrieving Full Computer Name [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I am developing a Windows Application by using C#. I have a form in which I dragged one textBox and one button control. I want to retrieve the user Full Computer Name in the textBox by clicking the button.I wrote some codes for that as..........
private void button1_Click(object sender, EventArgs e)
{
string name = System.Environment.MachineName;
textBox1.Text = name[0].ToString();
}
By clicking the button, it retrieves only the first letter(eg: D for Donald-PC) of the Computer Name by which it starts but I want to retrieve the Full Name(eg: Donald-PC). Please someone help if I have make any change in my codes.Thanks
Just output the full name, not just the 0 index:
textBox1.Text = name;
You are taking the first character of System.Environment.MachineName. You need to do this:
textBox1.Text = System.Environment.MachineName;
As an aside, textBox1 for a control name is really lousy.
You are using char index of array, drop [0] and use just name.

Is multidimension array a good choice for this? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
For my parser i want to (depending on user input) save parse stats for the sites a user chooses to parse.
So lets say the user chooses site A, site B and site D:
I then from my threads want to be able to add different statitsitcs like; links parsed, pages parsed, duplicates found, etc. (so basically int values for the sites).
How would i create this into c#?
My idea is to make some kind of multidimensional array?
Is there a better way?
Any suggestions are very welcome :)
I would create a class that encapsulates the statistics and than use a dictionary with a website name as key.
public class Statistics
{
public int PagesParsed { get; set; }
}
var collection = new Dictionary<string, Statistics>();
collection.Add("Site A", new Statistics { PagesParsed = 42 });
var siteAStatistics = collection["Site A"];

What happens when a null collection is queried with LINQ query syntax? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
My understanding is nothing will happen.
For instance this code:
foreach (var some in (from u in possiblyNullCollection ) )
{
//
}
Should be guarded as:
if ( possiblyNullCollection != null )
{
foreach (var some in (from u in possiblyNullCollection ) )
{
//
}
}
Or is it safe to query a null collection?
A null collection will throw an exception if you query it with LINQ. You need to check for null.
Empty collections are fine however.
Something to keep in mind is that it's generally considered bad practice for collections to be null. Similar to having null items in a collection, it can cause a lot of bugs.

Categories