Prefix '0' to the int variable which less than '10', How? - string

Is there any simple method to concatenate '0' before an int variable.
Like:
int i = 2;
// produce
i = someMethod(i);
// output:
i = 02

If you mean "concatenate", then you can define someMethod() as follows:
string someMethod(int i){
return string.Format("{0:d2}", i);
}
The "2" in the string format defines the number of characters in the output.

Related

C# How to find substring in string?

I have this data:
AC level : FAIL
ADC AC - 0x0440
AC level : FAIL
Average ADC Batt - 0x733e
I want to get the Adc Ac value for example the 0x0440 but when I want to store it in the database ,the output will display 0x737eBatt Level : MidADC but it happened intermittently.
Here is my code:
public static string getBetween(string strSource, string strStart, string strEnd)
{
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
int Start, End;
Start = strSource.IndexOf(strStart, 1) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}
return "";
}
// Find ADC value
string ADC = getBetween(serialdata, "-", "AC");
You should probably try to extract the values by their key, rather than relying on characters before and after. In your example the string "AC" already appears twice before the text that you are interested in. Try a function like the following, to get any value:
static bool TryGetValue(string serialData, string key, out string value)
{
var m = Regex.Match(serialData, $".*{Regex.Escape(key)}\\s*[-:](?<value>.*)$", RegexOptions.Multiline);
value = m.Groups["value"].Value.Trim();
return m.Success;
}
you'd call it like this:
if (TryGetValue(serialData, "ADC AC", out var value))
{
Console.WriteLine(value);
}

Arduino does not return the desired output via serial port

I would like to send a list of elements inside a structure via serial port but the output produced by Arduino is abnormal.
A little help? What is the reason for this abnormal output?
const int menu_max_item = 20;
int menu_num_item = 0;
typedef struct item_menu{
String text;
void (*func)(void);
} t_item_menu;
t_item_menu arr_menu[menu_max_item];
void menu_add_item(String txt, void (*f)(void)){
arr_menu[menu_num_item].text = txt;
arr_menu[menu_num_item].func = f;
menu_num_item++;
}
void fn_nd_function(){
Serial.println('test');
}
void print_menu_lcd(){
for(int x = 0; x < 4 && x < menu_num_item; x++){
lcd.setCursor(0,x);
lcd.print(arr_menu[x].text);
}
}
void setup(){
Serial.begin(9600);
for(int i = 0; i < 2; i++) menu_add_item("item " + i, fn_nd_function);
}
void loop() {
print_menu_lcd();
delay(1000);
}
Real output
item
tem
em
Desired output
item 1
item 2
item 3
You have a couple of errors...
This code:
void fn_nd_function(){
Serial.println('test');
}
test is NOT a single character is it? So why do you have it in single quotes?
But more importantly this which is the cause of your bad output:
menu_add_item("item " + i, fn_nd_function);
"item" + i is NOT how you concatenate a number to the end of the character string "item". This is C++ not Java or Python. You'll have to build that string separately. Please don't be tempted to use the String class as that can cause other issues.
What is happening now is that you are passing "item" which is a pointer to the character array stored somewhere in memory holding the characters 'i', 't', 'e' and 'm'. When you add 1 to that pointer you end up with a pointer pointing to the 't' and when you add 2 you end up with a pointer pointing to the 'e'. So when you print from those pointers you only get the part after what that pointer points to.
You need to have a line ahead of that to build the string first. Something along the lines of:
char str[7] = "item "; // Note the two spaces to leave room for the digit
str[5] = i + '0'; // Add '0' to convert single digit to ascii
menu_add_item(str, fn_nd_function);

Dynamic character generator; Generate all possible strings from a character set

