Javascript slugifier to C# - c#

I am looking at converting the JS slugify function by diegok and he is using this JavaScript construct:
function turkish_map() {
return {
'ş':'s', 'Ş':'S', 'ı':'i', 'İ':'I', 'ç':'c', 'Ç':'C', 'ü':'u', 'Ü':'U',
'ö':'o', 'Ö':'O', 'ğ':'g', 'Ğ':'G'
};
}
It is a map of char to char translations. However, I don't know which JS construct is this and how could it be rewritten in C# preferably without spending too much time on rewriting? (There's more to it, this is just one of the functions).
Should I make an array, dictionary, something else?

Dictionary<char, char> turkish_map() {
return new Dictionary<char, char> {
{'ş','s'}, {'Ş','S'}, {'ı','i'}, {'İ','I'} {'ç','c'} , {'Ç','C' }, {'ü','u'}, {'Ü','U'}, {'ö','o'}, {'Ö','O'}, {'ğ','g'}, {'Ğ','G'} };
}
The use it like:
turkish_map()['İ'] // returns I
Or you can save it into field and use it without creating it every time.

Use these methods to remove diacritics, the result will be sSıIcCuUoOgG.
namespace Test
{
public class Program
{
public static IEnumerable<char> RemoveDiacriticsEnum(string src, bool compatNorm, Func<char, char> customFolding)
{
foreach (char c in src.Normalize(compatNorm ? NormalizationForm.FormKD : NormalizationForm.FormD))
switch (CharUnicodeInfo.GetUnicodeCategory(c))
{
case UnicodeCategory.NonSpacingMark:
case UnicodeCategory.SpacingCombiningMark:
case UnicodeCategory.EnclosingMark:
//do nothing
break;
default:
yield return customFolding(c);
break;
}
}
public static IEnumerable<char> RemoveDiacriticsEnum(string src, bool compatNorm)
{
return RemoveDiacritics(src, compatNorm, c => c);
}
public static string RemoveDiacritics(string src, bool compatNorm, Func<char, char> customFolding)
{
StringBuilder sb = new StringBuilder();
foreach (char c in RemoveDiacriticsEnum(src, compatNorm, customFolding))
sb.Append(c);
return sb.ToString();
}
public static string RemoveDiacritics(string src, bool compatNorm)
{
return RemoveDiacritics(src, compatNorm, c => c);
}
static void Main(string[] args)
{
var str = "şŞıİçÇüÜöÖğĞ";
Console.Write(RemoveDiacritics(str, false));
// output: sSıIcCuUoOgG
Console.ReadKey();
}
}
}
For other characters like ı which wasn't converted, and others as you mentioned as #, ™ you can use the method to remove diacritics then use a regex to remove invalid characters. If you care enough for some characters you can make a Dictionary<char, char> and use it to replace them each one of them.
Then you can do this:
var input = "Şöme-p#ttern"; // text to convert into a slug
var replaces = new Dictionary<char, char> { { '#', 'a' } }; // list of chars you care
var pattern = #"[^A-Z0-9_-]+"; // regex to remove invalid characters
var result = new StringBuilder(RemoveDiacritics(input, false)); // convert Ş to S
// and so on
foreach (var item in replaces)
{
result = result.Replace(item.Key, item.Value); // replace # with a and so on
}
// remove invalid characters which weren't converted
var slug = Regex.Replace(result.ToString(), pattern, String.Empty,
RegexOptions.IgnoreCase);
// output: Some-pattern

Related

Replace Multiple Characters in string

