Increment a string with both letters and numbers - c#

I have a string which i need to increment by 1 The string has both characters and numeric values.
The string layout i have is as follows "MD00494"
How would i increment this to "MD00496" & "MD00497" ect
If it was a normal string with numbers i would parse it to an int.
I have tried the following
int i = int.Parse(sdesptchNo);
i++;
txtDispatchNo.Text = i.ToString();
Anyone any ideas how i would go about this.

You first should figure out any commonality between the strings. If there is always a prefix of letters followed by digits (with a fixed width) at the end, then you can just remove the letters, parse the rest, increment, and stick them together again.
E.g. in your case you could use something like the following:
var prefix = Regex.Match(sdesptchNo, "^\\D+").Value;
var number = Regex.Replace(sdesptchNo, "^\\D+", "");
var i = int.Parse(number) + 1;
var newString = prefix + i.ToString(new string('0', number.Length));
Another option that might be a little more robust might be
var newString = Regex.Replace(x, "\\d+",
m => (int.Parse(m.Value) + 1).ToString(new string('0', m.Value.Length)));
This would replace any number in the string by the incremented number in the same width – but leaves every non-number exactly the same and in the same place.

Here is one Non-Regex way :P
string str = "MD00494";
string digits = new string(str.Where(char.IsDigit).ToArray());
string letters = new string(str.Where(char.IsLetter).ToArray());
int number;
if (!int.TryParse(digits, out number)) //int.Parse would do the job since only digits are selected
{
Console.WriteLine("Something weired happened");
}
string newStr = letters + (++number).ToString("D5");
output would be:
newStr = "MD00495"

