【问题标题】:Concatenate 2 strings in android?在android中连接2个字符串?
【发布时间】:2014-05-14 18:30:36
【问题描述】:

我在带有strings.xml 的 Toast 中显示错误消息。

像这样 mErrorMsgId=R.string.username_or_password_incorrectfull;

但现在我需要将消息与R.string.add_selfhosted_blog 连接起来以避免翻译不一致。

阅读了一些类似的问题,但无法弄清楚。

编辑:

我想在 username_or_password_incorrectfull 字符串中的单词 tap 之后连接 nux_add_selfhosted_blog ...

<string name="username_or_password_incorrectfull">The username or password you entered is incorrect
 \- If you\'re a self hosted user, don\'t forget to tap **Add Self Hosted Site** and fill the URL field</string> 

&lt;string name="nux_add_selfhosted_blog"&gt;Add self-hosted site&lt;/string&gt;

我怎样才能做到这一点??

【问题讨论】:

  • 向我们展示更多代码以更好地帮助您,字符串连接实际上是一件非常简单的事情
  • 请阅读编辑...

标签: java android string-concatenation string-formatting


【解决方案1】:

您不能将R.string.username_or_password_incorrectfullR.string.add_selfhosted_blog 直接连接,因为它们不是String 实例,而是strings.xml 中实际字符串的资源ID。您可以获取 2 个字符串,然后将它们正常连接。

类似这样的:

String string1 = getResources().getString(R.string.username_or_password_incorrectfull);
String string2 = getResources().getString(R.string.add_selfhosted_blog);

String combined = string1 + string2;

【讨论】:

    【解决方案2】:

    如果您想简单地从strings.xml 资源中连接这两个String,那么您可以通过这个来实现...

    String errorMessage = getString(R.string.username_or_password_incorrectfull) + getString(R.string.add_selfhosted_blog);
    

    然后在 `Toast 中显示连接的错误消息

    Toast.makeText(context, errorMessage, Toast.LENGTH_SHORT).show();
    

    【讨论】:

    • mErrorMsgId 在操作问题中必须是 int 类型。
    【解决方案3】:

    你可以通过以下方式在另一个字符串的中间插入一个字符串,

    String mErrorMsgId = getResources().getString(R.string.username_or_password_incorrectfull); // +  getResources().getString(R.string.nux_add_selfhosted_blog);
    
    String firstPart = mErrorMsgId.substring( 0, mErrorMsgId.indexOf( "tap") + 4 );
    String selfhosted_blog = getResources().getString(R.string.nux_add_selfhosted_blog) + " ";
    String thirdPart = mErrorMsgId.substring( mErrorMsgId.indexOf( "tap") + 4 );
    mErrorMsgId = firstPart + selfhosted_blog + thirdPart;
    

    【讨论】:

    • 好吧,我添加了您在问题中给出的字符串,它显示了正确的输出,您是否收到任何错误?
    • 您的代码只是连接两个字符串。但我需要在字符串中间连接
    • 你的意思是在这个文本之后forget to tap
    • 不可能,我在 Eclipse 中运行了代码,并在其中显示了正确的结果。
    【解决方案4】:

    只需从两个 id 中检索字符串并与 + 连接,然后将结果字符串变量放入 Toast。

    String messageToUser = getResources().getString(R.string.username_or_password_incorrectfull) +  getResources().getString(R.string.add_selfhosted_blog);
    Toast.makeText(context, messageToUser, Toast.LENGTH_SHORT).show();
    

    【讨论】:

      【解决方案5】:

      方法 makeText 的第二个参数接受 stringResId 或只是 String

      例如:

      String error = getResources().getString(R.string.username_or_password_incorrectful);
      String error2 = getResources().getString(R.string.add_selfhosted_blog);
      String conctString = error + " " + error2;
      Toast.makeText(context, conctString, duration).show();
      

      【讨论】:

      【解决方案6】:

      您可以使用 MessageFormat。

      将 username_or_password_incorrectfull 设置为格式字符串

      ...忘记点按 {0} 并填写 URL...

      然后使用 MessageFormat.format(username_or_password_incorrectfull, nux_add_selfhosted_blog)

      【讨论】:

        【解决方案7】:

        您可以使用我创建的这个库来做到这一点:https://github.com/LikeTheSalad/android-string-reference 它会根据您定义的模板字符串连接您想要的所有字符串,然后在构建时生成最终的 XML 字符串。因此,对于您的情况,您可以像这样定义字符串:

        <string name="nux_add_selfhosted_blog">Add self-hosted site</string>
        <string name="template_username_or_password_incorrectfull">The username or password you entered is incorrect
         \- If you\'re a self hosted user, don\'t forget to tap ${nux_add_selfhosted_blog} and fill the URL field</string>
        

        然后在构建你的项目之后,将生成以下字符串:

        <string name="username_or_password_incorrectfull">The username or password you entered is incorrect
             \- If you\'re a self hosted user, don\'t forget to tap Add self-hosted site and fill the URL field</string>
        

        该工具会根据您对模板和/或值所做的任何更改来更新此自动生成的字符串。有关回购页面的更多信息。

        【讨论】:

          【解决方案8】:

          您可以像在 C#/Java 中一样简单地添加/连接字符串,

          这里的Address1和Address2是EditText(TextBox)

          String finalString = Address1.getText().toString() + " "+ Address2.getText().toString();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-02-07
            • 2018-03-13
            • 2019-03-16
            • 2011-01-28
            • 1970-01-01
            • 2011-02-03
            • 1970-01-01
            相关资源
            最近更新 更多