How do I make a string only X char and specific? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I don't know if you got me what I'm trying to explain... (English is not my main language).
Ok here, I got this string 590CBC145FA and this one F6EC5CA9A and the only part that I want, no matter how long is the string, is the last eight char like this: CBC145FA 6EC5CA9A. The 590 and F are obsolete to me, like: !S!ZV)+D_?CEFZEZAF = CEFZ!Z#F.
I tried to use serial.Substring(3); only work for the first one: 590CBC145FA = CBC145FA
If I try to use serial.Substring(1); it work for F6EC5CA9A = 6EC5CA9A
But what happen if I get a string very long and I don't know how much the obsolete part is... I can't use serial.Substring(X); because I don't, like I said, how long is the obsolete part.
I only want to get the eight last char of any serial no matter how long is.

You want to get the eight last char of any serial no matter how long is.
string test = "F6EC5CA9A";
string result = test;
if (test.Length >= 8)
result = test.Substring(test.Length-8);
You just need to discover the index of the 8th character before the end of the string. And this could be easily calculated using the Length property of every string.
Of course you need to be sure that your string is at least 8 characters. You don't say what do you want in case this length is less than 8, so I assume that you want the original input back

Related

Beginner trying to program simple change calculator [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm new to coding and need to write a program for C#
the goal is to write a program that prompts the user to enter an amount including decimals. and the program gives the user back the remander as money. ie...
quater dimes nickle pennie
First you want to get the users input and store it in a variable.
The input will be text so you have to convert that to a decimal.
Then you will need to do your calculations. That is probably the part you need help on.
Lets say the user entered in 25 dollars and 43 cents - 25.43.
I would first divide the dollars by 4 to get the number or quarters.
Then go from there. You will likely use mod %.
Have fun, im sure you can figure it out if you try.
I think the problem isn't with your int.Parse, but with the way you were concatenating your Console.WriteLine string. Here's the fix, I'm not sure if that's what you were needing.
When you concatenate object to a string, add {}. Starting with the number 0 add those inside depending on how many object you want to add to the string like so: {0}. Once you're done with the string you can add the objects after the it separated by a comma.
int dollar = 100;
decimal quarter = .25m;
decimal dime = .10m;
decimal pennie = .01m;
Console.WriteLine("tell me how much money you have, make sure you include doolars and cents. ");
string userMoney = Console.ReadLine();
double userMoney1 = double.Parse(userMoney);
Console.WriteLine("that equals, {0} ", userMoney);
Console.ReadLine();

string.contains() vs string.equals() or string == performance [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm returning a string from an API that has a length of 45 characters. There is one word that is unique for one condition that doesn't appear in the other condition.
I'm wondering if using string.contains() is faster performance-wise than comparing the whole string with string.equals() or string == "blah blah".
I don't know the inner workings of any of these methods, but logically, it seems like contains() should be faster because it can stop traversing the string after it finds the match. Is this accurate? Incidentally, the word I want to check is the first word in the string.
I agree with D Stanley (comment). You should use String.StartsWith()
That said, I also don't know the inner working of each method either, but I can see your logic. However "String.Contains()" may still load the entire string before processing it, in which case the performance difference would be very small.
As a final point, with a string length of only 45 characters, the performance difference should me extremely minute. I was shocked when I wrote a junky method to substitute characters and found that is processes ~10kb of text in a fraction of a blink of the eye. So unless you're doing some crazy handling else wise in your app, it shouldn't matter much.

C# Check if a string is a Sentence [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Basically I want to check if a String is a Sentence ("Hello, I am Me!") or Symbol Spam ("HH,,,{''{"), without using the number of symbols as a factor as much as possible. Right now it just detects based on a counter of symbols, but when someone says something with lots of punctuation, they get kicked.
Help?
If the number of symbols in the text is not sufficient, and you don't want to use something too fancy (or bought) could I suggest implementing one or more of these further steps (of increasing difficulty):
Make a count of all A-Za-z and space characters in the string and make a ratio of this to the count of symbols - so if they write a sentence then !!!!!!!!!!!!! at the end it still doesn't snag as the ratio is high enough.
If this still isn't discerning enough, add a further check if you pass item 1...
Count numbers of consecutive A-Za-z characters in the string - work out the average length of these 'words' - if the average is too short then it is probably spam.
These can be done in RegEx reasonably easily - If you want more sophistication then you have to use something written by someone else that has much more developed statistical methods (or start reading lexographical university papers that are beyond me!)

Format a unstructured phone # input in 999-999-9999 format [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am looking for a way to take phone numbers Input from an end user, given the various ways that can be formatted, and reformat it in a more standardized way. My end users work with only US phone numbers. Looking at the C# port of google's LibPhoneNumber, I assume it was made for exactly this task, but I can't figure out its usage.
My goals for output is to standardize on 999-999-9999 format.
So if the user inputs (516)666-1234, I want to output 516-666-1234.
If the users inputs 5166661234, I want to output 516-666-1234.
If possible, if the user inputs (516)666-1234x102, I'd like the output to be 516-666-1234x102
If the library can't handle extensions, I'll deal with that problem externally.
What calls do I have to make to produce the desired output?
Alternatively, can you provide a link that provides the answer to my question?
You have to combine RegEx (to string out non-numeric fields) and string formatting. Note that string.Format of a number is tricky, so we format the 10-digit phone number and then append the extension.
public string getPhoneNumber(string phone) {
string result = System.Text.RegularExpressions.Regex.Replace(phone, #"[^0-9]+", string.Empty);
if (result.Length <= 10) {
return Double.Parse(result).ToString("###-###-####");
} else {
return Double.Parse(result.Substring(0,10)).ToString("###-###-####") +
"x " + result.Substring(10, result.Length-10);
}
}
To do this right, I'd want to check for "1" at the start of my digits and ditch it. I'd want to check for < 10 characters and make some assumptions about area code, etc.
But - this is a good start.

How do I get two numbers between two words (C#) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a string "Building1Floor2" and it's always in that format, how do I cleanly get the building number (e.g. 1) and floor number. I'm thinking I need a regex, but not entirely sure that's the best way. I could just use the index if the format stays the same, but if I have have a high floor number e.g. 100 it will break.
P.S. I'm using C#.
Use a regex like this:
Building(\d+)Floor(\d+)
Regex would be an ok option here if "Building" and "Floor" could change. e.g.: "Floor1Room23"
You could use "[A-Za-z]+([0-9]{1,})[A-Za-z]+([0-9]{1,})"
With those groupings, $1 would now be the Building number, and $2 would be Floor.
If "Building" and "Floor" never changed, however, then regex might be overkill.. you could use a string split
Find the index of the "F" and substring on that.
int first = str.IndexOf("F") ;
String building = str.substring(1, first);

Categories