Assuming that you only need to increment the numeric portion of the string, and that the structure of the strings is always - bunch of non-numeric characters followed by a bunch of numerals, you can use a regular expression to break up the string into these two components, convert the numeric portion to an integer, increment and then concatenate back.
var match = Regex.Match("MD123", #"^([^0-9]+)([0-9]+)$");
var num = int.Parse(match.Groups[2].Value);
var after = match.Groups[1].Value + (num + 1);

You need to find the position of the first digit in the string.
Then split the string into 2 fields.
0 1 2 3 4 5 6
M D 0 0 4 9 4
The first field will be the non numeric part "MD"
The second field will be the numeric part "00494"
Increment the numeric only part to "00495"
You will lose the leading zero's so you'll need to pad your new number with the same amount of zero's once you've incremented.
Then join the 2 fields.

The accepted answer does not work if there is a number in the middle of the string e.g. XXX123YYY456, exceptions are thrown.
I have written a generic method that will increment the end of the string and you can pass it the minimum amount of digits.
public static string IncrementStringEnd(string name, int minNumericalCharacters = 1)
{
var prefix = System.Text.RegularExpressions.Regex.Match(name, #"\d+$");
if (prefix.Success)
{
var capture = prefix.Captures[0];
int number = int.Parse(capture.Value) + 1;
name = name.Remove(capture.Index, capture.Length) + number.ToString("D" + minNumericalCharacters);
}
return name;
}
Test Results:
MD00494 : MD00495
XXX123YYY456 : XXX123YYY457
SD50MRF999 : SD50MRF1000
SD50MRF9 : SD50MRF010
For testing purposes https://dotnetfiddle.net/j1f6wh

You can use regex:
int kod = int.Parse(Regex.Replace(sdesptchNo, "[^0-9]", "")) + 1;
string zeroStr=Regex.Replace(sdesptchNo, "[^0-9]", "");
string newZeroStr="";
for (int x=0;x<zeroStr.length;x++)
if (zeroStr[x]=='0') newZeroStr=newZeroStr+"0";
else break;
string newVal=Regex.Replace(sdesptchNo, "[0-9]", "") + newZeroStr + kod;
UPDATED: This will save your zero

string sDispatchNo = "MS00914";
var pattern = #"^[a-zA-Z]+";
var strPart = Regex.Match(sDispatchNo, pattern).Value;
var noPart = Regex.Replace(sDispatchNo, pattern, "");
var no = int.Parse(noPart);
var length = noPart.Length;
length = (no + 1)/(Math.Pow(10,length)) == 1 ? length + 1 : length;
var output = strPart + (no + 1).ToString("D" + length);

Here's my solution:
string str = Console.ReadLine();
string digits = new string(str.Where(char.IsDigit).ToArray());
string letters = new string(str.Where(char.IsLetter).ToArray());
string newStr;
int number;
if (!int.TryParse(digits, out number))
{
Console.WriteLine("Something weird happened");
}
if (digits.StartsWith("0"))
{
newStr = letters + (++number).ToString("D5");
}
else
{
newStr = letters + (++number).ToString();
}
Try it!

I use this to Increment/Decrement Barcodes
/// <summary>
/// Gets the number portion of the string and adds 1 to it
/// </summary>
public static string IncrementNumbers(this string numString)
{
if (numString.IsEmpty())
return numString;
else if (!numString.Where(Char.IsDigit).Any())
return numString;
else
{
string prefix = Regex.Match(numString, "^\\D+").Value;
string number = Regex.Replace(numString, "^\\D+", "");
int i = int.Parse(number) + 1;
return prefix + i.ToString($"D{numString.Length - prefix.Length}");
}
}
/// <summary>
/// Gets the number portion of the string and subtracts 1 from it
/// </summary>
public static string DecrementNumbers(this string numString)
{
if (numString.IsEmpty())
return numString;
else if (!numString.Where(Char.IsDigit).Any())
return numString;
else
{
string prefix = Regex.Match(numString, "^\\D+").Value;
string number = Regex.Replace(numString, "^\\D+", "");
int i = int.Parse(number) - 1;
return prefix + i.ToString($"D{numString.Length - prefix.Length}");
}
}
/// <summary>
/// Shortented IsNullOrWhiteSpace
/// </summary>
public static bool IsEmpty(this string str)
{
if (str.TrimFix() == null)
return true;
return false;
}
/// <summary>
/// Trims the String and returns Null if it's empty space
/// </summary>
public static string TrimFix(this string rawString)
{
if (!string.IsNullOrWhiteSpace(rawString))
{
return rawString.Trim();
}
return null;
}

Related

ASP.Net - Masked first 6 character on a string with dynamic length

is it possible to masked a first 6 characters on a string if the string is dynamics on it's length?
Example, I have a string "test123456789" and I want a result of "******3456789" or a string of "1234test" and I want a result of "******st". All I'm seeing sample codes here in masking are strings with a static length. Can anyone kindly help me with this? Thank you so much in advance.
Yes, it's possible, and even quite easy, using simple string concatenation and SubString:
var original = "Some string here";
var target = "******" + ((original.Length > 6) ? original.Substring(6) : "") ;
If you want shorter strings to mask all characters but keep original length, you can do it like this:
var target = new string('*', Math.Min(original.Length, 6)) + ((original.Length > 6) ? original.Substring(6) : "") ;
This way, an input of "123" would return 3 asterisks ("***"). The first code I've shown will return 6 asterisks ("******")
Linq is an alternative to Substring and ternary operator solution (see Zohar Peled's answer):
using System.Linq;
...
string original = "Some string here";
string result = "******" + string.Concat(original.Skip(6));
If you want to preserve the length of short (less than 6 character string):
// if original shorter than 6 symbols, e.g. "short"
// we'll get "*****" (Length number of *, not 6)
// if original has six or more symbols, e.g. "QuiteLong"
// we'll get "******ong" as usual
string original = "short";
...
string result = new string('*', Math.Min(6, original.Length)) +
string.Concat(original.Skip(Math.Min(6, original.Length)));
You may want to have the routine as an extension method:
public static partial class StringExtensions {
public static string MaskPrefix(this string value, int count = 6) {
if (null == value)
throw new ArgumentNullException("value"); // or return value
else if (count < 0)
throw new ArgumentOutOfRangeException("count"); // or return value
int length = Math.Min(value.Length, count);
return new string('*', length) + string.Concat(value.Skip(length));
}
}
And so you can put as if string has MaskPrefix method:
string original = "Some string here";
string result = original.MaskPrefix(6);
You can substring and mask it. Make sure to check if the input string is lower than 6 as below sample
string str = "123456789345798";
var strResult = "******"+str.Substring(6, str.Length - 6);
Console.WriteLine("strResult :" + strResult);

C# How to generate a new string based on multiple ranged index

Let's say I have a string like this one, left part is a word, right part is a collection of indices (single or range) used to reference furigana (phonetics) for kanjis in my word:
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす"
The pattern in detail:
word,<startIndex>(-<endIndex>):<furigana>
What would be the best way to achieve something like this (with a space in front of the kanji to mark which part is linked to the [furigana]):
子[こ]で 子[こ]にならぬ 時鳥[ほととぎす]
Edit: (thanks for your comments guys)
Here is what I wrote so far:
static void Main(string[] args)
{
string myString = "ABCDEF,1:test;3:test2";
//Split Kanjis / Indices
string[] tokens = myString.Split(',');
//Extract furigana indices
string[] indices = tokens[1].Split(';');
//Dictionnary to store furigana indices
Dictionary<string, string> furiganaIndices = new Dictionary<string, string>();
//Collect
foreach (string index in indices)
{
string[] splitIndex = index.Split(':');
furiganaIndices.Add(splitIndex[0], splitIndex[1]);
}
//Processing
string result = tokens[0] + ",";
for (int i = 0; i < tokens[0].Length; i++)
{
string currentIndex = i.ToString();
if (furiganaIndices.ContainsKey(currentIndex)) //add [furigana]
{
string currentFurigana = furiganaIndices[currentIndex].ToString();
result = result + " " + tokens[0].ElementAt(i) + string.Format("[{0}]", currentFurigana);
}
else //nothing to add
{
result = result + tokens[0].ElementAt(i);
}
}
File.AppendAllText(#"D:\test.txt", result + Environment.NewLine);
}
Result:
ABCDEF,A B[test]C D[test2]EF
I struggle to find a way to process ranged indices:
string myString = "ABCDEF,1:test;2-3:test2";
Result : ABCDEF,A B[test] CD[test2]EF
I don't have anything against manually manipulating strings per se. But given that you seem to have a regular pattern describing the inputs, it seems to me that a solution that uses regex would be more maintainable and readable. So with that in mind, here's an example program that takes that approach:
class Program
{
private const string _kinvalidFormatException = "Invalid format for edit specification";
private static readonly Regex
regex1 = new Regex(#"(?<word>[^,]+),(?<edit>(?:\d+)(?:-(?:\d+))?:(?:[^;]+);?)+", RegexOptions.Compiled),
regex2 = new Regex(#"(?<start>\d+)(?:-(?<end>\d+))?:(?<furigana>[^;]+);?", RegexOptions.Compiled);
static void Main(string[] args)
{
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす";
string result = EditString(myString);
}
private static string EditString(string myString)
{
Match editsMatch = regex1.Match(myString);
if (!editsMatch.Success)
{
throw new ArgumentException(_kinvalidFormatException);
}
int ichCur = 0;
string input = editsMatch.Groups["word"].Value;
StringBuilder text = new StringBuilder();
foreach (Capture capture in editsMatch.Groups["edit"].Captures)
{
Match oneEditMatch = regex2.Match(capture.Value);
if (!oneEditMatch.Success)
{
throw new ArgumentException(_kinvalidFormatException);
}
int start, end;
if (!int.TryParse(oneEditMatch.Groups["start"].Value, out start))
{
throw new ArgumentException(_kinvalidFormatException);
}
Group endGroup = oneEditMatch.Groups["end"];
if (endGroup.Success)
{
if (!int.TryParse(endGroup.Value, out end))
{
throw new ArgumentException(_kinvalidFormatException);
}
}
else
{
end = start;
}
text.Append(input.Substring(ichCur, start - ichCur));
if (text.Length > 0)
{
text.Append(' ');
}
ichCur = end + 1;
text.Append(input.Substring(start, ichCur - start));
text.Append(string.Format("[{0}]", oneEditMatch.Groups["furigana"]));
}
if (ichCur < input.Length)
{
text.Append(input.Substring(ichCur));
}
return text.ToString();
}
}
Notes:
This implementation assumes that the edit specifications will be listed in order and won't overlap. It makes no attempt to validate that part of the input; depending on where you are getting your input from you may want to add that. If it's valid for the specifications to be listed out of order, you can also extend the above to first store the edits in a list and sort the list by the start index before actually editing the string. (In similar fashion to the way the other proposed answer works; though, why they are using a dictionary instead of a simple list to store the individual edits, I have no idea…that seems arbitrarily complicated to me.)
I included basic input validation, throwing exceptions where failures occur in the pattern matching. A more user-friendly implementation would add more specific information to each exception, describing what part of the input actually was invalid.
The Regex class actually has a Replace() method, which allows for complete customization. The above could have been implemented that way, using Replace() and a MatchEvaluator to provide the replacement text, instead of just appending text to a StringBuilder. Which way to do it is mostly a matter of preference, though the MatchEvaluator might be preferred if you have a need for more flexible implementation options (i.e. if the exact format of the result can vary).
If you do choose to use the other proposed answer, I strongly recommend you use StringBuilder instead of simply concatenating onto the results variable. For short strings it won't matter much, but you should get into the habit of always using StringBuilder when you have a loop that is incrementally adding onto a string value, because for long string the performance implications of using concatenation can be very negative.
This should do it (and even handle ranged indices), based on the formatting of the input string you have-
using System;
using System.Collections.Generic;
public class stringParser
{
private struct IndexElements
{
public int start;
public int end;
public string value;
}
public static void Main()
{
//input string
string myString = "子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす";
int wordIndexSplit = myString.IndexOf(',');
string word = myString.Substring(0,wordIndexSplit);
string indices = myString.Substring(wordIndexSplit + 1);
string[] eachIndex = indices.Split(';');
Dictionary<int,IndexElements> index = new Dictionary<int,IndexElements>();
string[] elements;
IndexElements e;
int dash;
int n = 0;
int last = -1;
string results = "";
foreach (string s in eachIndex)
{
e = new IndexElements();
elements = s.Split(':');
if (elements[0].Contains("-"))
{
dash = elements[0].IndexOf('-');
e.start = int.Parse(elements[0].Substring(0,dash));
e.end = int.Parse(elements[0].Substring(dash + 1));
}
else
{
e.start = int.Parse(elements[0]);
e.end = e.start;
}
e.value = elements[1];
index.Add(n,e);
n++;
}
//this is the part that takes the "setup" from the parts above and forms the result string
//loop through each of the "indices" parsed above
for (int i = 0; i < index.Count; i++)
{
//if this is the first iteration through the loop, and the first "index" does not start
//at position 0, add the beginning characters before its start
if (last == -1 && index[i].start > 0)
{
results += word.Substring(0,index[i].start);
}
//if this is not the first iteration through the loop, and the previous iteration did
//not stop at the position directly before the start of the current iteration, add
//the intermediary chracters
else if (last != -1 && last + 1 != index[i].start)
{
results += word.Substring(last + 1,index[i].start - (last + 1));
}
//add the space before the "index" match, the actual match, and then the formatted "index"
results += " " + word.Substring(index[i].start,(index[i].end - index[i].start) + 1)
+ "[" + index[i].value + "]";
//remember the position of the ending for the next iteration
last = index[i].end;
}
//if the last "index" did not stop at the end of the input string, add the remaining characters
if (index[index.Keys.Count - 1].end + 1 < word.Length)
{
results += word.Substring(index[index.Keys.Count-1].end + 1);
}
//trimming spaces that may be left behind
results = results.Trim();
Console.WriteLine("INPUT - " + myString);
Console.WriteLine("OUTPUT - " + results);
Console.Read();
}
}
input - 子で子にならぬ時鳥,0:こ;2:こ;7-8:ほととぎす
output - 子[こ]で 子[こ]にならぬ 時鳥[ほととぎす]
Note that this should also work with characters the English alphabet if you wanted to use English instead-
input - iliketocodeverymuch,2:A;4-6:B;9-12:CDEFG
output - il i[A]k eto[B]co deve[CDEFG]rymuch

Getting number from a string in C#

I am scraping some website content which is like this - "Company Stock Rs. 7100".
Now, what i want is to extract the numeric value from this string. I tried split but something or the other goes wrong with my regular expression.
Please let me know how to get this value.
Use:
var result = Regex.Match(input, #"\d+").Value;
If you want to find only number which is last "entity" in the string you should use this regex:
\d+$
If you want to match last number in the string, you can use:
\d+(?!\D*\d)
int val = int.Parse(Regex.Match(input, #"\d+", RegexOptions.RightToLeft).Value);
I always liked LINQ:
var theNumber = theString.Where(x => char.IsNumber(x));
Though Regex sounds like the native choice...
This code will return the integer at the end of the string. This will work better than the regular expressions in the case that there is a number somewhere else in the string.
public int getLastInt(string line)
{
int offset = line.Length;
for (int i = line.Length - 1; i >= 0; i--)
{
char c = line[i];
if (char.IsDigit(c))
{
offset--;
}
else
{
if (offset == line.Length)
{
// No int at the end
return -1;
}
return int.Parse(line.Substring(offset));
}
}
return int.Parse(line.Substring(offset));
}
If your number is always after the last space and your string always ends with this number, you can get it this way:
str.Substring(str.LastIndexOf(" ") + 1)
Here is my answer ....it is separating numeric from string using C#....
static void Main(string[] args)
{
String details = "XSD34AB67";
string numeric = "";
string nonnumeric = "";
char[] mychar = details.ToCharArray();
foreach (char ch in mychar)
{
if (char.IsDigit(ch))
{
numeric = numeric + ch.ToString();
}
else
{
nonnumeric = nonnumeric + ch.ToString();
}
}
int i = Convert.ToInt32(numeric);
Console.WriteLine(numeric);
Console.WriteLine(nonnumeric);
Console.ReadLine();
}
}
}
You can use \d+ to match the first occurrence of a number:
string num = Regex.Match(input, #"\d+").Value;

How to get number from string in C#

i have a String in HTML (1-3 of 3 Trip) how do i get the number 3(before trip) and convert it to int.I want to use it as a count
Found this code
public static string GetNumberFromStr(string str)
{
str = str.Trim();
Match m = Regex.Match(str, #"^[\+\-]?\d*\.?[Ee]?[\+\-]?\d*$");
return (m.Value);
}
But it can only get 1 number
Regex is unnecessary overhead in your case. try this:
int ExtractNumber(string input)
{
int number = Convert.ToInt32(input.Split(' ')[2]);
return number;
}
Other useful methods for Googlers:
// throws exception if it fails
int i = int.Parse(someString);
// returns false if it fails, returns true and changes `i` if it succeeds
bool b = int.TryParse(someString, out i);
// this one is able to convert any numeric Unicode character to a double. Returns -1 if it fails
double two = char.GetNumericValue('٢')
Forget Regex. This code splits the string using a space as a delimiter and gets the number in the index 2 position.
string trip = "1-3 of 3 trip";
string[] array = trip.Split(' ');
int theNumberYouWant = int.Parse(array[2]);
Try this:
public static int GetNumberFromStr(string str)
{
str = str.Trim();
Match m = Regex.Match(str, #"^.*of\s(?<TripCount>\d+)");
return m.Groups["TripCount"].Length > 0 ? int.Parse(m.Groups["TripCount"].Value) : 0;
}
Another way to do it:
public static int[] GetNumbersFromString(string str)
{
List<int> result = new List<int>();
string[] numbers = Regex.Split(input, #"\D+");
int i;
foreach (string value in numbers)
{
if (int.TryParse(value, out i))
{
result.Add(i);
}
}
return result.ToArray();
}
Example of how to use:
const string input = "There are 4 numbers in this string: 40, 30, and 10.";
int[] numbers = MyHelperClass.GetNumbersFromString();
for(i = 0; i < numbers.length; i++)
{
Console.WriteLine("Number {0}: {1}", i + 1, number[i]);
}
Output:
Number: 4
Number: 40
Number: 30
Number: 10
Thanks to: http://www.dotnetperls.com/regex-split-numbers
If I'm reading your question properly, you'll get a string that is a single digit number followed by ' Trip' and you want to get the numeric value out?
public static int GetTripNumber(string tripEntry)
{
return int.Parse(tripEntry.ToCharArray()[0]);
}
Not really sure if you mean that you always have "(x-y of y Trip)" as a part of the string you parse...if you look at the pattern it only catches the "x-y" part thought with the acceptance of .Ee+- as seperators. If you want to catch the "y Trip" part you will have to look at another regex instead.
You could do a simple, if you change the return type to int instead of string:
Match m = Regex.Match(str, #"(?<maxTrips>\d+)\sTrip");
return m.Groups["maxTrips"].Lenght > 0 ? Convert.ToInt32(m.Groups["maxTrips"].Value) : 0;

How to get the last five characters of a string using Substring() in C#?

I can get the first three characters with the function below.
However, how can I get the output of the last five characters ("Three") with the Substring() function? Or will another string function have to be used?
static void Main()
{
string input = "OneTwoThree";
// Get first three characters
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub); // Output One.
}
If your input string could be less than five characters long then you should be aware that string.Substring will throw an ArgumentOutOfRangeException if the startIndex argument is negative.
To solve this potential problem you can use the following code:
string sub = input.Substring(Math.Max(0, input.Length - 5));
Or more explicitly:
public static string Right(string input, int length)
{
if (length >= input.Length)
{
return input;
}
else
{
return input.Substring(input.Length - length);
}
}
string sub = input.Substring(input.Length - 5);
If you can use extension methods, this will do it in a safe way regardless of string length:
public static string Right(this string text, int maxLength)
{
if (string.IsNullOrEmpty(text) || maxLength <= 0)
{
return string.Empty;
}
if (maxLength < text.Length)
{
return text.Substring(text.Length - maxLength);
}
return text;
}
And to use it:
string sub = input.Right(5);
static void Main()
{
string input = "OneTwoThree";
//Get last 5 characters
string sub = input.Substring(6);
Console.WriteLine("Substring: {0}", sub); // Output Three.
}
Substring(0, 3) - Returns substring of first 3 chars. //One
Substring(3, 3) - Returns substring of second 3 chars. //Two
Substring(6) - Returns substring of all chars after first 6. //Three
One way is to use the Length property of the string as part of the input to Substring:
string sub = input.Substring(input.Length - 5); // Retrieves the last 5 characters of input
Here is a quick extension method you can use that mimics PHP syntax. Include AssemblyName.Extensions to the code file you are using the extension in.
Then you could call:
input.SubstringReverse(-5) and it will return "Three".
namespace AssemblyName.Extensions {
public static class StringExtensions
{
/// <summary>
/// Takes a negative integer - counts back from the end of the string.
/// </summary>
/// <param name="str"></param>
/// <param name="length"></param>
public static string SubstringReverse(this string str, int length)
{
if (length > 0)
{
throw new ArgumentOutOfRangeException("Length must be less than zero.");
}
if (str.Length < Math.Abs(length))
{
throw new ArgumentOutOfRangeException("Length cannot be greater than the length of the string.");
}
return str.Substring((str.Length + length), Math.Abs(length));
}
}
}
Substring. This method extracts strings. It requires the location of the substring (a start index, a length). It then returns a new string with the characters in that range.
See a small example :
string input = "OneTwoThree";
// Get first three characters.
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub);
Output :
Substring: One
simple way to do this in one line of code would be this
string sub = input.Substring(input.Length > 5 ? input.Length - 5 : 0);
and here some informations about Operator ? :
string input = "OneTwoThree";
(if input.length >5)
{
string str=input.substring(input.length-5,5);
}
e.g.
string str = null;
string retString = null;
str = "This is substring test";
retString = str.Substring(8, 9);
This return "substring"
C# substring sample source
In C# 8.0 and later you can use [^5..] to get the last five characters combined with a ? operator to avoid a potential ArgumentOutOfRangeException.
string input1 = "0123456789";
string input2 = "0123";
Console.WriteLine(input1.Length >= 5 ? input1[^5..] : input1); //returns 56789
Console.WriteLine(input2.Length >= 5 ? input2[^5..] : input2); //returns 0123
index-from-end-operator and range-operator
// Get first three characters
string sub = input.Substring(0, 3);
Console.WriteLine("Substring: {0}", sub); // Output One.
string sub = input.Substring(6, 5);
Console.WriteLine("Substring: {0}", sub); //You'll get output: Three

Categories