C# How to find out how long are two different numbers in a string?

c# string to int
c# string length
c# string length limit
c# get all numbers from string
find string length in c# without using length function
online c# compiler
c# if string length is greater than
how to get only numeric values from string c

i want to know how big are the 2 numbers in the string. The difficult thing is that the 2 numbers are variables and i dont know how long they are, they are growing with time. The Program write the variables into a txt file. Now i read all lines of the file and put it into a string. After this i need the two numbers as an int to determine the two variables, to keep counting with them. Example code:

int num1 = 0;
int num2 = 0;
string text = "";

//imagine foreach keypress B num1++ and foreach keypress N num2++
File.WriteAllLines("C:\\ExampleFile", $"Num1 = {num1}, Num2 = {num2}!");

// between this is a programm restart

text = File.ReadAllLines("C:\\ExampleFile");

//now iwant to get the value of num1 and num2 
text.substring(6, num1.length) //something like this idk

num1 = ?
num2 = ?

Hope you will understand :)

Instead of using SubString, you could use Regular Expressions for the purpose. For example,

var regex = new Regex(@"=\s*(?<Number1>\d*),\s*Num2\s*=\s*(?<Number2>\d*)");

var matches = regex.Match(text);
if(matches.Success)
{
    var num1 = Int32.Parse(matches.Groups["Number1"].Value);
    var num2 = Int32.Parse(matches.Groups["Number2"].Value);
}

The regex indicates two groups (indicated by "(?[expression])"), the first consisting of a number preceeded by an '=' and whitespaces. The first group would be followed by a "," & whitespace characters. This is followed by the Second group, is preceeded by the text"Num2" and has the same definition has the first group.

Demo Code

String.Length Property (System), Gets the number of characters in the current String object. public: property int Length { int get(); };. C# Copy. First, you need to find the position of the two strings in the string. Then use first string position as the starting position and find the length of the string by subtracting position of the first string from the position of the second string. The following code snippet retrieves a substring between ‘Henry’ and ‘Beniwal’.

You could print them to Strings using ToString(), then call String.Lenght. While string is not technically a char[] (even if it is propably wrapping one), it can be treated like one. However that might give you the wrong results. The string representation to print out to or parse from, is determined in large parts by the culture of the user as set in Windows.

And aside from differing thousands and decimal seperators, there are even different ideas how to group numbers between cultures - like the Indian Lakh and Crore.

If the question is about the number of digits in base 10, this would not work. However I remember a recent case, where someone figured out how many repeating digits the decimal part of a division had. I think this code would be a proper adaptation to your case:

int current = Input;
int Digits = 1;

while(current >= 10){
  current = current / 10;
  Digits++;
}

How to determine whether a string represents a numeric value, If the string contains nonnumeric characters or the numeric value is too large to use TryParse with string representations of long , byte , and decimal values. Primitive numeric types also implement the Parse static method,� In C#, Max() is a Math class method which is used to returns the larger of the two specified numbers. This method always takes two arguments and it can be overloaded by changing the data type of the passed arguments as follows: Math.Max(Byte, Byte): Returns the larger of the two 8-bit unsigned integers.

You could just split the string on your delimiter characters and use it as an array.

var input = $"Num1 = 123, Num2 = 456!"

//Results in the array { "Num1","123","Num2","456" }
var tokens = input.Split(new char[] { '=', ',', ' ', '!'}, StringSplitOptions.RemoveEmptyEntries);

var num1 = int.Parse(tokens[1]);
var num2 = int.Parse(tokens[3]);

C# Regex.Split, Get Numbers From String, Split and the non-digit Regex metacharacter to get numbers from a string. The const input string has 4 numbers in it: they are one or two digits long. static void Main() { const string input = "There are 4 numbers in this string: 40, 30, and 10. 5. Concatenate string using String.Format method in C# . String.Format() method formats strings in a desired format by inserting objects and variables with specified space and alignments into another strings and literals. It is also often used to format strings into specific formats.

C# data types - working with data types in CSharp, The two fundamental data types in C# are value types and reference types. For example, boolean default value is false, decimal 0, string an empty string "". Computers can practically work only with a subset of integer values, because computers have We often work with multiple data types at once. To simplify the process, we do following: 1) Reverse both strings. 2) Keep adding digits one by one from 0’th index (in reversed strings) to end of smaller string, append the sum % 10 to end of result and keep track of carry as sum/10. 3) Finally reverse the result.

Substring in C#, In other words, a substring is a portion of a string. Get a substring from index to end; Get a substring between two strings. To learn more about C# strings, check out String In C# tutorial to learn more about strings and how to work The second parameter is the number of characters including whitespaces. A string is an object of type String whose value is text. Internally, the text is stored as a sequential read-only collection of Char objects. There is no null-terminating character at the end of a C# string; therefore a C# string can contain any number of embedded null characters ('\0').

Comparing Strings and Portions of Strings (The Java™ Tutorials , See JDK Release Notes for information about new features, enhancements, and The String class has a number of methods for comparing strings and portions of strings. Compares two strings lexicographically, ignoring differences in case. In C#, Min() is a Math class method which returns the smaller of the two specified numbers. This method always takes two arguments and it can be overloaded by changing the data type of the passed arguments as follows: Math.Min(Byte, Byte): Returns the smaller of the two 8-bit unsigned integers. Math.Min(Decimal, Decimal): Returns the smaller of

Comments
  • Yeah, thank you. Could you explain the regex pattern :) I dont understand it :/
  • @Luuke I have added a brief description. Please let me know if you need more