I want to make a dynamic string generator that will generate all possible unique strings from a character set with a dynamic length.
I can make this very easily using for loops but then its static and not dynamic length.
// Prints all possible strings with the length of 3
for a in allowedCharacters {
for b in allowedCharacters {
for c in allowedCharacters {
println(a+b+c)
}
}
}
But when I want to make this dynamic of length so I can just call generate(length: 5) I get confused.
I found this Stackoverflow question But the accepted answer generates strings 1-maxLength length and I want maxLength on ever string.
As noted above, use recursion. Here is how it can be done with C#:
static IEnumerable<string> Generate(int length, char[] allowed_chars)
{
if (length == 1)
{
foreach (char c in allowed_chars)
yield return c.ToString();
}
else
{
var sub_strings = Generate(length - 1, allowed_chars);
foreach (char c in allowed_chars)
{
foreach (string sub in sub_strings)
{
yield return c + sub;
}
}
}
}
private static void Main(string[] args)
{
string chars = "abc";
List<string> result = Generate(3, chars.ToCharArray()).ToList();
}
Please note that the run time of this algorithm and the amount of data it returns is exponential as the length increases which means that if you have large lengths, you should expect the code to take a long time and to return a huge amount of data.
Translation of #YacoubMassad's C# code to Swift:
func generate(length: Int, allowedChars: [String]) -> [String] {
if length == 1 {
return allowedChars
}
else {
let subStrings = generate(length - 1, allowedChars: allowedChars)
var arr = [String]()
for c in allowedChars {
for sub in subStrings {
arr.append(c + sub)
}
}
return arr
}
}
println(generate(3, allowedChars: ["a", "b", "c"]))
Prints:
aaa, aab, aac, aba, abb, abc, aca, acb, acc, baa, bab, bac, bba, bbb, bbc, bca, bcb, bcc, caa, cab, cac, cba, cbb, cbc, cca, ccb, ccc
While you can (obviously enough) use recursion to solve this problem, it quite an inefficient way to do the job.
What you're really doing is just counting. In your example, with "a", "b" and "c" as the allowed characters, you're counting in base 3, and since you're allowing three character strings, they're three digit numbers.
An N-digit number in base M can represent NM different possible values, going from 0 through NM-1. So, for your case, that's limit=pow(3, 3)-1;. To generate all those values, you just count from 0 through the limit, and convert each number to base M, using the specified characters as the "digits". For example, in C++ the code can look like this:
#include <string>
#include <iostream>
int main() {
std::string letters = "abc";
std::size_t base = letters.length();
std::size_t digits = 3;
int limit = pow(base, digits);
for (int i = 0; i < limit; i++) {
int in = i;
for (int j = 0; j < digits; j++) {
std::cout << letters[in%base];
in /= base;
}
std::cout << "\t";
}
}
One minor note: as I've written it here, this produces the output in basically a little-endian format. That is, the "digit" that varies the fastest is on the left, and the one that changes the slowest is on the right.

How do I parse a string into a number with Dart?

