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
Related
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.
This question already has answers here:
Get Substring - everything before certain char
(9 answers)
Closed 8 years ago.
Lets say I have a string:
string a = "abc&dcg / foo / oiu";
now i would like the output to be
"abc&dcg"
i have tried:
string output= a.Substring(a.IndexOf('/'));
but it returns the last part not the first part
I have tried trim() as well, but doesn't provide me with the results.
Try this:
string result = a.Split('/')[0].Trim();
The split operation will give you the 3 substrings separated by '/' and you can choose whichever ones you want by specifying the index.
Try this one
string a = "abc&dcg / foo / oiu";
string output = a.Substring(0, a.IndexOf("/"));
Console.WriteLine(output);
It will show
abc&dcg
Try
string output;
if (a.IndexOf('/')>=0) { output = a.Split('/')[0].Trim() };
This wil prevents error case a doesn't contains any /
This question already has answers here:
Convert comma separated string of ints to int array
(9 answers)
Closed 8 years ago.
I have a string in C#. It's blank at the beginning, but eventually it will become something like
public string info12 = "0, 50, 120, 10";
One of you might be thinking, eh? Isn't than an integer array? Well it needs to stay a string for the time being, it must be a string.
How can I convert this string into an string array (Variable info13) so I can eventually reference it into more variables.
info 14 = info13[0];
info 15 = info13[1];
PLEASE NOTE: This is not a duplicate question. If you read the whole thing, I clearly stated that I have an array of strings not integers.
Here are a few options:
1. String.Split with char and String.Trim
Use string.Split and then trim the results to remove extra spaces.
public string[] info13 = info12.Split(',').Select(str => str.Trim()).ToArray();
Remember that Select needs using System.Linq;
2. String.Split with char array
No need for trim, although this method is not my favorite
public string[] info13 = info12.Split(new string[] { ", " }, StringSplitOptions.None);
3. Regex
public string[] info13 = Regex.Split(info12, ", ");
Which requires using System.Text.RegularExpressions;
EDIT: Because you no longer need to worry about spaces, you can simply do:
public string[] info13 = info12.Split(',');
Which will return a string array of the split items.
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 answers here:
Regex for numbers only
(20 answers)
Closed 9 years ago.
I want to check whether a string is only numeric, or is alphanumeric.
For example :
string test = "2323212343243423333";
string test1 = "34323df23233232323e";
I want to check test having number only or not. If the whole string having number means it returns true. Otherwise it returns false.
How can i do this?
bool allDigits = text.All(c => char.IsDigit(c));
Or
bool allDigits = text.All(char.IsDigit);
Unless by "numeric" you include hex numbers? My answer only works for strings that contain only digits, of course.
if the string length is not too long, you may try int.TryParse(string here) or you may write the function yourself by checking every character in the string like
if(MyString[i]-'0'<= 9 && MyString[i]-'0'>= 0)
//then it's a digit, and check other characters this way