Title case capitalising letter after ‘ - c#

Is there a .net bug causing the following code to capitalise the S, giving "Ben’S Pies"?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Globalization;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
string value = "ben’s pies";
string titleCase = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
Console.WriteLine(titleCase);
}
}
}
You can see the output at: https://rextester.com/ (where I tried it).

If you print a dump of the value:
string value = "ben’s pies";
Console.Write(string.Join(" ", value.Select(c => ((int)c).ToString("x4"))));
You'll get
0062 0065 006e 2019 0073 0020 0070 0069 0065 0073
Now, let's have a look for Unicode U+2019
https://www.fileformat.info/info/unicode/char/2019/index.htm
And we see that ’ is not an apostroph, but "RIGHT SINGLE QUOTATION MARK (U+2019)." That's why ToTitleCase does work right (it capitalizes the word after a punctuation - a quotation mark). To amend your example put apostroph instead of quotation:
string value = "ben's pies";
// Ben's Pies
string titleCase = CultureInfo.GetCultureInfo("en-US").TextInfo.ToTitleCase(value);

ToTitleCase does indeed seem to treat the right single quotation mark as a separator between the words it converts to title case. The documentation says:
the ToTitleCase method provides an arbitrary casing behavior which is not necessarily linguistically correct. A linguistically correct solution would require additional rules, and the current algorithm is somewhat simpler and faster. We reserve the right to make this API slower in the future.

Related

functions along a variable on c#

I am new into programming with c# and I am trying to do a Console.WriteLine where I have $ and a variable inside {}, instead of the usual way that is done in c#.
However when I try to add a fuction it does'nt work, because I don't know the correct syntax to do it.
example (function Math.round)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
decimal Euro, Dolar;
decimal tax = 1.19590m;
Console.WriteLine("Convert euro into dólar- day 06/05/2018.\nWhat is the amount in euros? ");
Euro = decimal.Parse(Console.ReadLine());
Dolar = Euro * tax;
Console.WriteLine($"The final value is: (Math.Round,2){Dolar} dólars");
Console.ReadKey();
}
}
}
I'm not sure where did you read about that syntax... but that should be something like:
$"The final value is: {Math.Round(Dolar, 2)} dólars"
The parentheses inside the string literal have no special meaning... all the parsed code must be within the brackets for interpolated strings. Think of it as:
"The final value is: " + Math.Round(Dolar, 2).ToString() + " dólars"
Or
var final = Math.Round(Dolar, 2);
$"The final value is: {final} dólars"
If you are working with an IDE (Visual Studio, VSCode, etc.) which is coloring the syntax, it should be obvious there (everything inside the brackets is colored as regular code, not as the string)
Also, you don't really need Math.Round here, but that wouldn't answer your question :-)
Try this:
Console.WriteLine("The final value is: {0} dólars", Math.Round(Dolar, 2));

Parse words out of a string

I need to parse a string from NHRM__Location__c to just Location. I feel really dumb because this is not my first rodeo. I do not want to add the code in an array. NHRM is a namespace that may change depending on who's using it so at some point that will be a key in the app.config file.
I was thinking of just splitting the string on __ and then loading the rest of the word into a character array and trimming that guy down but I feel like I may be waaaay over complicating things.
I have put this into a .Net Fiddle for you using this code:
using System;
public class Program
{
public static void Main()
{
string stringToSplit = "NHRM__Location__c"; // 2 underscores in each location
char[] sep = new char[] {'_', '_'};
string newString = stringToSplit.Split(sep)[2];
Console.WriteLine(newString);
}
}
I know this is similar to the 1st answer, but I just wanted to prove that [2] does work, well at least for me in .Net Fiddle.
I hope this helps!
Update
System.String
Return Value - An array whose elements contain the substrings from this instance that are delimited by one or more characters in separator
So if I were to change the code to [4] then it would return just c.. and [0] would return NHRM.. the array values do not include the separator
Use this:
string sample = "NHRM__Location__c";
Console.WriteLine(sample.Split('_')[2]);

