2

I want to access the font-family name out of the following string and then after applying a filter on the name i want to put it back. Here is my string :

font-size:36px;font-style:normal;font-variant:normal;font-weight:600;font-stretch:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#6b055f;fill-opacity:1;stroke:none;font-family:Abel;-inkscape-font-specification:'Abel, Semi-Bold'

How can i do this in c# ?

user1740381
  • 2,121
  • 8
  • 37
  • 61
  • 4
    [What have you tried](http://whathaveyoutried.com)? Please post your current attempt and explain where you are stuck. – Oded Dec 17 '12 at 14:05
  • Do you know that the string class is immutable? http://stackoverflow.com/questions/2365272/why-net-string-is-immutable – kenny Dec 17 '12 at 14:08
  • What do you mean by "put it back"? strings are immutable, so doing a substring will not not alter the original. There is no need to "put it back". Unless you are trying to change it, but if that is the case, please be more specific. – cadrell0 Dec 17 '12 at 14:09
  • 3
    I think you guys are being a bit too clever with your knowledge of mutability. In this case it's terminology, the OP won't care that a new string is actually created when they say 'replace', IMO. – Grant Thomas Dec 17 '12 at 14:11

4 Answers4

4

You can use the String class, which exposes all the methods you need to get cracking with this. For instance, use String.IndexOf to find the index of a character or string, and String.Substring to extract, then you can use String.Replace.

That should be sufficient to make a start, if you have a specific question related to a problem, then ask that.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
2

You can use Regex.Replace:

string test = "stroke:none;font-family:Abel;-inkscape-font-specification:'Bickham Script Pro Semibold, Semi-Bold'";

// search for the font style
Regex rex = new Regex(";font-family:.*;");

// replace the font with a new font
string newString = rex.Replace(test,";font=famliy:Arial;");
D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

I would use the power of ASP.NET instead of parsing the string myself. Why reinvent the wheel?

string style = "font-size:36px;font-style:normal;font-variant:normal;font-weight:600;font-stretch:normal;text-align:center;line-height:125%;letter-spacing:0px;word-spacing:0px;writing-mode:lr-tb;text-anchor:middle;fill:#6b055f;fill-opacity:1;stroke:none;font-family:Abel;-inkscape-font-specification:'Abel, Semi-Bold'";
System.Web.UI.WebControls.Label label = new System.Web.UI.WebControls.Label();
label.Style.Value = style;
label.Style["font-family"] = "Verdana";
style = label.Style.Value;
label.Dispose();

This will work also in WinForms, you just have to add reference to the System.Web assembly.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

You could do something like this:

public static class CssStyle
{
    public static string Update(string style, string key, string value)
    {
        var parts = style.Split(';');

        for (int i = 0; i < parts.Length; i++)
        {
            if (parts[i].StartsWith(key))
            {
                parts[i] = key + ":" + value;
                break;
            }
        }

        return string.Join(";", parts);
    }
}

Which would allow you to have a generic function which could update any part of the style. You could also extend it to add the style if it does not already exist.

Trevor Pilley
  • 16,156
  • 5
  • 44
  • 60