删除字符串开头和结尾" "的双引号



我正在尝试在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中显示字符串,但没有显示任何内容,因为字符串是以"开始"one_answers"结束"传递的"因此被忽略。

下面以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();

有人能帮忙吗?只是代替";您的字符串"我只需要传递你的琴弦。谢谢

我不确定如何获得引号,但必须将CoordinatesToString的结果强制转换为MarkupString才能进行渲染。CCD_ 3被视为简单文本。

这是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>

@mcbr说得对。您需要将字符串声明为标记。或者,如果您确信您将有一个<polygon>,那么我会考虑只构建一个点的字符串,并将它们作为字符串注入:<polygon points='@pointString'/>另一个选项是为SVG内容创建Blazor包装器,这样您就可以更容易地使用C#逻辑:

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

相关内容

  • 没有找到相关文章

最新更新