I would like to parse strings like 1 or 32.23 into integers and doubles. How can I do this with Dart?
You can parse a string into an integer with int.parse(). For example:
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10.
You can parse a string into a double with double.parse(). For example:
var myDouble = double.parse('123.45');
assert(myDouble is double);
print(myDouble); // 123.45
parse() will throw FormatException if it cannot parse the input.
In Dart 2 int.tryParse is available.
It returns null for invalid inputs instead of throwing. You can use it like this:
int val = int.tryParse(text) ?? defaultValue;
Convert String to Int
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
print(myInt.runtimeType);
Convert String to Double
var myDouble = double.parse('123.45');
assert(myInt is double);
print(myDouble); // 123.45
print(myDouble.runtimeType);
Example in DartPad
As per dart 2.6
The optional onError parameter of int.parse is deprecated. Therefore, you should use int.tryParse instead.
Note:
The same applies to double.parse. Therefore, use double.tryParse instead.
/**
* ...
*
* The [onError] parameter is deprecated and will be removed.
* Instead of `int.parse(string, onError: (string) => ...)`,
* you should use `int.tryParse(string) ?? (...)`.
*
* ...
*/
external static int parse(String source, {int radix, #deprecated int onError(String source)});
The difference is that int.tryParse returns null if the source string is invalid.
/**
* Parse [source] as a, possibly signed, integer literal and return its value.
*
* Like [parse] except that this function returns `null` where a
* similar call to [parse] would throw a [FormatException],
* and the [source] must still not be `null`.
*/
external static int tryParse(String source, {int radix});
So, in your case it should look like:
// Valid source value
int parsedValue1 = int.tryParse('12345');
print(parsedValue1); // 12345
// Error handling
int parsedValue2 = int.tryParse('');
if (parsedValue2 == null) {
print(parsedValue2); // null
//
// handle the error here ...
//
}
void main(){
var x = "4";
int number = int.parse(x);//STRING to INT
var y = "4.6";
double doubleNum = double.parse(y);//STRING to DOUBLE
var z = 55;
String myStr = z.toString();//INT to STRING
}
int.parse() and double.parse() can throw an error when it couldn't parse the String
Above solutions will not work for String like:
String str = '123 km';
So, the answer in a single line, that works in every situation for me will be:
int r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), '')) ?? defaultValue;
or
int? r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), ''));
But be warned that it will not work for the below kind of string
String problemString = 'I am a fraction 123.45';
String moreProblem = '20 and 30 is friend';
If you want to extract double which will work in every kind then use:
double d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), '')) ?? defaultValue;
or
double? d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), ''));
This will work for problemString but not for moreProblem.
you can parse string with int.parse('your string value');.
Example:- int num = int.parse('110011'); print(num); // prints 110011 ;
If you don't know whether your type is string or int you can do like this:
int parseInt(dynamic s){
if(s.runtimeType==String) return int.parse(s);
return s as int;
}
For double:
double parseDouble(dynamic s){
if(s.runtimeType==String) return double.parse(s);
return s as double;
}
Therefore you can do parseInt('1') or parseInt(1)
void main(){
String myString ='111';
int data = int.parse(myString);
print(data);
}
String age = stdin.readLineSync()!; // first take the input from user in string form
int.parse(age); // then parse it to integer that's it
You can do this for easy conversion like this
Example Code Here
void main() {
var myInt = int.parse('12345');
var number = myInt.toInt();
print(number); // 12345
print(number.runtimeType); // int
var myDouble = double.parse('123.45');
var double_int = myDouble.toDouble();
print(double_int); // 123.45
print(double_int.runtimeType);
}

String Format Issue

I've got the following method:
public static string ReturnFormat(string input, int maxLength, int decimalPrecision, char formatChar)
{
string[] format = new string[2];
string[] inputs = new string[2];
inputs = input.Split(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0]);
if (input.Length > maxLength)
{
int offset = 0;
int counter = 0;
if (inputs[0].Length > maxLength - (1 + decimalPrecision))
{
offset = maxLength - (1 + decimalPrecision);
}
else
offset = inputs[0].Length;
for (int i = 0; i < offset; i++)
{
format[0] += formatChar;
if (counter < decimalPrecision)
{
format[1] += '0';
counter++;
}
}
System.Windows.Forms.MessageBox.Show("{0:" + format[0] + "." + format[1] + "}");
return String.Format(CultureInfo.CurrentCulture, "{0:" + format[0] + "." + format[1] + "}", input);
}
else
return input;
}
Which say I'm using as:
ReturnFormat("12.3456789011243", 10, 2, '#') // format is {0:##.00} // output 12.3456789011243
ReturnFormat("12345678901.1243", 10, 2, '#') // format is {0:#######.00} // output 12345678901.1243
Now my issue is that the input string is not formatted well, still the format strig appears to be ok.
Any ideas of what I'm doing wrong?
Your input is a String not a Double, so it gets formatted like a string: the formatting does not know about decimal places in that case.
You could use Double.Parse() to transform the string into a Double value, but take care of using the right culture.
Another thing, is there a specific reason for not using the more natural format {0:0.00} in both cases? If you really mean to use a placeholder for digits then # is ok, otherwise 0 is best.
Tested solution (beware it truncates and does not round) I needed some time to understand what was actually wanted:
public static string ReturnFormat(string input, int maxLength, int decimalPrecision)
{
if (input.Length <= maxLength)
return input;
Char separator = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];
string[] inputs = input.Split(separator);
// NB: truncating rather than rounding
if (inputs[1].Length > decimalPrecision)
inputs[1] = inputs[1].Substring(0, decimalPrecision);
int digits = (maxLength - decimalPrecision - 1);
// NB: truncating rather than rounding, adding ~ to signalize the
// presence of missing significant digits
if (inputs[0].Length > digits)
inputs[0] = inputs[0].Substring(0, digits-1) + "~";
return inputs[0] + separator + inputs[1];
}

Resources