MVC/C# Put a comma after every number in string - c#

I have a string of 4 numbers:
1234
I want to convert this in the most elegant way possible in MVC to
1,2,3,4
I've tried this:
codeToSend.ToString("#,#");
but this outputs "1,234" (which I expected really).
I suspected that the following would put a comma after every digit, but to no avail.
codeToSend.ToString("#,#,#,#");
I have also tried string.format, but again I am facing the same issue.
var formattedString = string.Format("{0:0,0}", 1234);
Whats the most efficient way of doing this therefore?
Note:
The string of numbers will always be 4 digits long - and numbers only. I don't want to use Insert as this wouldn't be very elegant IMO and I am aware this question has been asked before in similar ways but it is always slightly different in crucial ways (such as formatting to thousands, not every digit or just not elegantly!).

How about just using string.Join?
int i = 1234;
string.Join(",", i.ToString().ToCharArray()); // 1,2,3,4
If 1234 is string, just use;
string s = "1234";
string.Join(",", s.ToCharArray()); // 1,2,3,4
or
string s = "1234";
string.Join(",", s.ToList()); // 1,2,3,4

This one could be more efficient. (But hardly elegant)
var target = "1234";
var result = Regex.Replace(target , #"(\d)(?=.)", "$1,");
Taking fixed string length into account, the same result could be acomplished without lookahead (simpler for reading, and with better efficiency)
var target = "1234";
var result = Regex.Replace(target, #"(\d)(\d)(\d)(\d)", "$1,$2,$3,$4");
Also, if you are processing many such values, you should compile regex before using.
var target = "1234";
var digitExceptLastRegex = new Regex(#"(\d)(?=.)", RegexOptions.Compiled);
var result = regex.Replace(target, "$1,");
But I haven't measured actual performance.

Related

How to parse, actually get number from string in c#?

I am trying to get only part with number. For example in input could be just number like 50, or string like: 50 and more. Also Number always will be on first place. And I want always to get only number from that. I tried like this, but this does not work:
double tendency;
var tendencyToString= Convert.ToString(tendency.ToString());
var tendencySplited = tendencyToString.Split(' ');
var tendencyNumber = tendencySplited[0];
You can extract the number from the string using a regular expression.
See an example below. One thing to pay an attention to are your locale settings as they influence the format of the double.
string pattern = #"^\d+\.\d+";
string input = "50.1 , or more";
Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
if (m.Success) {
double nr = Double.Parse(m.Value);
Console.WriteLine(nr);
}
So if i enter "507.89foo123,456bah" you want only 507 or you want
507.89? –
Please read, I write 50, not 50 with coma or something decimal. I just want 507 in your example
Well, then it's pretty easy:
string input = "50.1 , or more";
char[] digits = input.TakeWhile(Char.IsDigit).ToArray();
if(digits.Any())
{
string result = new string(digits); // "50"
// if you want an integer:
int number = int.Parse(result);
}

How to find one of many possible substrings in a larger string?

I have a simple problem, but I could not find a simple solution yet.
I have a string containing for example this
UNB+123UNH+234BGM+345DTM+456
The actual string is lots larger, but you get the idea
now I have a set of values I need to find in this string
for example UNH and BGM and DTM and so on
So I need to search in the large string, and find the position of the first set of values.
something like this (not existing but to explain the idea)
string[] chars = {"UNH", "BGM", "DTM" };
int pos = test.IndexOfAny(chars);
in this case pos would be 8 because from all 3 substrings, UNH is the first occurrence in the variable test
What I actually trying to accomplish is splitting the large string into a list of strings, but the delimiter can be one of many values ("BGM", "UNH", "DTM")
So the result would be
UNB+123
UNH+234
BGM+345
DTM+456
I can off course build a loop that does IndexOf for each of the substrings, and then remember the smallest value, but that seems so inefficient. I am hoping for a better way to do this
EDIT
the substrings to search for are always 3 letters, but the text in between can be anything at all with any length
EDIT
It are always 3 alfanumeric characters, and then anything can be there, also lots of + signs
You will find more problems with EDI than just splitting into corresponding fields, what about conditions or multiple values or lists?. I recommend you to take a look at EDI.net
EDIT:
EDIFact is a format pretty complex to just use regex, as I mentioned before, you will have conditions for each format/field/process, you will need to catch the whole field in order to really parse it, means as example DTM can have one specific datetime format and in another EDI can have a DateTime format totally different.
However, this is the structure of a DTM field:
DTM DATE/TIME/PERIOD
Function: To specify date, and/or time, or period.
010 C507 DATE/TIME/PERIOD M 1
2005 Date or time or period function code
qualifier M an..3
2380 Date or time or period text C an..35
2379 Date or time or period format code C an..3
So you will have always something like 'DTM+d3:d35:d3' to search for.
Really, it doesn't worth the struggle, use EDI.net, create your own POCO classes and work from there.
Friendly reminder that EDIFact changes every 6 months on Europe.
If the separators can be any one of UNB, UNH, BGM, or DTM, the following Regex could work:
foreach (Match match in Regex.Matches(input, #"(UNB|UNH|BGM|DTM).+?(?=(UNB|UNH|BGM|DTM)|$)"))
{
Console.WriteLine(match.Value);
}
Explanation:
(UNB|UNH|BGM|DTM) matches either of the separators
.+? matches any string with at least one character (but as short as possible)
(?=(UNB|UNH|BGM|DTM)|$) matches if either a separator follows or if the string ends there - the match is however not included in the value.
It sounds like the other answer recognises the format - you should definitely consider a library specifically for parsing this format!
If you're intent on parsing it yourself, you could simply find the index of your identifiers in the string, determine the first 2 by position, and use those positions to Substring the original input
var input = "UNB+123UNH+234BGM+345DTM+456";
var chars = new[]{"UNH", "BGM", "DTM" };
var indexes = chars.Select(c => new{Length=c.Length,Position= input.IndexOf(c)}) // Get position and length of each input
.Where(x => x.Position>-1) // where there is actually a match
.OrderBy(x =>x.Position) // put them in order of the position in the input
.Take(2) // only interested in first 2
.ToArray(); // make it an array
if(indexes.Length < 2)
throw new Exception("Did not find 2");
var result = input.Substring(indexes[0].Position + indexes[0].Length, indexes[1].Position - indexes[0].Position - indexes[0].Length);
Live example: https://dotnetfiddle.net/tDiQLG
There is already a lot of answers here, but I took the time to write mine so might as well post it even if it's not as elegant.
The code assumes all tags are accounted for in the chars array.
string str = "UNB+123UNH+234BGM+345DTM+456";
string[] chars = { "UNH", "BGM", "DTM" };
var locations = chars.Select(o => str.IndexOf(o)).Where(i => i > -1).OrderBy(o => o);
var resultList = new List<string>();
for(int i = 0;i < locations.Count();i++)
{
var nextIndex = locations.ElementAtOrDefault(i + 1);
nextIndex = nextIndex > 0 ? nextIndex : str.Length;
nextIndex = nextIndex - locations.ElementAt(i);
resultList.Add(str.Substring(locations.ElementAt(i), nextIndex));
}
This is a fairly efficient O(n) solution using a HashSet
It's extremely simple, low allocations, more efficient than regex, and doesn't need a library
Given
private static HashSet<string> _set;
public static IEnumerable<string> Split(string input)
{
var last = 0;
for (int i = 0; i < input.Length-3; i++)
{
if (!_set.Contains(input.Substring(i, 3))) continue;
yield return input.Substring(last, i - last);
last = i;
}
yield return input.Substring(last);
}
Usage
_set = new HashSet<string>(new []{ "UNH", "BGM", "DTM" });
var results = Split("UNB+123UNH+234BGM+345DTM+456");
foreach (var item in results)
Console.WriteLine(item);
Output
UNB+123
UNH+234
BGM+345
DTM+456
Full Demo Here
Note : You could get this faster with a simple sorted tree, but would require more effort

Need to get two values out into an array from string split

I have a string that looks like this:
var result = "y-9m-10y-9m-11y-0m-02y-0m-03";
I need to make 2 lists:
one for all the y- objects(9,9,0,0)
and another for the m- objects(10,11,02,03).
How can I do this?
I have this older code from before that doesn't care about the y- objects. Now I need to get both sets.
var result = "m-10m-11m-02m-03";
var months = result.Split(new[] { "m-" }, StringSplitOptions.RemoveEmptyEntries);
Quick and dirty solution using regular expressions and LINQ:
var months = Regex.Matches(result, #"m-(\d+)").Cast<Match>().Select(m => int.Parse(m.Groups[1].Value));
var years = Regex.Matches(result, #"y-(\d+)").Cast<Match>().Select(m => int.Parse(m.Groups[1].Value));
Note that this doesn't do any error checking.
Edit: In the question you seem to use the extracted strings without converting them to int. In this case, omit the int.Parse and use m.Groups[1].Value directly.

String Find & Replace Method

I need to locate a specific part of a string value like the one below, I need to alter the "Meeting ID" to a specific number.
This number comes from a dropdownlist of multiple numbers, so I cant simply use find & replace. As the text could change to one of multiple numbers before the user is happy.
The "0783," part of the string never changes, and "Meeting ID" is always followed by a ",".
So i need to get to "0783, INSERT TEXT ," and then insert the new number on the Index Changed event.
Here is an example :-
Business Invitation, start time, M Problem, 518-06-xxx, 9999 999
0783, Meeting ID, xxx ??
What is the best way of locating this string and replacing the test each time?
I hope this makes sense guys?
Okay, so there are several ways of doing this, however this seems to be a string you have control over so I'm going to say here's what you want to do.
var myString = string.Format("Business Invitation, start time, M Problem, 518-06-xxx, 9999 999 0783, {0}, xxx ??", yourMeetingId);
If you don't have control over it then you're going to have to be a bit more clever:
var startingIndex = myString.IndexOf("0783, ");
var endingIndex = myString.IndexOf(",", startingIndex + 6);
var pattern = myString.Substring(startingIndex + 6, endingIndex - (startingIndex + 6));
myString = myString.Replace(pattern, yourMeetingId);
You should store your "current" Meeting ID in a variable, changing it along with your user's actions, and then use that same global variable whenever you need the string.
This way, you don't have to worry about what's inside the string and don't need to mess with array indexes. You will also be safe from magic numbers / strings, which are bound to blow up in your face at some point in the future.
You can try with Regex.Replace method
string pattern = #"\d{3},";
Regex regex = new Regex(pattern);
var inputStr = "518-06-xxx, 9999 999 0783";
var replace = "..."
var outputStr = regex.Replace(inputStr, replace);
use Regex.Split by token "0783," then in the second string in the array return split by token "," the first element in the string array would be where you would insert new text. Then use string.Join to join the first split with "0783," and the join the second with ",".
string temp = "Business Invitation, start time, M Problem, 518-06-xxx, 9999 999 0783, Meeting ID, xxx ??";
string newID = "1234";
string[] firstSplits = Regex.Split(temp, "0783,");
string[] secondSplits = Regex.Split(firstSplits[1], ",");
secondSplits[0] = newID;
string #join = string.Join(",", secondSplits);
firstSplits[1] = #join;
string newString = string.Join("0783,", firstSplits);

How to split a string if it contains period (.)?

I am taking numerical input from a text box. I want to check
if(textBox1.Text.Contains("."))
like 55.37
then split the string in two parts/strings.
First part before period (55)
Second part after the period (37)
Use this:
string[] ret = textBox1.Text.Split('.');
Then you can do
if (ret.Length != 2) // error ?!?
ret[0] is integer part
ret[1] is fractional part
var splitted = textBox1.Text.Split('.');
The result will be an array of strings. In your sample, the array will have 2 strings, 55 and 37.
use string.Split method
string[] a = textBox1.Text.Split('.');
string b = a[0];
string c = a[1];
In case there is a chance your code will be executed on OS with non-windows localization please use:
var separators = new[] {CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator};
var parts = textBox1.Text.Split(separators, StringSplitOptions.None);
It looks too verbose but it may be hard to understand why your code works on your machine (with dev environment) but don't on customers.
if (!textBox1.Text.Contains('.'))
return;
var parts = textBox1.Text.Split('.')
should do the trick.
use Split method
dim s as string = textbox1.text
s.split(".")
Use the following:
textBox1.Text.Split('.')

Categories

Resources