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.
For the start up method of the console application static void Main(string[] args),
assuming that the parameters are passed as below:
helloworld a , b , c, d
What is the best way to pick up a, b, c and d from agrs (without space and comma) ?
string[] s = args[0].Split(new char[] { ' ', ',' },
StringSplitOptions.RemoveEmptyEntries);
var elements = args[0].Split(',').Select(s => s.Trim());
You can use Trim() to remove space and Replace() to replace comma separated values.
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 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;
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 stored my ArrayList into a multidimensional array to be displayed on the richtextbox.
How do I sort an ArrayList/multidimensional array from the smallest value to the biggest and be displayed on rtbx?
The variables are initialized as:
public static ArrayList dataList = new ArrayList();
public static float[,] finalData = new float[superX.var, 8];
superX.var is int 72.
If possible, instead of the deprecated ArrayList, you should use a List object (in the System.Collections.Generic namespace). The List object has sort methods in which you can specify the sort using a Lambda expression, or you can use LINQ (since it implements IEnumerable)
Using a Lambda expression to order by descending:
var dataListSorted = dataList.OrderByDescending(x => x.PropertyName);
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 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?