I would like to format a string that looks like this
BPT4SH9R0XJ6
Into something that looks like this
BPT4-SH9R-0XJ6
The string will always be a mix of 12 letters and numbers
Any advice will be highly appreciated, thanks
Try Regex.Replace(input, #"(\w{4})(\w{4})(\w{4})", #"$1-$2-$3");
Regex is often derided, but is a pretty neat way of doing what you need. Can be extended to more complex requirements that are difficult to meet using string methods.
You can use "(.{4})(.{4})(.{4})" as your expression and "$1-$2-$3" as your replacement. This is, however, hardly a good use for regexp: you can do it much easier with Substring.
var res = s.Substring(0,4)+"-"+s.Substring(4,4)+"-"+s.Substring(8);
If the rule is to always split in three block of four characters no need for a reg exp:
str.Substring(0,4) + "-" + str.Substring(4,4) + "-" + str.Substring(8,4)
It would seem that a combination of String.Concat and string.Substring should take care of everything that you need.
var str = "BPT4SH9R0XJ6";
var newStr = str.Substring(0, 4) + "-" + str.Substring(4, 4) + "-" + str.Substring(8, 4);
Any reason you want to do a regex? you could just insert hyphens:
string s = "BPT4SH9R0XJ6";
for(int i = 4; i < s.length; i = i+5)
s = s.Insert(i, "-");
This would keep adding hyphens every 4 characters, would not error out if string was too short/long/etc.
return original_string.SubString(0,4)+"-"+original_string.SubString(4,4)+"-"+original_string.SubString(8,4);
string str = #"BPT4SH9R0XJ6";
string formattedString = string.Format("{0}-{1}-{2}", str.Substring(0, 4), str.Substring(4,4), str.Substring(8,4));
This works with any length of string:
for (int i = 0; i < (int)Math.Floor((myString.Length - 1) / 4d); i++)
{
myString = myString.Insert((i + 1) * 4 + i, "-");
}
Ended upp using this
var original = "BPT4SH9R0XJ6".ToCharArray();
var first = new string(original, 0, 4);
var second = new string(original, 4, 4);
var third = new string(original, 8, 4);
var mystring = string.Concat(first, "-", second, "-", third);
Thanks
If you are guaranteed the text you're operating on is the 12 character code then why don't you just use substring? Why do you need the Regex?
String theString = "AB12CD34EF56";
String theNewString = theString.Substring(0, 4) + "-" + theString.Substring(4, 4) + "-" + theString.Substring(8, 4);'
Related
I want to add space between every 3 characters in a string in C#, but count from right to left.
For example :
11222333 -> 11 222 333
Answer by #Jimi from comments (will delete if they post their own)
var YourString = "11222333";
var sb = new StringBuilder(YourString);
for (int i = sb.Length -3; i >= 0; i -= 3)
sb.Insert(i, ' ');
return sb.ToString();
The benefit of this algorithm appears to be that you are working backwards through the string and therefore only moving a certain amount on each run, rather than the whole string.
If you are trying to format a string as a number according to some locale conventions you can use the NumberFormat class to set how you want a number to be formatted as a string
So for example
string input = "11222333";
NumberFormatInfo currentFormat = new NumberFormatInfo();
currentFormat.NumberGroupSeparator = " ";
if(Int32.TryParse(input, NumberStyles.None, currentFormat, out int result))
{
string output = result.ToString("N0", currentFormat);
Console.WriteLine(output); // 11 222 333
}
The following recursive function would do the job:
string space3(string s)
{
int len3 = s.Length - 3;
return (len <= 0) ? s
: (space3(s.Substring(0, len3)) + " " + s.Substring(len3));
}
C# 8.0 introduced string ranges. Ranges allow for a more compact form:
string space3(string s)
{
return (s.Length <= 3) ? s
: (space3(s[..^3]) + " " + s[^3..]);
}
Using Regex.Replace:
string input = "11222333";
string result = Regex.Replace( input, #"\d{3}", #" $0", RegexOptions.RightToLeft );
Demo and detailed explanation of RegEx pattern at regex101.
tl;dr: Match groups of 3 digits from right to left and replace them by space + the 3 digits.
The most efficient algorithm I can come up with is the following:
var sb = new StringBuilder(YourString.Length + YourString.Length / 3 + 1);
if (YourString.Length % 3 > 0)
{
sb.Append(YourString, 0, YourString.Length % 3);
sb.Append(' ');
}
for (var i = YourString.Length % 3; i < YourString.Length; i += 3)
{
sb.Append(YourString, i, 3);
sb.Append(' ');
}
return sb.ToString();
We first assign a StringBuilder of the correct size.
Then we check to see if we need to append the first one or two characters. Then we loop the rest.
dotnetfiddle
For example, TOMILA RELEASE V6.24 , i want to get 6.24 i used
if (txt.Contains("<TOMILA RELEASE"))
{
int iStartIndex = txt.LastIndexOf("<TOMILA RELEASE") + 17;
for (int i = 0; i < 50; i++) {
if (txt[iStartIndex + i] == '>') break;
currentRelease += txt[iStartIndex + i];
}
}
So, my question is if i want to get the specific 6 from TOMILA RELEASE V6.24, how could i get it?
You can try LastIndexOf followed by Substring
var result = str.Substring(str.LastIndexOf('TOMILA RELEASE V') + 1);
If you want to take first number in the string you can use following regular expression.
string s = "TOMILA RELEASE V6.24";
string digit = Regex.Match(s, "\\d").Value;
Here \d is for matching the digit, you can find more about regular expression in this tutorial, The 30 Minute Regex Tutorial
If you want to extract all number before dot then you can add + with \d and use do to end the extraction.
string number = Regex.Match(s, "\\d+.").Value.Replace(".","");
If you want to get a specific portion of a string, you could use the below code
string str = "6.24";
var val = str.Substring(0, 1);
For example I have those strings:
"qwe/qwe/qwe/qwe//qwe/somethinghere_blabla.exe"
"qwe/qwe/q//we/qwe//qwe/somethingother_here_blabla.exe"
"qwe/qwe/qwe/qwe//qwe/some_numbers_here_blabla.exe"
Now I want to get the text between the last '/' and the last '_'.
So the outcome would be:
"somethinghere"
"somethingother_here"
"some_numbers_here"
What is the easiest and clearest way to do this?
I have no idea how to do this, should I split them in '/' and '_', so do this apart? I couldn't think of any way how to do it.
Maybe scan the string from the end till it reaches its first '/' and '_'? Or is there an easier and faster way? Because it has to scan ~10.000 strings.
string[] words = line.Split('/', '_'); //maybe use this? probably not
Thanks in advance!
string s = "qwe/qwe/q//we/qwe//qwe/somethingother_here_blabla.exe";
int last_ = s.LastIndexOf('_');
if (last_ < 0) // _ not found, take the tail of string
last_ = s.Length;
int lastSlash = s.LastIndexOf('/');
string part = s.Substring(lastSlash + 1, last_ - lastSlash - 1);
Well, there is string.LastIndexOf:
var start = line.LastIndexOf('/') + 1;
var end = line.LastIndexOf('_');
var result = line.Substring(start, end - start);
The LINQ way:
var str = "qwe/qwe/qwe/qwe//qwe/somethinghere_blabla.exe";
var newStr = new string(str.Reverse().SkipWhile(c => c != '_').Skip(1).TakeWhile(c => c != '/').Reverse().ToArray());
var string = "qwe/qwe/qwe/qwe//qwe/some_numbers_here_blabla.exe";
var start = string.lastIndexOf("/");
var end = string.lastIndexOf("_");
var result = string.substring(start + 1, end);
Note: above code does not handle error if string does not have / or _ after a last slash
I have a file name: kjrjh20111103-BATCH2242_20111113-091337.txt
I only need 091337, not the txt or the - how can I achieve that. It does not have to be 6 numbers it could be more or less but will always be after "-" and the last ones before ."doc" or ."txt"
You can either do this with a regex, or with simple string operations. For the latter:
int lastDash = text.LastIndexOf('-');
string afterDash = text.Substring(lastDash + 1);
int dot = afterDash.IndexOf('.');
string data = dot == -1 ? afterDash : afterDash.Substring(0, dot);
Personally I find this easier to understand and verify than a regular expression, but your mileage may vary.
String fileName = kjrjh20111103-BATCH2242_20111113-091337.txt;
String[] splitString = fileName.Split ( new char[] { '-', '.' } );
String Number = splitString[2];
Regex: .*-(?<num>[0-9]*). should do the job. num capture group contains your string.
The Regex would be:
string fileName = "kjrjh20111103-BATCH2242_20111113-091337.txt";
string fileMatch = Regex.Match(fileName, "(?<=-)\d+", RegexOptions.IgnoreCase).Value;
String fileName = "kjrjh20111103-BATCH2242_20111113-091337.txt";
var startIndex = fileName.LastIndexOf('-') + 1;
var length = fileName.LastIndexOf('.') - startIndex;
var output = fileName.Substring(startIndex, length);
I want to convert this string format:
11/2013
TO
201311
So, suppose my string is in this variable:
string s = "11/2013";
What should be the code for this problem? Thanks!
sprime = s.Split(new char [] {'/'});
s = sprime[1] + sprime[0];
This is fastest:
s = s[3] + s[4] + s[5] + s[6] + s[0] + s[1];
If you are converting a gazillion billion records it will matter.
string newString = s.Split('/')[1] + s.Split('/')[0];
string [] split = s.Split('/');
string str = split[1] + split[0];
string s = "11/2011";
s = String.Format("{0}{1}", s.Split('/')[1], s.Split('/')[0]);
Note that this answer assumes s will match, and it will return an empty string if it doesn't.
Using Split will throw an IndexOutOfRangeException when you try to access [1] when there was no /.
You could of course add code to handle these cases regardless of whether you use Regex or Split.
string s = "11/2013";
// Match 1+ digits followed by a forward slash followed by 1+ digits
Regex r = new Regex(#"(\d+)/(\d+)");
var m = r.Match(s);
string result = m.Groups[2].Value + m.Groups[1].Value;
All so complicated. Makes my head hurt. Try this instead:
static readonly Regex rx = new Regex( #"^(\d\d)/(\d\d\d\d)$" ) ;
...
string s1 = "12/3456" ;
string s2 = rx.Replace( s1 , #"$2/$1" ) ;
Or Splitless for [??]/YYYY; s.Substring(s.Length - 4, 4) + s.Substring(0, s.Length - 5);
string s = "11/2013";
Regex r = new Regex("^(?<month>[0-1]?[0-9])/(?<year>[0-9]{4})$");
var match = r.Match(s);
string month = match.Groups["month"].Value;
string year = match.Groups["year"].Value;
string result = year + month;