Best way to use Regex and int's - c#

I have an application that loops through a group of documents and if a value is detected, then the user receives a prompt to replace this value. My current code looks like the following;
if (alllines[i].Contains("$"))
{
// prompt
int dollarIndex = alllines[i].IndexOf("%");
string nextTenChars = alllines[i].Substring(dollarIndex + 1, 18);
string PromtText = nextTenChars.Replace("%", "").Replace("/*", "").Replace("*/", "");
string promptValue = CreateInput.ShowDialog(PromtText, fi.FullName);
if (promptValue.Equals(""))
{
}
else
{
alllines[i] = alllines[i].Replace("$", promptValue);
File.WriteAllLines(fi.FullName, alllines.ToArray());
}
}
As you can see the prompt box displays 18 characters after the index which in this case is % however, if there are not 18 characters then the application crashes. What I want to do is use regex but I am unsure of how to apply this in the codes current state.
If I use the below I get the error Cannot convert from int to string any help would be appreciated.
Regex regex = new Regex(#"(\$.{1,10})");
var chars = regex.Matches(dollarIndex);

This should work
Regex regex = new Regex(#"(/*%.{1,50})");
var chars = regex.Match(alllines[i]).ToString();
string promptValue = CreateInput.ShowDialog(PromtText, fi.FullName);

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);
}

Trimming degree symbol on C#

Can anyone tell me why this is not working:
string txt = "+0°1,0'";
string degree = txt.TrimEnd('°');
I am trying to separate the degrees on this string, but after this, what remains on degree is the same content of txt.
I am using C# in Visual Studio.
string.TrimEnd remove char at the end. In your example, '°' isn't at the end.
For example :
string txt = "+0°°°°";
string degree = txt.TrimEnd('°');
// degree => "+0"
If you want remove '°' and all next characters, you can :
string txt = "+0°1,0'";
string degree = txt.Remove(txt.IndexOf('°'));
// degree => "+0"
string txt = "+0°1,0'";
if(txt.IndexOf('°') > 0) // Checking if character '°' exist in the string
{
string withoutdegree = txt.Remove(txt.IndexOf('°'),1);
}
Another safe way of handling the same is using the String.Split method. You will not have to bother to verify the presence of the character in this case.
string txt = "+0°1,0'";
var str = txt.Split('°')[0]; // "+0"
string txt = "+01,0'";
var str = txt.Split('°')[0]; // "+01,0'"
You can use this to remove all the '°' symbols present in your string using String.Replace
string txt = "+0°1,0'°°";
var text = txt.Replace(#"°", ""); // +01,0'
Edit: Added a safe way to handle the OP's exact query.

Remove illegal characters from filename to burn on CD

I have done research but my app downloads mp3 files every once in a while I get weird filename which doesn't hurt until I try to burn them to CD. Below is a good example.
The Animals - House of the Rising Sun (1964) + clip compilation ♫♥ 50 YEARS - counting.mp3
I have some code to try and catch illegal characters but it doesn't stop this filename. Is there a better way to catch the weird stuff the code I use currently is:
public static string RemoveIllegalFileNameChars(string input, string replacement = "")
{
if (input.Contains("?"))
{
input = input.Replace('?', char.Parse(" "));
}
if (input.Contains("&"))
{
input = input.Replace('&', char.Parse("-"));
}
var regexSearch = new string(Path.GetInvalidFileNameChars()) +
new string(Path.GetInvalidPathChars());
var r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
return r.Replace(input, replacement);
}
The CD file system is different to the OS file system, so those Path.GetInvalidX functions don't really apply to CDs.
I'm not sure, but possibly the standard you are looking at is ISO 9660
https://en.wikipedia.org/wiki/ISO_9660
Which has an extremely limited character set in filenames.
I think that Joliet extension to that standard must be in play:
https://en.wikipedia.org/wiki/Joliet_(file_system)
I think that maybe you are running into the filename length problem more than anything: "The specification only allows filenames to be up to 64 Unicode characters in length". Your filename is 90 characters long.
The following code will turn non-ascii characters into '?'
string sOut = Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(s))
Then you can use a sOut.Replace('?', '') call to take them out. Does this seem like it would work for you?
Although, in this case your file name is valid, to catch invalid file names, it is suggested to use GetInvalidFileNameChars() method.
string fileName = "The Animals - House of the Rising Sun ? (1964) + clip compilation ♫♥ 50 YEARS - counting.mp3";
byte[] bytes = Encoding.ASCII.GetBytes(fileName);
char[] characters = Encoding.ASCII.GetChars(bytes);
string name = new string(characters);
StringBuilder fileN = new StringBuilder(name);
foreach (char c in Path.GetInvalidFileNameChars())
{
fileN.Replace(c, '_');
}
string validFileName = fileN.ToString();
https://learn.microsoft.com/en-us/dotnet/api/system.io.path.getinvalidfilenamechars?view=netframework-4.7.2
Thanks for all your help the final working code is listed below
public static string RemoveIllegalFileNameChars(string input, string replacement = "")
{
if (input.Contains("?"))
{
input = input.Replace('?', char.Parse(" "));
}
if (input.Contains("&"))
{
input = input.Replace('&', char.Parse("-"));
}
var regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
var r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
// check for non asccii characters
byte[] bytes = Encoding.ASCII.GetBytes(input);
char[] chars = Encoding.ASCII.GetChars(bytes);
string line = new String(chars);
line = line.Replace("?", "");
//MessageBox.Show(line);
return r.Replace(line, replacement);
}