How can I use C# contains method with the functionality of sql LIKE condition?

I want to be able to use the contains function the same way the like operator works in sql. So when I call .contains(%This%%%%%%is%%my%string%%) from a list or whatever such as "This is my string " then the function will return true. I've done a lot of searching and it seems like a lot of people would like this function. So how can this be done or how can a custom like function with the same functionality be made?
EDIT
Thank you, for the quick response. I was able to use a regular expressions inside of my own custom Like function. Below is the code for anyone else who wants to use something similar to SQL Like. In this code the user would input the databaze value and then spaces in that value are replaced with .* to ignore anything in-between the values.Just like using the % to replace spaces and values in SQL. I can then use .Like on my string value called testValue that I am searching through to return true or false depending on if the words or whatever are in my string. I also added ToUpper to ignore the case.
//C# Like function
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace sqllinqstuff
{
class Program
{
static void Main(string[] args)
{
string testValue = "Big RED Car";
string databaze = "big red CAR";
string databaze3 = databaze.Replace(" ", ".*");
databaze3 = databaze3.Replace(" ", ".*");
Console.WriteLine(databaze3);
Console.WriteLine(testValue.Like(databaze3));
Console.Read();
}
}
public static class CaseyStringExtension
{
public static bool Like(this string str,string value)
{
if (!string.IsNullOrEmpty(str))
{
Regex r = new Regex(value.ToUpper());
if (r.IsMatch(str.ToUpper()))
return true;
}
return false;
}
}
}
The result of this test will be true.
The way this would be done is with Regex. The syntax is not the same as a SQL LIKE query so there will be a bit of a learning curve. This is a good tutorial site I often reference, it can get you started with the basics.
Translating your original string you asked about, the regex search pattern would be
.*This.*is.*my.*string.*
Once you get good at it you can do searches like this one I helped create for a password complexity checker
(?=^.{8,}$)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W_])(?=^.*[^\s].*$).*$
The above search looks for a string that has at least 8 characters with at least one lower case letter, one upper case letter, one special character, and no white-space characters.

Regex/Method to remove namespace from a Type.FullName - C#

