Convert string with leading zeros to a number [closed] - c#

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 3 years ago.
Improve this question
I've got a bit of a challenge. I have a string which contains of 11 characters, the first 10 denotes an amount, and the last one is a letter or special letter, which represents a value to add.
For example if I got
0000000400A
this means
400 + 2
So the result should be
402
I can't figure out how to do this. Any ideas?

Something like this?
// Setup
var dict = new Dictionary<char, int>();
dict.Add('A', 2);
dict.Add('B', 3);
dict.Add('C', 4);
dict.Add('D', 5);
var str = "0000000400A";
// This is what you need
var result = Convert.ToInt32(str.Substring(0, 10)) + dict[str[10]];
Console.WriteLine(result);

Related

Mutiline a random string [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 2 years ago.
Improve this question
I am a novice in this programming language so now I have a few problems. I have a bill where I need it to find the "/" character and then the string must be automatically carriage return
For example, I have the following string of characters:
BL4444 / BL5555 / BL6666
I need it to be:
BL4444 /
BL5555 /
BL6666
Replace / (slash and space) with / and new line:
String.Replace and Environment.NewLine:
var str = "BL4444 / BL5555 / BL6666";
str = str.Replace("/ ", "/" + Environment.NewLine);

Split string on every 500 values with comma [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 4 years ago.
Improve this question
how to split this string on every 500 values ending in comma
string val= "1,2,3,4,5,.....................,2000,2001";
//Solution
[1,2,3,...,500]
[501,502,503,...,1000]
[1001,1002,...,1500]
You can match the string with the following regex instead (where 499 is 500 minus 1):
(?:[^,]+,){0,4}[^,]+
Demo (for splitting at every 5 commas here): https://regex101.com/r/nbRxdv/2
Assuming by "no loops" you mean you're happy to let LINQ use loops internally, maybe something like this:
string s = "your,comma,string";
string[] ss = s.Split(',');
Print500(ss, 0);
private void Print500(IEnumerable<string> ies, int skip)
{
if (skip > ies.Count())
return;
Console.Out.WriteLine(string.Join(",", ies.Skip(skip).Take(500)));
Print500(ies, skip + 500);
}
I haven't run it, so it might have some minor issues..

Get String Between First n Character and Last n Charcter in C# [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 4 years ago.
Improve this question
My String: draw.text("hello",11,22);
Need Output: 11
So i need to get a String Between first , character and Last , character.
Its not duplicate question.I not found same questions.
I need to get the 2nd argument in my string,the string between first , and last , in string
Split your string on the , character, remove the first and last item, and join the remaining components back together:
var input = "draw.text(\"hello\",11,22);";
var components = input.Split(',');
var result = String.Join(",", components.Skip(1).Take(components.Length - 2));
// yields 11
Note: using System.Linq is required for Skip and Take.

How can i generate unique number as format XXXX-XXXX-XXXX using c# [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 5 years ago.
Improve this question
I want to generate number as format above and I try to use UUID() and GUID() but it is not what I want(difference format and it is hexadecimal not number)
anyone have any idea or logic to do it?
The simplest way to generate 1000000000000 unique numbers is just an ever-increasing sequence:
long i = 42; // whatever
string key = $"{i / 100000000:0000}-{(i / 10000) % 10000:0000}-{i % 10000:0000}";
The next time, increase i before generating the key.

Count how many characters [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 usually never use char so sorry if it seems a noob question. I'm doing a Crypter, and wanted to do "if there's at least 4 differents char, then crypt, else don't crypt and return error"
Actual Code:
string msg = Console.ReadLine();
char[] msgToChar = msg.ToCharArray();
if(here is the problem)
{
Console.WriteLine("Crypted message: " + Crypt(msg.ToUpper()));
}
else
{
Console.WriteLine("Please enter at least 4 differentes characters.");
}
You could do this to get the distinct count of characters.
msgToChar.Distinct().Count()
So your if would be something like
if (msgToChar.Distinct().Count() >= 4)

Categories