Home > Software engineering >  Get all class name icon from fontawesome
Get all class name icon from fontawesome

Time:10-30

I'd like to get all class name icon from fontawesome to list or array in c# from fontawesome. I'd like to get all variants icon for example:

fas fa-abacus
far fa-abacus
fal fa-abacus
...

I tried to extract it from the css file, but it only gets the icon names themselves without prefixes.

var text = File.ReadAllText(@"fontawesome\all.css");
var allMatches = Regex.Matches(text, @"^\.fa\-(.*):", RegexOptions.Multiline);

Thank's for help. Monika

CodePudding user response:

public List<string> ListOfFontawesomeIcons(string FilePath)
{
   List<string> response = new List<string>();
   List<string> fontAweseomPrefixes = new {  "fas", "far" /*  add all prefixes*/ }
   var textLines = File.ReadAllLines($"{FilePath}");
   foreach(string l in textLines)
   {
       foreach(string p in fontAwesomPrefixes)
       {
          if(l.Contains(p))
            response.Add(l.Replace($"{p} "));    
       }
   }

   return response;
}

CodePudding user response:

I ran this in C# 9, VS2019 just based off of feeding it a test string, and it worked for me. I double-checked for fa- in the result list to make sure I excluded all of the other results.

Also, I just converted to List of strings for display. You don't really need to do that, depending on what you're doing with your results.

string text = "i class=\"fas fa-camera\"></i> \"far fa-camera\"></i> class=\"fa fa-camera\"></span>";
var allMatches = Regex.Matches(text, "(?<=\").*?(fa-)?.*?(?=\")", RegexOptions.None);
List<string> allMatchesStrings = new();

foreach (var item in allMatches)
{
    if (item.ToString().Contains("fa-"))
        allMatchesStrings.Add(item.ToString());
}

foreach (var item in allMatchesStrings)
{
    Console.WriteLine(item);
}

Output:

fas fa-camera
far fa-camera
fa fa-camera
  •  Tags:  
  • c#
  • Related