I am working on writing a method to remove the namespace from a System.Type.FullName (not XML).
I started off googling and didn't get too far so switched to trying to write a Regex I could use with a Regex.Replace(). But I am far from a master of the Regex arts, so I present myself humbly before the regex gods.
Given the following inputs:
name.space.class
name.space.class<other.name.space.class1>
name.space.class<other.name.space.class1, shortSpace.class2>
I need to remove the namespaces so I get:
class
class<class1>
class<class1, class2>
Alternatively, if anyone knows of an existing library that has this functionality, all the better!
Note: I know System.Type has a Namespace property that I could use to remove the namespace (ie System.Type.FullName - System.Type.Namespace), but my method takes a type name as a string and needs to work with type names that the run-time does not know about (can't resolve).
How about this...
[.\w]+\.(\w+)
...and substiuting with $1. See it in action on regex101.
From looking at some C# examples it seems you would do
string output = Regex.Replace(input, #"[.\w]+\.(\w+)", "$1");
Try this:
public static string RemoveNamespaces(string typename)
{
return string.Join("",
Regex.Split(typename,
#"([^\w\.])").Select(p =>
p.Substring(p.LastIndexOf('.') + 1)));
}
I wouldn't even consider using regexes for this. Imperative code is pretty trivial here, although it requires a bit of string-fu:
public string RemoveNamespace(string typename)
{
if(typename.Contains("<")
{
var genericArguments =
typename.
// in reality, we need a substring before
// first occurence of "<" and last occurence of ">"
SubstringBetween("<", ">").
Split(',').
Select(string.Trim).
Select(RemoveNamespace);
return
RemoveNamespace(typename.SubstringBefore("<")) +
"<" +
string.Join(", ", genericArguments) +
">";
}
else
{
return typename.Trim().SubstringAfterLastOccurenceOf(".");
}
}
Sounds like a good situation to use positive lookahead:
(\w+[.+])+(?=\w+)
This pattern will match any number of words separated by periods or plusses, except the last one in a sequence (the short name of the type). Replacing the matches by the empty string will remove all namespace prefixes.
Why not split by dot(.) and take only the last string

Regular expression to match valid namespace name

I thought this question was asked before but I tried Google but didn't find an answer. Maybe I used wrong keywords.
Is it possible to use regular expression to match valid C# namespace name?
Update:
Thanks everyone for your answers and research! This question is much more complex than I expected. As Oscar Mederos and Joey pointed out, a valid namespace cannot contain C# reserved keywords, and can contain a lot more Unicode characters than Latin letters.
But my current project only need to syntactically validate namespaces. So I accepted primfaktor's answer, but I upvoted all answers.
For me, this worked:
^using (#?[a-z_A-Z]\w+(?:\.#?[a-z_A-Z]\w+)*);$
It matches using lines in C# and returns the complete namespace in the first (and only) match group. You may want to remove ^ and $ to allow for indentation and trailing comments.
Example on RegExr.
I know that the question was how to validate a namespace using a regex, but another way to do it is to make the compiler do the work. I am not certain that what I have here catches 100% of all errors, it does work pretty well. I created this ValidationRule for a project on which I am currently working:
using System.CodeDom.Compiler;
using System.Windows.Controls;
using Microsoft.CSharp;
using System.Text.RegularExpressions;
namespace Com.Gmail.Birklid.Ray.CodeGeneratorTemplateDialog
{
public class NamespaceValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
var input = value as string;
if (string.IsNullOrWhiteSpace(value as string))
{
return new ValidationResult(false, "A namespace must be provided.");
}
else if (this.doubleDot.IsMatch(input))
{
return new ValidationResult(false, "'..' is not valid.");
}
var inputs = (value as string).Split('.');
foreach (var item in inputs)
{
if (!this.compiler.IsValidIdentifier(item))
{
return new ValidationResult(false, string.Format(cultureInfo, "'{0}' is invalid.", item));
}
}
return ValidationResult.ValidResult;
}
private readonly CodeDomProvider compiler = CSharpCodeProvider.CreateProvider("CSharp");
private readonly Regex doubleDot = new Regex("\\.\\.");
}
}
If you want to know if a string can be used as a namespace, you should refer to The C# Language Specifications and look at the grammar that validates the namespace.
The namespace should be a sequence of identifiers separated by a .. Example:
identifier
identifier.identifier
identifier.identifier.identifier
...
And what is an identifier?
available_identifier or #any_identifier
An available_identifier is an any_identifier but cannot be a keyword reserved by the language.
any_identifier is the following:
(_|letter)(letter|number)*
Edit:
I must say that this regex can be really really complicated. Take in count that it is necessary to check if no reserved keywords are used, and here is the list of the reserved keywords:
abstract as base bool break byte case
catch char checked class const
continue decimal default delegate do
double else enum event explicit extern
false finally fixed float for foreach
goto if implicit in int interface
internal is lock long namespace new
null object operator out override
params private protected public
readonly ref return sbyte sealed short
sizeof stackalloc static string struct
switch this throw true try typeof uint
ulong unchecked unsafe ushort using
virtual void volatile while
Can't you split the validation, maybe creating a method in C# or any other language to validate it instead of using only one regex?
To be honest, I suggest you any of those two things:
Implement a parser of that grammar (see the reference). You can do it either by hand or using tools like ANTLR
Implement a method that takes the string you want to validate (let's call it str) and write a file like:
namespace str
{
class A {}
}
and try to compile it :) using msbuild or any C# compiler. If it gives an error, then you know that word is not correct :)
How about this...
(?:[A-Z][a-zA-Z0-9\._]+)+[a-z0-9_]

Categories