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;
Related
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();
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.
I have been working with the Sqlite Database on the Windows 8 App.(Sqlite for Windows Runtime)
Turkish character problem when I insert to database.
How can I fix this problem. can you help me?
Thank you very much.
Easy come.
using (var db = new SQLite.SQLiteConnection(App.DbPath))
db.Execute("Insert Into Stock (Name) Values('ŞşİıĞğ')");
i try this but result -> ÞþÝýÐð
string a = "ŞşİıĞğ";
string b = string.Empty;
byte[] utf8Bytes = Encoding.UTF8.GetBytes(a);
b= Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
using (var db = new SQLite.SQLiteConnection(App.DbPath))
db.Execute("Insert Into Stock (Name) Values('"+ b +"')");
Most likely the problem is that your column cannot handle UTF-16 (unicode) properly (could be client API). Safest bet is to convert to/from utf-8 on the client side then read/write to database.
System.Encoding.UTF8 has all the magic.
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 trying to emulate some C++ code in C#. I am not familiar with the intricate workings of C++ and don't quite understand how to implement this code in C#.
Could someone please explain what the functions are doing and what their output would be in ASCII? In particular, I do not understand what the "memcpy" method is doing the way this code is written.
//example values
str = "<Request Type="Query" Version="1.0"></Request>"
uintcrc = getCrc(str, strlen(str));
//code i don't understand
//create a byte array with a null terminator?
memset(strQueryBuffer, '\0', sizeof(str));
//print the values into the byte array
sprintf(strQueryBuffer, "%c%s%c", COMM_STX, str, COMM_ETX);
//append the uintcrc to the end of the byte array?
memcpy(strQueryBuffer + strlen(strQueryBuffer), &uintcrc, sizeof(uintcrc));
it does nothing else than
strQueryBuffer = COMM_STX + "<Request Type='Query' Version="1.0"></Request>" + COMM_ETX + Encoding.Ascii.GetString(BitConverter.GetBytes(uintcrc));
if you have a binary system and want to send the complete information binary, you can also write
var str = "<Request Type='Query' Version="1.0"></Request>";
byte[] Data = (new [] { COMM_STX }).Concat(Encoding.Ascii.GetBytes(str)).Concat(new [] { COMM_ETX }).Concat(BitConverter.GetBytes(uintcrc)).ToArray();
strQueryBuffer + strlen(strQueryBuffer)
is same as
&strQueryBuffer[strlen(strQueryBuffer)]
so it append binary value of crc to the end of strQueryBuffer
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 have a string as below :
...[down]<a title="Download: Click Here" href="http://www.domain.com/T60B8JK2WT/" target="_blank"><strong> blah blah (2011)(1 - 22)</strong></a> <a title="Download: Click Here" href="http://www.domain.com/5FBLQYBQTV/" target="_blank"><strong> blah blah (2011) </strong></a>[/down]...
How can I find [down][/down] tag from string & get all href attribute & text of each link in [down][/down] tag and put it in a data table, with each row of table containing title & url field?
Thanks
Your code should be like this:
foreach (Match match in Regex.Matches(inputString,
#"\[down\](?<content>.+)\[/down\]"))
{
var content = match.Groups["content"].Value;
var hrefs = new List<string>();
foreach (Match matchhref in Regex.Matches(t, #"href=""(?<href>[^""]+)"""))
{
hrefs.Add(matchhref.Groups["href"].Value);
}
}
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?