在foreach循环中连接字符串时出错

时间:2015-12-23 09:36:18

标签: c# .net string

我想在代码字中显示公司名称,例如Dell Computers = DC,但在连接字符串时出错。

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2015-12-23T15:07:03 -->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="javabeantest1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="78330407-cdc3-4758-b2a3-2ddae526bed9">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="test factory3"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="javabean" uuid="adf401bd-6c3e-4316-8ff1-285d86d56c48">
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TestFactory1"/>
        <queryString language="SQL">
            <![CDATA[]]>
        </queryString>
        <field name="name" class="java.lang.String"/>
        <field name="age" class="java.lang.Integer"/>
    </subDataset>
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="name" class="java.lang.String"/>
    <field name="age" class="java.lang.Integer"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="87" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="33" splitType="Stretch">
            <staticText>
                <reportElement x="70" y="3" width="100" height="30" uuid="2c3c0b2c-ef69-423b-ba00-47bfb52dfade"/>
                <text><![CDATA[name]]></text>
            </staticText>
            <staticText>
                <reportElement x="170" y="1" width="100" height="30" uuid="8c4db8c2-212f-4427-89b7-5eaf3091a1aa"/>
                <text><![CDATA[age]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="31" splitType="Stretch">
            <textField>
                <reportElement x="70" y="0" width="100" height="30" uuid="39fd9f41-27dd-406f-aa32-d6e930dc346a"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="170" y="0" width="100" height="30" uuid="1c916b19-cb2f-481c-8d0d-349eced5a8a2"/>
                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="249" splitType="Stretch">
            <componentElement>
                <reportElement x="290" y="49" width="200" height="200" uuid="0d721a59-5052-42a4-bd71-b61e51308115">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="javabean" uuid="31830583-c665-4c8d-af3a-1a5a0aba5306">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource() ]]></dataSourceExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="89affd55-2820-4860-a01c-ce794c5941a9">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="33b03723-89a8-4eba-b08a-e9360ecf3e08"/>
                                <text><![CDATA[name]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="54e2604e-96f8-4e83-b506-ca2beeef5751"/>
                                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="41fcbd02-87dc-4a72-8464-d9609e5e592a">
                        <jr:tableHeader style="Table 1_TH" height="30"/>
                        <jr:tableFooter style="Table 1_TH" height="30"/>
                        <jr:columnHeader style="Table 1_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="f4078108-0c94-406e-907f-d20a0932afeb"/>
                                <text><![CDATA[age]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:columnFooter style="Table 1_CH" height="30"/>
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="b1250d44-7bb0-445f-8c46-ab6c880872a8"/>
                                <textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </summary>
</jasperReport>

4 个答案:

答案 0 :(得分:4)

在foreach循环之外定义NewID。您无法同时定义和分配变量。

 string str = txtCompanyname.Text.Trim();
 string[] output = str.Split(' ');
 string newid= string.empty;
 foreach (string s in output)
  {
     // Console.Write(s[0] + " ");
        Response.Write(s[0]);
        newid += s[0].ToString();//getting error here

  }

答案 1 :(得分:0)

        string str = txtCompanyname.Text.Trim();

        string[] output = str.Split(' ');
        foreach (string s in output)
        {

            Response.Write(s);
            string newid += s;

        }

答案 2 :(得分:0)

你可以使用linq。

只需拆分全名并选择每个部分的第一个字符;

string str = "Dell Computers";
var abbr = new string(str.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
    .Select(x => char.ToUpper(x[0]))
    .ToArray());

答案 3 :(得分:0)

您可以使用LINQ执行此操作:

public string FirstLetters(string s)
{
    return String.Join("", s.Trim().Split(' ').Select(w => w.Substring(0, 1).ToUpper()));
}

var result = FirstLetter(txtCompanyname.Text);

此代码执行以下操作:

  1. 修剪你的字符串
  2. 按空格将字符串拆分为单独的单词集合
  3. 通过从每个单词的第一个大写字母生成一个集合
  4. 将字母集合合并为字符串