I'm trying to format a string to a time string format. The PadLeft method is working but after that method I use the Insert method and for some reason it removes the zeros added by the PadLeft:
var formatString = data.Rows[i][j].ToString().PadLeft(6, '0');
formatString = data.Rows[i][j].ToString().Insert(1, ":").Insert(4, ":");
data.Rows[i][n] = formatString;
After the second line of code, the zeros are removed and the colons are added when the Insert method executes.
Hope I explained it thoroughly
String in .Net is immutable. Your first line returns a new string and does not modify the value in the table. So you need to continue working with that string and not the value in the table.
var formatString = data.Rows[i][j].ToString().PadLeft(6, '0');
formatString = formatString.Insert(1, ":").Insert(4, ":");
data.Rows[i][n] = formatString;
You have the zeroes in the formatString variable, and then, in the second line, you are assigning a new value to that variable, I think you want to do something like:
var formatString = data.Rows[i][j].ToString().PadLeft(6, '0');
formatString = formatString.Insert(1, ":").Insert(4, ":");
data.Rows[i][n] = formatString;
Related
I have as input the string format CST-000000 and an integer with value 1
Upon using
string result = string.Format("CST-000000", 1);
the expected result should be CST-000001 instead of CST-000000
How could i create this string format dynamically?
For example
- CST-000 should produce CST-001
- HELLO000 should produce HELLO001
- CUSTOMER0000 should produce CUSTOMER0001
Assuming that:
You receive your format string from somewhere and you can't control what it looks like
Your format string ends with 1 or more zeros
If the format string is e.g. CST-00000 and your value is 123, you want the result to be CST-00123
You can do something like this:
Inspect your format string, and separate out the stuff at the beginning from the zeros at the end. It's easy to do this with Regex, e.g.:
string format = "CST-000000";
// "Zero or more of anything, followed by one or more zeros at the end of the string"
var match = Regex.Match(format, "(.*?)(0+)$");
if (!match.Success)
{
throw new ArgumentException("Format must end with one or more zeros");
}
string prefix = match.Groups[1].Value; // E.g. CST-
string zeros = match.Groups[2].Value; // E.g. 000000
Once you have these, note the "Zero placeholder" in this list of custom numeric format strings -- you can write e.g. 123.ToString("0000") and the output will be 0123. This lets you finish off with:
int value = 123;
string result = prefix + value.ToString(zeros);
See it on dotnetfiddle
String.Format requires a placeholder {n} with a zero-based argument number. You can also add it a format {n:format}.
string result = String.Format("CST-{0:000000}", 1);
You can also use String interpolation
string result = $"CST-{1:000000}"
The difference is that instead of a placeholder you specify the value directly (or as an expression). Instead of the Custom numeric format string, you can also use the Standard numeric format string d6: $"CST-{1:d6}"
If you want to change the format template dynamically, String.Format will work better, as you can specify the format and the value as separate arguments.
(Example assumes an enum FormatKind and C# >= 8.0)
int value = 1;
string format = formatKind switch {
FormatKind.CstSmall => "CST-{0:d3}",
FormatKind.CstLarge => "CST-{0:d6}",
FormatKind.Hello => "HELLO{0:d3}",
FormatEnum.Customer => "CUSTOMER{0:d4}"
};
string result = String.Format(format, value);
Also note that the value to be formatted must be of a numeric type. Strings cannot be formatted.
See also: Composite formatting
It seems .toString("CST-000"), .toString("HELLO000") and so on, does the trick.
ToString and String.Format can do much more than use predefined formats.
For example :
string result = string.Format("CST-{0:000000}", 1);
string result = 1.ToString("CST-000000");
Should both do what you want.
(Of course you could replace "1" by any variable, even a decimal one).
I've see
this post. What if my string is string x = "Tomorrow 04-26-19 09:14AM sunrise.";, basically the datetime value is always in mm-dd-yy hh:mm<AM/PM> format. So I'd need to extract this value of 04-26-19 09:14AM and remember the starting position which = 9.
This works nicely for me:
string x = "Tomorrow 04-26-19 09:14AM sunrise.";
var regex = new Regex(#"\d{2}-\d{2}-\d{2} \d{2}:\d{2}(AM|PM)");
var match = regex.Match(x);
if (match.Success)
{
var prefix = x.Substring(0, match.Index);
var value = DateTime.ParseExact(match.Value, "MM-dd-yy hh:mmtt", System.Globalization.CultureInfo.CurrentCulture);
var suffix = x.Substring(match.Index + match.Length);
}
It's using Regex to find a potential DateTime string and then determines the prefix part of the string, the DateTime value, and the suffix part of the string.
It gives me:
"Tomorrow "
2019/04/26 09:14:00
" sunrise."
If "tomorrow" and "sunshine" aren't necessarily always going to be present in a string like x but the format of this string will be similar, I'd probably split these strings according to the space character like this.
x.Split(' ')
with indices
string strDate = x.Split(' ')[1];
string strTime = x.Split(' ')[2];
& then join those sets of strings
string dt = $"{strDate} {strTime}";
& then probably use the DateTime.ParseExact functionality to craft an actual DateTime object out of what I parsed.
DateTime.ParseExact(dt, "MM-dd-yy hh:mmtt", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.AssumeLocal)
I generally use assumelocal so that I know the time won't be UTC.
"tt" is a placeholder for AM or PM.
If this is something you'll have to do iteratively, then I would probably create a method for this, and then simply call it routinely.
Here is my code:
//I want to find the oldValue
string oldValue = "##OfferTitle##";
//In this string:
stringToReplace = "123##OfferTitle##456";
string newValue = "Test Offer Title";
//and return it in this variable as "123Test Offer Title456"
var newString = stringToReplace .Replace(oldValue, newValue );
return newString ;
The above is not working. All I get back is the original string.
I expect: "123Test Offer Title456"
But I get stringToReplace ("123##OfferTitle##456") unchanged or unmodified. What am I missing?
Please read the documentation carefully String.Replace Method (String, String):
Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string.
Strings are immutable in C#. Original string will stay unchanged.
i have a WPF TextBox that user can type number in that . now i am searching for a string format that can separate TextBox number each 3 point (like 0,0) but i want separate text with Slash or Back Slash or another character. and we do not know how many point our number has.
i am searching for string format not Linq solution or etc . i read Microsoft help but cant find any way .
sample = 123456789 == > 123/456/789 (good) --- 123,456,789 (bad)
Update :
Thanks guys but i search for some thing like this stringformat= {}{0:0,0} etc . i mean dont want use string function like regex , replace or linq or any c# code . i want use a string like {#,#} or etc. see microsoft link in my post i need create a string for my issue.
As the OP insists on the use of String.Format:
string input; //the input of your textbox
int temp = int.Parse(input); //parse your input into an int
//the Format() adds the decimal points, the replace replaces them with the /
string output = String.Format("{0:0,0}", temp).Replace('.', '/');
The important step here is to cast the text of your textbox into an integer, as this simplifies the insertion of the decimal points with String.Format().
Of course, you have to make sure that your textbox is a valid number upon parsing or you may get an exception.
EDIT
So... you have some dynamic-length number and want to format it using a static format-string (as regexes, string replaces, ling or any c# code at all (!) are a no go)? This is impossible.
You HAVE TO have some dynamic code creating a format string somewhere.
Without referencing to regexes or string replaces again, here is some code to create a format string depending on your input number.
This way you have just one String.Format() call. Perhaps you can put the algorithm to create the format string somewhere else and just call it from whereever you need it.
string input; //the input of your textbox
int temp = int.Parse(input); //parse your input into an int
string customString = "{0:";
string tempS = "";
for (int i = 0; i < input.Length; i++)
{
if (i % 3 == 0 && i != 0)
{
tempS += "/";
}
tempS += "#";
}
tempS = new string(tempS.Reverse().ToArray());
customString += tempS;
customString += "}";
string output = String.Format(customString, temp));
You can use a custom NumberFormatInfo. Then use it for ToString with the "n"-format specifier:
NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = "/";
nfi.NumberDecimalDigits = 0; // otherwise the "n" format specifier adds .00
Console.Write(123456789.ToString("n", nfi)); // 123/456/789
You can use NumberFormatInfo.NumberGroupSeparator Property
Sample from MSDN
using System;
using System.Globalization;
class NumberFormatInfoSample {
public static void Main() {
// Gets a NumberFormatInfo associated with the en-US culture.
NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;
// Displays a value with the default separator (",").
Int64 myInt = 123456789;
Console.WriteLine( myInt.ToString( "N", nfi ) );
// Displays the same value with a blank as the separator.
nfi.NumberGroupSeparator = " ";
Console.WriteLine( myInt.ToString( "N", nfi ) );
}
}
/*
This code produces the following output.
123,456,789.00
123 456 789.00
*/
for you - set NumberGroupSeparator property to '/'
UPDATE
another sample
var t = long.Parse("123/456/789",NumberStyles.Any, new NumberFormatInfo() { NumberGroupSeparator = "/" });
var output = string.Format(new NumberFormatInfo() { NumberGroupSeparator="/"}, "{0:0,0}", t);
I have a numeric string like this 2223,00. I would like to transform it to 2223. This is: without the information after the ",". Assume that there will be only two decimals after the ",".
I did:
str = str.Remove(str.Length - 3, 3);
Is there a more elegant solution? Maybe using another function? -I don´t like putting explicit numbers-
You can actually just use the Remove overload that takes one parameter:
str = str.Remove(str.Length - 3);
However, if you're trying to avoid hard coding the length, you can use:
str = str.Remove(str.IndexOf(','));
Perhaps this:
str = str.Split(",").First();
This will return to you a string excluding everything after the comma
str = str.Substring(0, str.IndexOf(','));
Of course, this assumes your string actually has a comma with decimals. The above code will fail if it doesn't. You'd want to do more checks:
commaPos = str.IndexOf(',');
if(commaPos != -1)
str = str.Substring(0, commaPos)
I'm assuming you're working with a string to begin with. Ideally, if you're working with a number to begin with, like a float or double, you could just cast it to an int, then do myInt.ToString() like:
myInt = (int)double.Parse(myString)
This parses the double using the current culture (here in the US, we use . for decimal points). However, this again assumes that your input string is can be parsed.
String.Format("{0:0}", 123.4567); // "123"
If your initial value is a decimal into a string, you will need to convert
String.Format("{0:0}", double.Parse("3.5", CultureInfo.InvariantCulture)) //3.5
In this example, I choose Invariant culture but you could use the one you want.
I prefer using the Formatting function because you never know if the decimal may contain 2 or 3 leading number in the future.
Edit: You can also use Truncate to remove all after the , or .
Console.WriteLine(Decimal.Truncate(Convert.ToDecimal("3,5")));
Use:
public static class StringExtensions
{
/// <summary>
/// Cut End. "12".SubstringFromEnd(1) -> "1"
/// </summary>
public static string SubstringFromEnd(this string value, int startindex)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Substring(0, value.Length - startindex);
}
}
I prefer an extension method here for two reasons:
I can chain it with Substring.
Example: f1.Substring(directorypathLength).SubstringFromEnd(1)
Speed.
You could use LastIndexOf and Substring combined to get all characters to the left of the last index of the comma within the sting.
string var = var.Substring(0, var.LastIndexOf(','));
You can use TrimEnd. It's efficient as well and looks clean.
"Name,".TrimEnd(',');
Try the following. It worked for me:
str = str.Split(',').Last();
Since C# 8.0 it has been possible to do this with a range operator.
string textValue = "2223,00";
textValue = textValue[0..^3];
Console.WriteLine(textValue);
This would output the string 2223.
The 0 says that it should start from the zeroth position in the string
The .. says that it should take the range between the operands on either side
The ^ says that it should take the operand relative to the end of the sequence
The 3 says that it should end from the third position in the string
Use lastIndexOf. Like:
string var = var.lastIndexOf(',');