Transform alpha-numeric strings to int value [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 2 years ago.
Improve this question
I am looking for a way to transform strings of any size to an integer.
For example: "a" - 01; "b" - 02; "c" - 03; "ab" - 0102; "aac" - 010103.
Right now, I am replacing every single char in the string with a value from an array.
Is there a simple, more fancy way to do this?

more fancy
string source = "aac";
string result_string = string.Join("", source.Select(c => (c - 'a' + 1).ToString("00")));
string source = "aac";
int result_int = source.Select(c => (c - 'a' + 1)).Aggregate((a, i) => a * 100 + i);

Related

In c#, how can i generate a set of strings like aa0000 > aa0001 >... > zz9999 [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 1 year ago.
Improve this question
Hi everyone I need to generate all strings from aa0000 to zz9999; where the first two position are only chars from a to z and the last four positions are from 0000 to 9999.
I tried everything I could but I can't find a way to do this.
Thanks in advance!
You can try nested loops, e.g.
Code:
public static IEnumerable<string> Generator() {
for (char a = 'a'; a <= 'z'; ++a)
for (char b = 'a'; b <= 'z'; ++b)
for (int i = 0; i <= 9999; ++i)
yield return $"{a}{b}{i:d4}";
}
...
foreach (string s in Generator()) {
//TODO: Put relevant code here
}
Demo:
Console.WriteLine(string.Join(Environment.NewLine, Generator()
.Skip(1_000_000 - 5) // skip some items
.Take(10))); // then take some items
Console.WriteLine();
Console.Write(Generator().Count()); // how many items do we have?
Outcome:
dv9995
dv9996
dv9997
dv9998
dv9999
dw0000
dw0001
dw0002
dw0003
dw0004
6760000

How do increment an integer that is part of a string at each iteration of loop [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 have a string like this S02E01 and now i want to run it through a loop like 20 times but at each iteration i want to increment the number after 'E' so that we have for example S02E01,S02E02,S02E03 ..S0210,...S02E20. Please help me.
Its really simple by using some string functions: Substring, PadLeft or ToString
string mystring = "S02E00";
var template = mystring.Substring(0, mystring.Length - 2);//template = "S02E"
for(int i = 0;i <= 20; i++)
{
var result = template + i.ToString("00");
Console.WriteLine(result);
}
you could use PadLeft too => var result = template + i.ToString().PadLeft(2, '0');

How can I split a mixed string 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 3 years ago.
Improve this question
Here is sample input and output.
I have strings like as following.
I want to increment string last digit by 1
AVAP001P001 output AVAP001P002
CD009 output CD010
Here's a quick solution that you can work with. You might want to make it more robust, but I went ahead and added applicable comments to describe what is being done.
static void Main(string[] args)
{
var s = "CBC004DS009";
// get the very last index of the character that is not a number
var lastNonNumeric = s.LastOrDefault(c => !char.IsDigit(c));
if (lastNonNumeric != '\x0000')
{
var numericStart = s.LastIndexOf(lastNonNumeric);
// grab the number chunk from the string based on the last character found
var numericValueString = s.Substring(numericStart + 1, s.Length - numericStart - 1);
// convert that number so we can increment accordingly
if (int.TryParse(numericValueString, out var newValue))
{
newValue += 1;
// create the new string without the number chunk at the end
var newString = s.Substring(0, s.Length - numericValueString.Length);
// append the newly increment number to the end of the string, and pad
// accordingly based on the original number scheme
newString += newValue.ToString().PadLeft(numericValueString.Length, '0');
Console.WriteLine(newString);
}
}
}

int permutations in 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 6 years ago.
Improve this question
I want to implement a method with the following signature:
public int Max(int number){ }
The largest number that can be created by a digits of a given number is obtained by ordering the digits from largest to smallest. See the following code for a possible implementation.
public int Max(int number)
{
var numberAsCharArray = number.ToString().OrderByDescending(c => c).ToArray();
var largestNumberAsString = new string(numberAsCharArray);
return Int32.Parse(str);
}
Maybe not most efficient way is to cast to string and order desc
var result = int.Parse(String.Join("", digit.ToString().OrderByDescending(x => x)))

C# if statement within a range [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 need my if statement to return an image if it is within a range. the current code does not work
if (Int32.Parse(Domain_OSUMC_IT_CHECKBOX.Text.Trim()) == 1)
{
Domain_green_Check.Visible = true;
}
else if (Int32.Parse(XP_OSUWMC_IT_LBL.Text.Trim()) >= 1 && <=.9)
{
Domain_green_Check.Visible = true;
This is where im having the trouble
else if (Int32.Parse(XP_OSUWMC_IT_LBL.Text.Trim()) >= 1 && <=.9)
I need to make the image domain_green_check visible if another label Domain_OSUMC_IT_CHCEKBOX is between the values of .9 and 1
You need to fix your syntax and conert the string to decimal
decimal val = decimal.Parse(XP_OSUWMC_IT_LBL.Text.Trim());
else if (val > .9 && val < 1) //though this condition makes nosense since it will never evaluate to TRUE

Categories

Resources