Hi all a have a program working but return string like ")Hi("
it should be "(Hi)" so i need to replace '(' with ')' and replace
')' with '('
it sounds easy
s.Replace('(',')').Replace(')','(')
the trick is that after the first replace the string change from
")Hi(" to "(Hi(" after the second the replace will change both characters back
the final will become ")Hi)"
Help Please
You could also use a regex replacement.
s = System.Text.RegularExpressions.Regex.Replace(s, #"[)](\w+)[(]", "($1)");
You cannot use Replace because it works its replacement operation on the whole string, not char by char.
A simple brute force solution could be this one
void Main()
{
// A dictionary where every key points to its replacement char
Dictionary<char, char> replacements = new Dictionary<char, char>()
{
{'(', ')'},
{')', '('},
};
string source = ")Hi(";
StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if(replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement,1);
}
Console.WriteLine(sb.ToString());
}
You can transform this in an extension method adding to a static class
public static class StringExtensions
{
public static string ProgressiveReplace(this string source, Dictionary<char, char> replacements)
{
StringBuilder sb = new StringBuilder();
foreach (char c in source)
{
char replacement = c;
if (replacements.ContainsKey(c))
replacement = replacements[c];
sb.Append(replacement, 1);
}
return sb.ToString();
}
}
and call it from your code with
Dictionary<char, char> replacements = new Dictionary<char, char>()
{{'(', ')'},{')', '('}};
s = s.ProgressiveReplace(replacements);
var s = ")Hi(";
var sb = new StringBuilder();
foreach (var c in s)
if (c == ')')
sb.Append('(');
else if (c == '(')
sb.Append(')');
else
sb.Append(c);
s = sb.ToString();
Method 1)
Use the following:
var requiredString = string.Join(string.Empty, str.Select(x=>{if (x == '(') x = ')'; else if (x == ')') x = '('; return x;}));
Method 2)
Or you can Use following Extension Method:
public static class StringExtensions
{
public static string ReplaceMultiple(this string source, Dictionary<char, char> replacements)
{
return string.Join(string.Empty , source.Select(x=>Replace(x,replacements)));
}
private static char Replace(char arg, Dictionary<char, char> replacements)
{
if(replacements.ContainsKey(arg))
arg = replacements[arg];
return arg;
}
}
This method can be used as follows:
var rep = new Dictionary<char, char>
{
{ ')', '(' },
{ '(', ')' },
// { '#', '*' },
// { '*', '#' }
};
var c = str.ReplaceMultiple(rep);
Regex.Replace lets you process each Match (needs using System.Text.RegularExpressions;) :
string result = Regex.Replace(")Hi(", #"\(|\)", m => m.Value == "(" ? ")" : "(");
Alternative can be replacing one of the characters with something else:
string result = ")Hi(".Replace('(', '\0').Replace(')', '(').Replace('\0', ')');

Return two Chars from a method receiving a String

I'm trying to return two Chars from a method that takes a string. Say the method receives the word "Hello", and i want to return the first and last letter = ho. I've made a method that takes a string and returns one char. But i don't know how to return two.
It can be done quite easily using a extension method returning a char[].
Complete example:
public class Program
{
public static void Main(string[] args)
{
string myString = "abc";
var firstAndLast = myString.GetFirstAndLast();
Console.WriteLine("First: " + firstAndLast[0] + " Last: " + firstAndLast[1]);
}
}
public static class StringExtensions
{
public static char[] GetFirstAndLast(this string str)
{
return new char[2] { str.FirstOrDefault(), str.LastOrDefault() };
}
}
This will output:
First: a Last: c
You can return char[] - array of characters.
The method signature will become something like this:
public static char[] GetFirstAndLastChars(string input)
{
var result = new char[2];
//your code to take first and last letters
result[0] = inputString.FirstOrDefault();
result[1] = inputString.LastOrDefault();
return result;
}
As mentioned in other answers, you can turn this into extension method if used frequently.
You could create a char[] from your input and return the first and last element of that char[]:
public char[] GetChars(string input)
{
char[] charArray = input.ToCharArray();
char[] result = new char[] { charArray.First(), charArray.Last() };
return result;
}
Perhaps you could try this:
public struct Ends
{
public char Front;
public char Back;
}
public Ends GetEnds(string text)
{
return new Ends() { Front = text.First(), Back = text.Last() };
}
You would obviously call it like:
Ends ends = GetEnds("hello");
I get this result:
Return any type of data structure holding both of the chars. I would probably return the string "ho", but you can also return an array or a tuple.

Removing matching characters between two strings

I want to remove the characters which are matching between the two given strings. Eg.
string str1 = "Abbbccd";
string str2 = "Ebbd";
From these two strings I want the output as:
"Abcc", only those many matching characters should be removed from str1,which are present in str2.
I tried the following code:
public string Sub(string str1, string str2)
{
char[] arr1 = str1.ToCharArray();
char[] arr2 = str2.ToCharArray();
char[] arrDifference = arr1.Except(arr2).ToArray();
string final = new string(arrDifference);
return final;
}
With this code I get the output as "Ac". It removes all the matching characters between two arrays and stores 'c' only once.
First create this helper method:
IEnumerable<Tuple<char, int>> IndexDistinct(IEnumerable<char> source)
{
var D = new Dictionary<char, int>();
foreach (var c in source)
{
D[c] = D.ContainsKey(c) ? (D[c] + 1) : 0;
yield return Tuple.Create(c, D[c]);
}
}
It converts a string "aabcccd" to [(a,0),(a,1),(b,0),(c,0),(c,1),(c,2),(d,0)]. The idea is to make every character distinct by adding a counting index on equal characters.
Then modify your proposed function like this:
string Sub(string str1, string str2)
{
return new string(
IndexDistinct(str1)
.Except(IndexDistinct(str2))
.Select(x => x.Item1)
.ToArray());
}
Now that you are doing Except on Tuple<char, int> instead of just char, you should get the behavior you specified.
You can do it with lists as well:
List<char> one = new List<char>("Abbbccd".ToCharArray());
List<char> two = new List<char>("Ebbd".ToCharArray());
foreach (char c in two) {
try { one.RemoveAt(one.IndexOf(c)); } catch { }
}
string result = new string(one.ToArray());
Use C#'s string commands to modify the string.
public string testmethod(string str1, string str2)
{
string result = str1;
foreach (char character in str2.ToCharArray())
{
result = result.Replace(character.ToString(), "");
}
return result;
}

display format for results in console

I am trying to display my results as follows:
-.-|- [tab] kt
-.|-- [tab] nm
-.|-|- [tab] ntt
But this is my current output
-.-|-| kt
-.|--| nm
-.|-|-| [tab]ntt
There is a | at the end of every Morse code which I would like to remove since it is at the end.
Also because the user can input Morse code with space between dots and dashes - i noticed that it affects the alignment of the characters and not all of them get tabbed properly. The word tab isn't supposed to show i just wrote it in because I didn't know how to place a real tab.
private static readonly IDictionary<char, string> morseCode_alpha = new Dictionary<char, string>
{
{'a', ".-"},{'b',"-..."}, {'c',"-.-."}, {'d',"-.."}, {'e',"."},
{'f',"..-."}, {'g',"--."}, {'h',"...."},{'i',".."}, {'j',".---"},
{'k',"-.-"}, {'l',".-.."}, {'m',"--"}, {'n',"-."}, {'o',"---"},
{'p',".--."}, {'q',"--.-"}, {'r',".-."}, {'s',"..."}, {'t',"-"},
{'u',"..-"}, {'v',"...-"}, {'w',".--"}, {'x',"-..-"}, {'y',"-.--"}, {'z',"--.."}
};
private static string ConvertMorseToText(string symbolCode)
{
var builder = new StringBuilder(4 * symbolCode.Length);
foreach (char c in symbolCode)
builder.Append(morseCode_alpha[c]);
return builder.ToString();
}
private static string ConvertTextToMorse(char ch)
{
if (morseCode_alpha.Keys.Contains(ch))
return morseCode_alpha[ch];
else
return string.Empty;
}
private static string ConvertStringToMorse(string letters)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in letters)
{
if (sb.Length != 0 && sb[sb.Length - 1] != ' ')
sb.Append("|");
sb.Append(ConvertTextToMorse(ch));
}
return sb.ToString();
}
private static IEnumerable<string> Permutations( string symbolCode)
{
int n = symbolCode.Length;
if (n == 0 || symbolCode.Length == 0)
yield return " ";
else
foreach (var entry in morseCode_alpha)
if (symbolCode.StartsWith(entry.Value))
foreach (string next in Permutations(symbolCode.Substring(entry.Value.Length)))
yield return entry.Key + next;
}
private static void Write( string rest)
{
string result = ConvertStringToMorse(rest);
Console.Write(result+"\t");
Console.WriteLine(rest);
}
static void Main(string[] args)
{
string morseInput;
string entered = "";
do
{
Console.WriteLine("Enter Morse Code: \n");
morseInput = Console.ReadLine().Replace(" ","");
bool isValid = Regex.IsMatch(morseInput, #"^[-.]+$");
if (isValid)
{
Console.WriteLine("\nAll permutations:\n");
string morse = ConvertMorseToText(entered);
string permutations = morseInput.Substring(morse.Length);
Write(permutations);
var nexts = new List<string>(Permutations(permutations));
foreach (string next in nexts)
Write(next);
}
else
{
Console.WriteLine("\nFormat of morse must be only dots and dashes.");
Console.WriteLine("Parameter name: "+morseInput+"\n");
}
}
while (morseInput.Length != 0);
}
And, to answer the other part of the question...
Tabstops are fixed for console writing, so it would be better to use something like String.PadRight
so, your code could be:
private static void Write(string rest)
{
string result = ConvertStringToMorse(rest);
Console.Write(result.PadRight(20));
Console.WriteLine(rest);
}
Draft version of the method:
private static string ConvertStringToMorse(string letters)
{
var result = string.Join("|",
letters
.Select(ConvertTextToMorse)
.Where(morse => !string.IsNullOrEmpty(morse)));
return result;
}
Update:
Please note that the entered variable is used only once: when defined - empty string is assigned. Then the ConvertMorseToText(entered) method is called: it always returns empty string for the empty string argument. After this assignment string permutations = morseInput.Substring(morse.Length); the permutations variable will store exactly the same value as morse variable (because morse.Length is always 0).
So, it seems that the entered variable and the ConvertMorseToText() method are useless (both can be safely removed):
static void Main(string[] args)
{
do
{
Console.WriteLine("Enter Morse Code: ");
string morseInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(morseInput))
{
// Empty or consists only of white-space characters
break;
}
morseInput = morseInput.Replace(" ", "");
bool isValid = Regex.IsMatch(morseInput, #"^[-.]+$");
if (isValid)
{
Console.WriteLine("All permutations:");
Console.WriteLine();
var nexts = Permutations(morseInput).ToList();
foreach (string next in nexts)
Write(next);
}
else
{
Console.WriteLine();
Console.WriteLine("Format of morse must be only dots and dashes.");
Console.WriteLine("Parameter name: {0}", morseInput);
}
}
while (true);
}
Update 2:
Consider using TryGetValue() method of Dictionary<TKey, TValue> instead of Keys.Contains and [] (indexer) i.e. do not perform look-up twice:
private static string ConvertTextToMorse(char ch)
{
string result;
return morseCode_alpha.TryGetValue(ch, out result) ? result : string.Empty;
}
Instead this code:
Console.Write(result+"\t");
Console.WriteLine(rest);
Use
Console.WriteLine("{0,-10}{1,-10}", result, rest);
Then you will see two columns (each max 10 charachters) with left alignment. Or remove "-" sign if you want right alignment.

C# Stripping / converting one or more characters

Is there a fast way (without having to explicitly looping through each character in a string) and either stripping or keeping it. In Visual FoxPro, there is a function CHRTRAN() that does it great. Its on a 1:1 character replacement, but if no character in the alternate position, its stripped from the final string. Ex
CHRTRAN( "This will be a test", "it", "X" )
will return
"ThXs wXll be a es"
Notice the original "i" is converted to "X", and lower case "t" is stripped out.
I looked at the replace for similar intent, but did not see an option to replace with nothing.
I'm looking to make some generic routines to validate multiple origins of data that have different types of input restrictions. Some of the data may be coming from external sources, so its not just textbox entry validation I need to test for.
Thanks
All you need is a couple of calls to String.Replace().
string s = "This will be a test";
s = s.Replace("i", "X");
s = s.Replace("t", "");
Note that Replace() returns a new string. It does not alter the string itself.
Does this what you want?
"This will be a test".Replace("i", "X").Replace("t", String.Empty)
Here is a simple implementation of the CHRTRAN function - it does not work if the string contains \0 and is quite messy. You could write a nicer one using loops, but I just wanted to try it using LINQ.
public static String ChrTran(String input, String source, String destination)
{
return source.Aggregate(
input,
(current, symbol) => current.Replace(
symbol,
destination.ElementAtOrDefault(source.IndexOf(symbol))),
preResult => preResult.Replace("\0", String.Empty));
}
And the you can use it.
// Returns "ThXs wXll be a es"
String output = ChrTran("This will be a test", "it", "X");
Just to have a clean solution - the same without LINQ and working for the \0 cases, too, and it is almost in place because of using a StringBuilder but won't modify the input, of course.
public static String ChrTran(String input, String source, String destination)
{
StringBuilder result = new StringBuilder(input);
Int32 minLength = Math.Min(source.Length, destination.Length);
for (Int32 i = 0; i < minLength; i++)
{
result.Replace(source[i], destination[i]);
}
for (Int32 i = minLength; i < searchPattern.Length; i++)
{
result.Replace(source[i].ToString(), String.Empty);
}
return result.ToString();
}
Null reference handling is missing.
Inspired by tvanfosson's solution, I gave LINQ a second shot.
public static String ChrTran(String input, String source, String destination)
{
return new String(input.
Where(symbol =>
!source.Contains(symbol) ||
source.IndexOf(symbol) < destination.Length).
Select(symbol =>
source.Contains(symbol)
? destination[source.IndexOf(symbol)]
: symbol).
ToArray());
}
Here was my final function and works perfectly as expected.
public static String ChrTran(String ToBeCleaned,
String ChangeThese,
String IntoThese)
{
String CurRepl = String.Empty;
for (int lnI = 0; lnI < ChangeThese.Length; lnI++)
{
if (lnI < IntoThese.Length)
CurRepl = IntoThese.Substring(lnI, 1);
else
CurRepl = String.Empty;
ToBeCleaned = ToBeCleaned.Replace(ChangeThese.Substring(lnI, 1), CurRepl);
}
return ToBeCleaned;
}
This is a case where I think using LINQ overcomplicates the matter. This is simple and to the point:
private static string Translate(string input, string from, string to)
{
StringBuilder sb = new StringBuilder();
foreach (char ch in input)
{
int i = from.IndexOf(ch);
if (from.IndexOf(ch) < 0)
{
sb.Append(ch);
}
else
{
if (i >= 0 && i < to.Length)
{
sb.Append(to[i]);
}
}
}
return sb.ToString();
}
To "replace with nothing", just replace with an empty string. This will give you:
String str = "This will be a test";
str = str.Replace("i", "X");
str = str.Replace("t","");
A more general version as a string extension. Like the others this does not do a translation in place since strings are immutable in C#, but instead returns a new string with the replacements as specified.
public static class StringExtensions
{
public static string Translate( this string source, string from, string to )
{
if (string.IsNullOrEmpty( source ) || string.IsNullOrEmpty( from ))
{
return source;
}
return string.Join( "", source.ToCharArray()
.Select( c => Translate( c, from, to ) )
.Where( c => c != null )
.ToArray() );
}
private static string Translate( char c, string from, string to )
{
int i = from != null ? from.IndexOf( c ) : -1;
if (i >= 0)
{
return (to != null && to.Length > i)
? to[i].ToString()
: null;
}
else
{
return c.ToString();
}
}
}

Categories