I have a string of telephone number. The format I wanted to use is like this
(044) 463-2256
How can I format my string like this?
string tel = "0444632256";
var match = Regex.Replace(tel, #"");
I tried using Regex.Replace but I don't know what the format that I will put in.
If the Length of the string is always constant(10) , then wecan use the below method to format
string tel = "0444632256";
var format = $"({tel.Substring(0, 3)}) {tel.Substring(3, 3)}-{tel.Substring(6, 4)}";
Console.WriteLine(format);
Try this code:
string tel = "0444632256";
tel=String.Format("{0:(###) ###-####}", Int64.Parse(tel));
Output:
(44) 463-2256
You can see below answer:
Convert.ToInt64(tel).ToString("###-###-#### ####")
How to format a string as a telephone number in C#
Related
For one method I have the parameters take a string, but I need that string to become a verbatim string once in the method.
Here is an example of what I mean.
//the string I have
string test = "c:\\documents\\testfile\\test.txt"
//the string I want
string converted = #"c:\\documents\\testfile\\test.txt"
how do I go about converting this using the test identifier?
I have tried:
string test = "c:\\documents\\testfile\\test.txt"
string converted = #test;
string converted = #+test;
string converted = #""+test;
Is there a way I can do this? Thanks
You can't use the verbatim identifier like you are trying to. You'll have to do something along the lines of:
string test = "c:\\documents\\testfile\\test.txt";
string converted = test.Replace(#"\", #"\\");
How would I go about getting just the date from the following string?
"DateOfTest_01-30-2018-1_003"
This string is in position 8 in a CSV file, which I am looping through and parsing. What I have is:
while (!reader.EndOfStream) {
var splitLine = reader.ReadLine().SplitCommaSeparatedValues();
sample.RunDate = splitLine[8];
WriteLog("Run Date = " + sample.RunDate);}
So I need to extract characters from the string that fall between "_" and "-1" and convert the result to /mm/dd/yyyy format.
Thanks in advance for any assistance!
Better will be regular expression in this case: "(DateOfTest_)(\d{2}-\d{2}-\d{4})(-\d_\d{3})". Second group will be date. In c# you can use Regex.Match. MSDN
Use DateTime.ParseExact:
var culture = System.Globalization.CultureInfo.InvariantCulture;
var strToParse = splitLine[8].Substring(11, 10);
var date = DateTime.ParseExact(strToParse, "MM-dd-yyyy", culture);
var formattedStr = date.ToString("MM/dd/yyyy", culture);
You could use Regex matching to determine date string in the input.
var pattern = #"(?<=_)(.*?)(?=-1)";
var input = "DateOfTest_01-30-2018-1_003";
if (Regex.IsMatch(input, pattern))
{
var dateStr = Regex.Match(input, pattern);
var date = DateTime.ParseExact(dateStr.Value, "MM-dd-yyyy",null);
}
Unless you absolutely need the data at just that exact moment move your date parser outside of your reader.
After that, the answer really relies on whether or not the string in that field is always formatted the same way.
As others have pointed out, if the string is always of the same format you can substring the date out of the string. Then you can either do use one of the several built-in date format methods, or since it is formatted correctly, do a string.Replace("-", "//")
If the string format changes you'll need to try some regex to help you identify the substring to pull out.
My biggest point is that I think you should do this formatting of your field outside of your reader.
string TestString = "DateOfTest_01-30-2018-1_003";
Regex TestRegex = new Regex(#"(DateOfTest_)(\d{2}-\d{2}-20\d{2})(-\d_\d{3})");
string ExactDateFormat = "MM-dd-yyyy";
if (TestRegex.IsMatch(TestString))
{
Date = TestRegex.Match(TestString).Groups[2].ToString();
Date = DateTime.ParseExact(Date, ExactDateFormat, null).ToShortDateString();
}
I want to show my string in a special format.
For example I have a string like this s = "00012345"; and want to show it in this format 000(123-45)
I use this code for it:
label1.text = string.format("{0,###(###-##)}",s);
But just see this for result: 00012345.
What string formatting should I be using?
As mentioned you can't format an already existing string. First you need to convert the string to an int:
int num = Int32.TryParse(s);
Then you can do the formatting whilst converting it back into a string:
label1.text = num.ToString("000(000-00)");
string s = "00012345";
string s1 = s.Substring(0,3);
string s2 = s.Substring(3,3);
string s3 = s.Substring(6);
s = string.Format("{0}({1}-{2})", s1,s2,s3);
Numeric
string a = String.Format("{0:#/#}",12)
output:1/2
But How to Convert it for string Suppose
String b=String.Format("{0:###-#}","test")
Output : test
Expected Output: tes-t
You can't format a string through string.Format for the reason that it is already a string.
If you want to manipulate the string, use the methods defined on the string class.
I think you can split the string to its characters and then use format
string testString = "test";
string formattedString = string.Format("{0}{1}{2}-{3}", testString.Select(c => c.ToString()).ToArray());
I've a predefined string format. For instance '>>>,>>>,>>9.99' this means that the system should display string in this '500,000,000.10'. The format can change based on the users using it. How can I write a common function to display stings on the given format passing
the input value and the format as the parameter using C#
You can use the ToString method with a standard or custom format string
For example:
string format = "{0:000,000,000.00}";
string val = 12.3456;
Console.WriteLine(string.Format(format, value)); // it prints "000,000,123.23"
You can read more about formating values here http://www.csharp-examples.net/string-format-double/
decimal value = 1.2345;
string rounded = value.ToString("d2");
private string sDecimalFormat = "0.00";
decimal d = 120M;
txtText.Text = d.ToString(sDecimalFormat);
You could then have a setting for decimal format eg:
txtText.Text = d.ToString(Settings.DecimalFormat);
String.formate can be used for formating.
Go there if you want examples
http://www.csharp-examples.net/string-format-double/
I think the following might work:
String result = String.Format(fmt.Replace('>', '#').Replace('9', '0'), inpString);
fmt being the format you want to use and inpString being the string entered by the user.
Just replace the > with # and the 9 with 0 and it'll be a valid .Net formatstring.
There is a Format method on String.
String.Format("{0:X}", 10); // prints A (hex 10)
There are several methods to format numbers, date...
I dont seem to understand how you can make 500,000,000.10 from >>>,>>>,>>9.99' but I believe the answer would be
But I assume something you are looking for is: string.Format("500,000,00{0:0.##}", 9.9915)
You can then make a method like
Public string GetString(string Format, object value)
{
return string.Format(Format, value);
}
Something like this?