Getting the last numbers from a string [duplicate] - c#

This question already has answers here:
Select last element quickly after a .Split()
(6 answers)
Extract version number from string(eg : "ver.1.9.0")
(3 answers)
Closed 2 years ago.
Hello I need to get last number from 1.0.0.0 but the number will change so eventually it can be 1.0.0.111
so I have stripped the number from "."
var amount = "1.0.0.23";
var pureAmount = amount.Replace(#".", "");
Console.WriteLine(pureAmount);
and then I have this extension method that returns the number
public static class StringExtension
{
public static string GetLast(this string source, int tail_length)
{
if(tail_length >= source.Length)
return source;
return source.Substring(source.Length - tail_length);
}
}
used like this
Console.WriteLine(amount.GetLast(1));
But what if I don't know how many digits will the last number have? I just need the number behind the last "." is there a way to do the?

You can achieve this using single statement. Try string functions Split and Last
var result = amount.Split('.').Last();

Related

How do I make permanent changes to a variable within for loops so that the variable doesn't reset every iteration? [duplicate]

This question already has answers here:
string.Replace (or other string modification) not working
(4 answers)
Closed 3 years ago.
I want to replace characters in a string iteratively using the string.Replace() function in a for loop however every iteration the string itself resets and no changes made in the previous loop stay. Ending up in the string undergoing no changes overall.
I've tried making the string a class member but that didn't work,
static void Main(string[] args)
{
int i;
string strOne = "abcdefg";
for (i=0; i < (strOne.Length - 1); i++)
{
string c = Convert.ToString(strOne[i]);
strOne.Replace(c, "1");
}
}
I expect the output of 1111111 but instead i get abcdefg.
When you call string.Replace(), it does not modify the string itself. Instead, it returns the string with the replaced characters. Therefore, you need to do this: strOne = strOne.Replace(c, "1");

Using DataTable to evaluate string equation [duplicate]

This question already has answers here:
Evaluating string "3*(4+2)" yield int 18 [duplicate]
(13 answers)
Closed 3 years ago.
I have this TextBox where you can type in something like "1 + 2 + 3". I want the program to evaluate the answer (6 in this case) and assign it to a var. Then output the answer to a TextBlock. Tried the following but I'm getting a red underline under DataTable. It says 'DataTable' does not contain a constructor that takes 0 arguments
using System.Data;
private void ButtonCalculate_Click(object sender, RoutedEventArgs e)
{
string equation = textBoxEquation.Text;
var answer = new DataTable().Compute(equation, "");
textBlockAnswer.Text = answer;
}
I solved this by:
Right click project name in Solution Explorer
Choose Properties
Target the Min version to at least Fall Creators Update

c# - How to count the number of occurences of a string in a ReadOnlyCollection field [duplicate]

This question already has answers here:
count objects of a certain type in a collection and use this as a string in a textbox
(1 answer)
Finding count of particular items from a list
(5 answers)
Get Count in List of instances contained in a string
(6 answers)
Get item count of a list<> using Linq
(3 answers)
count objects that meet certain condition in List-collection
(2 answers)
Closed 5 years ago.
I have a ReadOnlyCollection of a Smartcard type from Microsoft.Clm.Shared.Smartcards namespace.
one of the fields/parameters of the smartcard object is AssingnedUserName.
I need to be able to count how many times a smartcard with the same username exist in the list,
something like:
[Pseudo Code]
int count = (smartcardCollection.AssignedUserName == my String).Count().
I tried to use the ReadOnlyCollection.Tolist() method, but I couldn't find the correct syntax to make it work.
I also found many examples but non for a ReadOnlyCollection object !
what is the best practice for achieving this ?
thanks
David.
just use this
int count = smartcardCollection.Count(s=>s.AssignedUserName == my String);
LINQ Count it takes a function to test each element for a condition
You just need to use the overload of Count or Where ... Count:
int count = smartcardCollection.Count(s => s.AssignedUserName == my String);
or
int count = smartcardCollection.Where(s => s.AssignedUserName == my String).Count();

Add values of a List object in Single line C# [duplicate]

This question already has answers here:
C# List of objects, how do I get the sum of a property
(4 answers)
Calculate SUM of a property in list
(4 answers)
How to sum a set of items in a list that all share a specific property?
(3 answers)
Total sum for an object property value in a list using a lambda function [duplicate]
(2 answers)
Generalized sum-function [duplicate]
(1 answer)
Closed 5 years ago.
Am having a list object. i need to add the values in the object in a single line of code without using foreach or for loop. Is it possible using the linq query??
For eg: am having a list object userCount of length 2. I need to find the TotalManagerCount by adding the ManagerCount values in the list.
public class UserCount
{
public int ManagerCount {get; set;}
public int EngineerCount {get; set;}
}
List<UserCount> userCount = new List<UserCount>();
int TotalManagerCount = ??
int TotalEngineerCount = ??
Thanks in advance
Dinesh.
Use LINQ Sum function:
int TotalManagerCount = userCount.Sum(x=>x.ManagerCount);
int TotalEngineerCount = userCount.Sum(x=>x.EngineerCount);
Use Sum function of Linq
int TotalManagerCount = userCount.Sum(item => item.ManagerCount);
int TotalEngineerCount = userCount.Sum(item => item.EngineerCount);

compiler is printing Strange value in c# [duplicate]

This question already has answers here:
printing all contents of array in C#
(13 answers)
How does the .ToString() method work?
(4 answers)
Closed 6 years ago.
I am using methods in c#. i returned something but compiler is not printing which i am expecting. It is printing (system.string[]). i dont know why Please help on this.
class Program
{
static void Main(string[] args)
{
string[] sub = subjects();
Console.WriteLine(sub);
}
public static string[] subjects()
{
Console.WriteLine("Please Enter How many Subject Do you Want to input");
int limit = System.Convert.ToInt32(Console.ReadLine());
string[] Subjects = new string[limit];
for (int i = 0; i < limit; i++)
{
Console.WriteLine("Please Enter Subject Name " + i);
Subjects[i] = Console.ReadLine();
}
return Subjects;
}
}
The reason that Program is printing system.string[] is because class string[] does not override ToString method, so the default one is used and the default ToString returns type name, not the "value" inside type.
You can use for example String.Join method to build one string from array of strings and put given delimiter between each string:
Console.WriteLine(String.Join(", ", sub));
Console.WriteLine(sub)
wont show you anything of value (it prints the sub type), since you are not referencing any value of the sub array, e.g sub[0]. Try this:
foreach (string s in sub)
{
Console.WriteLine(s);
}
Console.WriteLine(String.Join(", ", sub));

Categories

Resources