【问题标题】:Get rid of double quote marks " " at beginning and end of string去掉字符串开头和结尾的双引号“”
【发布时间】:2021-12-21 12:27:59
【问题描述】:

我正在尝试在 Blazor 中进行一些绘图操作,并希望根据用户选择绘制一些对象。因此,我试图将一个长字符串参数传递给 SVG 以绘制所需的元素。

对象类和方法

public class SupportRoller
    {
        public Point2D P1 { get; set; }
        public Point2D P2 { get; set; }
        public Point2D P3 { get; set; }

        public Point2D L1 { get; set; }
        public Point2D L2 { get; set; }

        public Point2D C1 { get; set; }
        public Point2D C2 { get; set; }

        public static List<SupportRoller> SupportCoordinates(Point2D pinPoint, double scale)
        {
            var point1 = new Point2D(pinPoint.X * scale, pinPoint.Y * scale);
            var point2 = new Point2D((pinPoint.X + 2.08) * scale, (pinPoint.Y + 4) * scale);
            var point3 = new Point2D((pinPoint.X - 2.08) * scale, (pinPoint.Y + 4) * scale);

            var line1 = new Point2D((pinPoint.X + 2.08) * scale, (pinPoint.Y + 5) * scale);
            var line2 = new Point2D((pinPoint.X - 2.08) * scale, (pinPoint.Y + 5) * scale);

            var circle1 = new Point2D((((line2.X - line1.X) / 3) + line2.X) * scale, (pinPoint.Y + 7) * scale);
            var circle2 = new Point2D((((line2.X - line1.X) / 3) + circle1.X) * scale, (pinPoint.Y + 7) * scale);

            var result = new List<SupportRoller>()
            {
                new SupportRoller() {P1 = point1, P2 = point2, P3 = point3, L1 = line1, L2 = line2, C1 = circle1, C2 = circle2}
            };

            return result;
        }

        public static string CoordinatesToString(List<SupportRoller> pointList)
        {
            string s1x = pointList[0].P1.X.ToString();
            string s1y = pointList[0].P1.Y.ToString();
            string s2x = pointList[0].P2.X.ToString();
            string s2y = pointList[0].P2.Y.ToString();
            string s3x = pointList[0].P3.X.ToString();
            string s3y = pointList[0].P3.Y.ToString();

            //string result1 = "<polygon points=\'" + s1x + "," + s1y + " " + s2x + "," + s2y + " " + s3x + "," + s3y + "\' />";

            //Console.WriteLine(result1);
            string l1x = pointList[0].L1.X.ToString();
            string l1y = pointList[0].L1.Y.ToString();
            string l2x = pointList[0].L2.X.ToString();
            string l2y = pointList[0].L2.Y.ToString();

            //string result2 = "<line x1 = \'" + l1x + "\' y1=\"" + l1y + "\' x2=\"" + l2x + "\' y2=\"" + l2y + "\' stroke=\'black\' stroke-width=\'0.2\' />";

            string c1x = pointList[0].C1.X.ToString();
            string c1y = pointList[0].C1.Y.ToString();
            string c2x = pointList[0].C2.X.ToString();
            string c2y = pointList[0].C2.Y.ToString();
            var radius = 4;

            string result = "<polygon points=\'" + s1x + "," + s1y + " " + s2x + "," + s2y + " " + s3x + "," + s3y + "\' /><line x1 = \'" + l1x + "\' y1=\'" + l1y + "\' x2=\'" + l2x + "\' y2=\'" + l2y + "\' stroke=\'black\' stroke-width=\'0.2\' /><circle cx=\'" + c1x + "\' cy=\'" + s1y + "\' r=\'" + radius + "\' /> <circle cx=\'" + s2x + "\' cy=\'" + s2y + "\' r=\'" + radius + "\' />";

            Console.WriteLine(result);

            //var resultMod1 = result.Remove('\"');
            //var resultMod1 = result.Replace("\"", "");
            Console.WriteLine(result);

            return result;
        }
    }

我想在 SVG 中显示字符串,但是没有显示任何内容,因为字符串是通过 start 和 end "" 传递的,因此被忽略了。

