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();
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 10 years ago.
I need regular expression to retrieve no of doubles, triplets, tetras etc from a telephone number
following is the example,
number is 1001055522
it should return me
group Numbers
=============================
Doubles 00
22
Triplets 555
This regex when used with Regex.Matches will produce exact double or triple (not part of longer consecutive sequence). This is due to the greediness of the quantifier.
(\d)\1+
Demo
Well, the rest is to check the length of the string and count... I will leave it to you.
To find doubles, use a backreference:
(.)\1
Here's a demo: http://regex101.com/r/zC3fM1
To find triplets, just repeat the backreference:
(.)\1{2}
Here's a demo: http://regex101.com/r/cJ4lJ8
If you want to match all consecutive numbers regardless of how many there are, then use + on the backreference:
(.)\1+
Here's a demo: http://regex101.com/r/pL8sB3
Dim n = "1001055522"
Dim doubles = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1")
Dim triples = System.Text.RegularExpressions.Regex.Matches(n, "(.)\1{2}")
'Doubles
For Each d As System.Text.RegularExpressions.Match In doubles
Console.WriteLine(d.Value)
Next
'Triples
For Each t As System.Text.RegularExpressions.Match In triples
Console.WriteLine(t.Value)
Next
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 want to get the integer value of this xml attribute limit=\"25\"
I tried this :Match match = Regex.Match(response.Content, "(?<=limit=))\\d+");
gives me an error : "too many )'s.
and this : Match match = Regex.Match(response.Content, #"limit=([0-9])$"
this returns nothing, the match is not successful
From this xml:
<issues type="array" limit="25" total_count="251" offset="0">
<issue>
<id>4317</id>
Your first regex has too many )s in it. Count them.
Your second is failing because of the quotation marks around the attribute value. Try "limit=\"([0-9])\"$" instead.
Lots of people will tell you to use an XML parser instead. I would strongly recommend that if you're doing anything more than very minor extraction of data from well-known XML, because XML itself isn't parseable with regular expressions.
Regex can be used for parsing XML since it is strict with its format but it is not recommended to use it
Use LINQ2XML
XElement doc=XElement.Parse(response.Content);
var value=doc.Attribute("limit").Value;
OR
var value=Regex.Match(response.Content, #"limit=""(\d+)""",RegexOptions.Singleline).Groups[1].Value;
It's better to use
string TheStringNeededToBeParsed = "... the xml ";
TheStringNeededToBeParsed .IndexOf(...
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?