This question already has answers here:
How can I parse the int from a String in C#?
(4 answers)
Closed 3 months ago.
I have this line of code
bool containsInt = "Sanjay 400".Any(char.IsDigit)
What I am trying to do is extract the 400 from the string but
Any(char.IsDigit)
only returns a true value. I am very new to coding and c# especially.
As you already found out, you cannot use Any for extraction.
You would need to use the Where method:
List<char> allInts = "Sanjay 400".Where(char.IsDigit).ToList();
The result will be a list containing all integers/digits from your string.
Ok if you are interested in the value as integer you would need to convert it again to a string and then into an integer. Fortunately string has a nice constructor for this.
char[] allIntCharsArray = "Sanjay 400".Where(char.IsDigit).ToArray();
int theValue = Convert.ToInt32(new string(allIntCharsArray));
If you are using .NET 5 or higher you could also use the new cool TryParse method without extra string conversion:
int.TryParse(allIntCharsArray, out int theValue);
int result = int.Parse(string.Concat("Sanjay 400".Where(char.IsDigit)));
Use the Regex
var containsInt = Convert.ToInt32(Regex.Replace(#"Sanjay 400", #"\D", ""));
Regular expressions allow you to take only numbers and convert them into integers.
Related
This question already has answers here:
String Interpolation vs String.Format
(7 answers)
Closed 5 years ago.
I'm a beginner to C#. So far I came across several ways that I can use to embed variables in a string value. One of the is String Interpolation which was introduced in C# 6.0. Following code is an example for String Interpolation.
int number = 5;
string myString = $"The number is {number}";
What I want to know is whether there is a benefit of using String Interpolation over the following ways to format a string.
// first way
int number = 5;
string myString = "The number is " + number;
//second way
int number = 5;
string myString = string.Format("The number is {0}", number);
The first way that you have shown will create multiple strings in memory. From memory I think it creates the number.ToString() string, the literal "The number is " string and then the string with name myString
For the second way that you show it's very simple: String interpolation compiles to the string.Format() method call that you use.
EDIT: The second way and the interpolation will also support format specifiers.
A more detailed discussion by Jon Skeet can be found here: http://freecontent.manning.com/interpolated-string-literals-in-c/
This question already has answers here:
How to get the last five characters of a string using Substring() in C#?
(12 answers)
Closed 8 years ago.
I have a string variable like test10015, i want to get just the 4 digits 1001,
what is the best way to do it?
i"m working in asp.net c#
With Linq:
var expected = str.Skip(4).Take(4);
Without Linq:
var expected = str.Substring(4,4);
Select the first four digits in your string:
string str = "test10015";
string strNum = new string(str.Where(c => char.IsDigit(c)).Take(4).ToArray());
You can use String.Substring Method (Int32, Int32). You can subtract 5 from from the length to start from your required index. Make sure the format of string remains the same.
string res = str.Substring(str.Length-5, 4);
string input = "test10015";
string result = input.Substring(4, 4);
This question already has an answer here:
string.Remove doesnt work [duplicate]
(1 answer)
Closed 9 years ago.
I have such code and can't understand where is the mistake, despite the fact, that this code pretty easy. So q is a full path, and I need to get required path to Gen_ParamFile
string q = #"C:\ProgramData\RadiolocationQ\script-Data=12^6-12^33.xml";
string _directoryName1 = #"C:\ProgramData\RadiolocationQ";
int Length = _directoryName1.Length + "ascript".Length;
string Gen_ParamFile = q;
Gen_ParamFile.Remove(0, Length); // this line don't do anything
var Gen_Parfile = Path.Combine(_directoryName1, "GeneralParam-Data" + Gen_ParamFile);
I used function like said here http://msdn.microsoft.com/ru-ru/library/9ad138yc(v=vs.110).aspx
It does, it just doesn't affect the actual string, it creates a new one as a result. Use:
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
Because String.Remove method returns a new string. It doesn't change original one.
Returns a new string in which a specified number of characters in the
current instance beginning at a specified position have been deleted.
Remember, strings are immutable types. You can't change them. Even if you think you change them, you actually create new strings object.
You can assign itself like;
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
As an alternative, you can use String.SubString method like;
Gen_ParamFile = Gen_ParamFile.SubString(Length);
defiantly you can use like
Gen_ParamFile = Gen_ParamFile.Remove(0, Length);
apart from that you can also use
String.Substring method according to your requirment
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to convert numbers between hexadecimal and decimal in C#?
I need to be able to take a hexadecimal string and convert it into actual hexadecimal value in .NET. How do I do this?
For instance, in Delphi, you can take string of "FF" and add the dollar sign as follow to it.
tmpstr := '$'+ 'FF';
Then, convert tmpstr string variable into an integer to get the actual hexidecimal. The result would be 255.
Assuming you are trying to convert your string to an int:
var i = Int32.Parse("FF", System.Globalization.NumberStyles.HexNumber)
Your example 1847504890 does not fit on an int, however. Use a longer type instead.
var i = Int64.Parse("1847504890", System.Globalization.NumberStyles.HexNumber)
Very simple:
int value = Convert.ToInt32("DEADBEEF", 16);
You can do it by following
string tmpstr = "FF";
int num = Int32.Parse(tmpstr, System.Globalization.NumberStyles.HexNumber);
You can also see the link Converting string to hex
int hexval = Convert.ToInt32("FF", 16);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Convert.ToInt32() a string with Commas
i have a value in the label as: 12,000
and i wish to convert it into an integer like 12000 (use it for comparison)
i tried int k = convert.toint32("12,000"); this does not work.
Thanks
You're being screwed up by the comma. If all of your values have commas in them, you'll want to run a string.replace() to remove them. Once that comma is gone, it should work fine.
A more thorough way would be to Parse it, allowing for thousands.
Try the following
var number = Int32.Parse("12,000", System.Globalization.NumberStyles.AllowThousands);
Try this
string num = "12,000";
int k = Convert.ToInt32(num.Replace(",",""));
string k = "12,000";
int i = Convert.ToInt32(k.Replace(",", ""));
will work