下面显示在html中调用变量的地方:

   <g>@mainDwg.EndSupport</g>

   <g>"<polygon points='190,50 192.08,54 187.92,54' />
   <line x1 = '192.08' y1='55' x2='187.92' y2='55' stroke='black' stroke-width='0.2' />
   <circle cx='186.5333333333333' cy='50' r='4' /><circle cx='192.08' cy='54' r='4' />"</g>

如下调用它

     mainDwg.EndSupport = SupportRoller.CoordinatesToString(endSupCoordRoller).Trim();

我尝试用多个函数修剪字符串但没有成功。

yourString = yourString.Replace('"', ' ').Trim();
yourString= yourString.Trim('"');
yourString=yourString.Replace("\"", "");
yourString = yourString.Replace("\"", string.Empty).Trim();

有人可以帮忙吗?而不是“你的字符串”,我只需要传递你的字符串。谢谢。

【问题讨论】:

  • 发布的代码似乎不对声称的输出负责。 string result = "&lt;polygon points=\' ...."; Console.WriteLine(result); 不可能输出以&lt;g&gt;"&lt;polygon points=' 开头的字符串
  • 我敢打赌,在您程序的其他地方,您在发布代码之前输出“”,在发布代码之后输出“”。
  • 对不起,输出以“ ...”开头,没有 变量被插入的地方。 @mainDwg.EndSupport
  • 那么&lt;g&gt;@mainDwg.EndSupport&lt;/g&gt; 是剃刀标记吗,mainDwg.EndSupport 真的是像&lt;polygon poi... r='4' /&gt; 这样的字符串,而您声称剃刀渲染在它周围添加了""?有 MCRE 吗?
  • 是的,是的,是的,什么是 MCRE?

标签: c# .net blazor


【解决方案1】:

我不确定你是如何得到引号的,但你必须将CoordinatesToString 的结果转换为MarkupString 才能呈现。 string 被视为简单文本。

这是 Blazor 示例代码:

<p>@someHtml</p>

<p>@((MarkupString)someHtml)</p>

<svg>@polygon</svg>

<svg>@((MarkupString)polygon)</svg>

@code{
    string someHtml => "<strong>lorem ipsum</strong>";

    string polygon => @"<polygon points='190,50 192.08,54 187.92,54' />
   <line x1 = '192.08' y1='55' x2='187.92' y2='55' stroke='black' stroke-width='0.2' />
   <circle cx='186.5333333333333' cy='50' r='4' /><circle cx='192.08' cy='54' r='4' />";
}

这里是渲染的html:

<!--!--><p>&lt;strong&gt;lorem ipsum&lt;/strong&gt;</p><!--!-->

<p><!--!--><strong>lorem ipsum</strong></p><!--!-->

<svg>&lt;polygon points='190,50 192.08,54 187.92,54' /&gt;
   &lt;line x1 = '192.08' y1='55' x2='187.92' y2='55' stroke='black' stroke-width='0.2' /&gt;
   &lt;circle cx='186.5333333333333' cy='50' r='4' /&gt;&lt;circle cx='192.08' cy='54' r='4' /&gt;</svg><!--!-->

<svg><!--!--><polygon points="190,50 192.08,54 187.92,54"></polygon>
   <line x1="192.08" y1="55" x2="187.92" y2="55" stroke="black" stroke-width="0.2"></line>
   <circle cx="186.5333333333333" cy="50" r="4"></circle><circle cx="192.08" cy="54" r="4"></circle></svg>

【讨论】:

  • 是的,非常感谢。我也没有注意到,如果标签中有空格,剃须刀不会渲染 SVG 组件:.
【解决方案2】:

@mcbr 说得对。您需要将字符串声明为标记。或者,如果您确定您将拥有&lt;polygon&gt;,那么我会考虑仅构建一个点字符串,并将它们作为字符串注入:&lt;polygon points='@pointString'/&gt; 另一种选择是为 SVG 内容创建 Blazor 包装器,以便您可以更轻松地使用 C# 逻辑:

<MyPoly>
    @foreach (var item in DataItems){
        <MyPolyPoint x="item.X" y="item.Y" />
    }
</MyPoly>

【讨论】:

  • 我也会保存这个选项。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2014-04-07
相关资源
最近更新 更多