Replacing characters in a string with another string

So what I am trying to do is as follows :
example of a string is A4PC
I am trying to replace for example any occurance of "A" with "[A4]" so I would get and similar any occurance of "4" with "[A4]"
"[A4][A4]PC"
I tried doing a normal Replace on the string but found out I got
"[A[A4]]PC"
string badWordAllVariants =
restriction.Value.Replace("A", "[A4]").Replace("4", "[A4]")
since I have two A's in a row causing an issue.
So I was thinking it would be better rather than the replace on the string I need to do it on a character per character basis and then build up a string again.
Is there anyway in Linq or so to do something like this ?
You don't need any LINQ here - String.Replace works just fine:
string input = "AAPC";
string result = input.Replace("A", "[A4]"); // "[A4][A4]PC"
UPDATE: For your updated requirements I suggest to use regular expression replace
string input = "A4PC";
var result = Regex.Replace(input, "A|4", "[A4]"); // "[A4][A4]PC"
This works well for me:
string x = "AAPC";
string replace = x.Replace("A", "[A4]");
EDIT:
Based on the updated question, the issue is the second replacement. In order to replace multiple strings you will want to do this sequentially:
var original = "AAPC";
// add arbitrary room to allow for more new characters
StringBuilder resultString = new StringBuilder(original.Length + 10);
foreach (char currentChar in original.ToCharArray())
{
if (currentChar == 'A') resultString.Append("[A4]");
else if (currentChar == '4') resultString.Append("[A4]");
else resultString.Append(currentChar);
}
string result = resultString.ToString();
You can run this routine with any replacements you want to make (in this case the letters 'A' and '4' and it should work. If you would want to replace strings the code would be similar in structure but you would need to "look ahead" and probably use a for loop. Hopefully this helps!
By the way - you want to use a string builder here and not strings because strings are static which means space gets allocated every time you loop. (Not good!)
I think this should do the trick
string str = "AA4PC";
string result = Regex.Replace(str, #"(?<Before>[^A4]?)(?<Value>A|4)(?<After>[^A4]?)", (m) =>
{
string before = m.Groups["Before"].Value;
string after = m.Groups["After"].Value;
string value = m.Groups["Value"].Value;
if (before != "[" || after != "]")
{
return "[A4]";
}
return m.ToString();
});
It is going to replace A and 4 that hasn't been replaced yet for [A4].

Unable to Split the string accordingly

I know this question would have been asked infinite number of times, but I'm kinda stuck.
I have a string something like
"Doc1;Doc2;Doc3;12"
it can be something like
"Doc1;Doc2;Doc3;Doc4;Doc5;56"
Its like few pieces of strings separated by semicolon, followed by a number or id.
I need to extract the number/id and the strings separately.
To be exact, I can have 2 strings: one having "Doc1;Doc2;Doc3" or "Doc1;Doc2;Doc3;Doc4" and the other having just the number/id as "12" or "34" or "45" etc.
And yeah I am using C# 3.5
I understand its a pretty easy and witty question, but this guy is stuck.
Assistance required from experts.
Regards
Anurag
string.LastIndexOf and string.Substring are the keys to what you're trying to do.
var str = "Doc1;Doc2;Doc3;12";
var ind = str.LastIndexOf(';');
var str1 = str.Substring(0, ind);
var str2 = str.Substring(ind+1);
One way:
string[] tokens = str.Split(';');
var docs = tokens.Where(s => s.StartsWith("Doc", StringComparison.OrdinalIgnoreCase));
var numbers = tokens.Where(s => s.All(Char.IsDigit));
String docs = s.Substring(0, s.LastIndexOf(';'));
String number = s.Substring(s.LastIndexOf(';') + 1);
One possible approach would be this:
var ids = new List<string>();
var nums = new List<string>();
foreach (var s in input.Split(';'))
{
int val;
if (!int.TryParse(s, out val)) { ids.Add(s); }
else { nums.Add(s); }
}
where input is something like Doc1;Doc2;Doc3;Doc4;Doc5;56. Now, ids will house all of the Doc1 like values and nums will house all of the 56 like values.
you can use StringTokenizer functionality.
http://www.c-sharpcorner.com/UploadFile/pseabury/JavaLikeStringTokenizer11232005015829AM/JavaLikeStringTokenizer.aspx
split string using ";"
StringTokenizer st = new StringTokenizer(src1,";");
collect final String. that will be your ID.
You may try one of two options: (assuming your input string is in string str;
Approach 1
Get LastIndexOf(';')
Split the string based on the index. This will give you string and int part.
Split the string part and process it
Process the int part
Approach 2
Split the string on ;
Run a for loop - for (int i = 0; i < str.length - 2; i++) - this is the string part
Process str[length - 1] separately - this is the int part
Please take this as a starting point as there could be other approaches to implement a solution for this
string actual = "Doc1;Doc2;Doc3;12";
int lstindex = actual.LastIndexOf(';');
string strvalue = actual.Substring(0, lstindex);
string id = actual.Substring(lstindex + 1);

Categories

Resources