Looking for SpanIncluding equivalent in C# [closed] - c#

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I'm porting a c++ class to C# and i have a difficulty.
I would like to find an equivalent of SpanIncluding.
Here is my cpp code :
while (Notes.Mid(j,1).SpanIncluding("0123456789").IsEmpty()!=NULL){}
Anyone can help me please ?

I believe SpanIncluding starts matching from the start of the string, stopping when the first non-matching character is found.
So one formulation in the general case would be this:
string match = new string(someString.ToCharArray().
TakeWhile(c => "0123456789".Contains(c)).ToArray());
(or an equivalent using a regular expression).
However, in the example given in the question there's only one character so the whole thing probably boils down to a test of whether this character is >= '0' and <= '9':
while(char.IsDigit(Notes[j])) { ... };

I found the MSDN page for SpanIncluding, and it seems like a ridiculously specific function. I can't really understand what it tries to solve, since it has some strange caveats.
LINQ would be one way of implementing it:
string text = "2334562";
IEnumerable<char> spannedChars = text.TakeWhile(c => "1234567890".Contains(c));
This is a more direct port of SpanIncluding than queen3's option, if I understand the MSDN page correctly, because the result set should stop the minute it hits a character not in the spanning string.

Related

C# taking large negative no. as a positive value [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
To cut to the chase, when performing some calculations within my code i have a result that is something like. 7.6742332E-30, i have this value stored in a double variable for example, double result = 7.6742332E-30;
When i later check whether this value is greater than 0 the result is true, that it is greater than 0, i assume due to the 7.6742332.
So my question is this, why is the E-30 not being considered and how do i resolve this?
Any advice would be great and thanks so much i advance!
7.6742332E-30 is 0.0000000000000000000000000000076742332, which is a positive number.
7.6742332E-30 mathematcally equals 7.6742332 x 10^-30 which is a positive number

How to save all words above 7 letters to a file [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am currently working on a console text analysis program for an assignment.
My problem is, I need to save all user entered words above 7 letters to a text file. The user can enter words by typing in their paragraph or by loading from a text file.
Any ideas on how I can do this ?
Thanks for any help in advance
Without giving you the code, think about what you really need to do.
Read in the entire text.
Split the text based on spaces / punctuation to identify each word. This will be stored in an array.
Test each split string's length.
Write the results to a file.
I'm not sure how you're getting the user-entered words, but you can do a simple LINQ statement to get the words greater than 7 letters:
//get all words into an array (wordArray)
var bigWords = wordArray.Where(w => w.Length > 7).ToArray();
Then you do something with the bigWords array.
I'm not going to do your assignment for you.
You should check functions such as String.Split, String.Length and String.Substring()

FormatNumber for c# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
Im struggling to get the right syntax to use in c# for the vb function FormatNumber...i will say that 'iPremium' is an object as it returns data from a tableadapter.
the value that 'ipremium' holds is 943.4000 and the idea is to only have two decimal places after the '.', i would hope this is acheivable using the right syntax but unfortunately not being a c# expert, it could take a while to figure this out.
here's the vb code:
iPremium = FormatNumber(iPremium, 2, TriState.True)
any idea's on how this is acheivable?
thanks for any idea's and suggestions and excuse the ignorance if this is not worded correctly
var formattedNumber = iPremium.ToString("0.00");
or, if you wish to round the number, instead of just chopping-off precision:
var formattedNumber = Math.Round(iPremium, 2).ToString("0.00").Dump();
Here's a list of the various formats you can use with ToString: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
var roundedNumber = Math.Round(iPremium, 2);
var formattedNumber = String.Format("{0:#.##}", roundedNumber);
I assume the TableAdapter returns a Datatable which can also be used strongly typed:
// first row as example (add using.System.Linq)
double value = table.AsEnumerable().First().Field<double>("iPremium");
Now you can use String.Format or just ToString with a custom format:
string result = value.ToString("0.00");

How to set limit in a regular expression? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have got a regular expression which I want to be able to accept characters upto a minimum length of 11 characters as against 16 which is the case at the moment.
^[A-Za-z]{6}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{2}[A-Za-z]{1}[0-9LMNPQRSTUV]{3}[A-Za-z]{1}$
Can some one please suggest the changes?
Thanks
Each {6} gives you the number of character you're expecting, here 6 for your 1st expression.
If you want a range of number of characters, do something like this : {1,6} and it will accept 1 to 6 characters at the beginning of your regex.
Which will give you from 11 to 16 characters in your Regex.
{n} - preceeding symbol (template) is expected to appear exactly n times
{min,max} - preceeding symbol (template) is expected to appear exactly minimum min times but not more than max times
Hope this helps you)

Long to Hex VB6, recode in c# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I want to code this part of a VB6 application in c#.
How can I change a long into a Hex value?
Public Function longToHex(l As Long) As String
longToHex = Hex(l)
If Len(longToHex) < 4 Then longToHex = String(4 - Len(longToHex), "0") & longToHex
longToHex = Right(longToHex, 2) & Left(longToHex, 2)
End Function
Just format to a padded hex string:
string.Format("{0:X4}", myLong.ToString().Length / 2)
Then transpose the first two characters with the last two.
The VB6 code appears to take the length of sData divided by 2, then converts the length to a Hex string and pads it with 0s to 4 characters if needed. It then transposes the first two characters with the last two.
Seems convoluted -- what is the code supposed to do? Half the length of the string in hex?
This might work: sLen = (sData.length / 2).ToString("X")

Categories

Resources