Java 在android中连接2个字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22828986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 18:09:21  来源:igfitidea点击:

Concatenate 2 strings in android?

javaandroidstring-concatenationstring-formatting

提问by user3467240

I am displaying a Error message in a Toast with strings.xml.

我在 Toast 中显示错误消息strings.xml

Like this mErrorMsgId=R.string.username_or_password_incorrectfull;

像这样 mErrorMsgId=R.string.username_or_password_incorrectfull;

But now i need to concatenate the message with R.string.add_selfhosted_blogto avoid translation inconsistencies.

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

Read some similar questions but can't figure it out.

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

EDIT:

编辑:

I want to concatenate nux_add_selfhosted_blog after the word tap in the username_or_password_incorrectfull string ...

我想在 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> 

<string name="nux_add_selfhosted_blog">Add self-hosted site</string>

<string name="nux_add_selfhosted_blog">Add self-hosted site</string>

How can i acheive this ??

我怎样才能做到这一点?

回答by SudoRahul

You cannout concatenate R.string.username_or_password_incorrectfullwith R.string.add_selfhosted_blogdirectly as they are not Stringinstances as such but rather the resource id to the actual String in the strings.xml. You can get the 2 strings and then concatenate them normally.

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

Something like this:

像这样的东西:

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

String combined = string1 + string2;

回答by Hamid Shatu

If you want to simply concate those two Stringfrom strings.xmlresource then you can achieve by this...

如果你想简单地Stringstrings.xml资源中连接这两个,那么你可以通过这个实现......

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

Then show the concated error message in the `Toast

然后在`Toast

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

回答by Lucifer

You can insert a string in Middle of another string in following way,

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

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;

回答by The Holy Coder

Just retrieve string from both id's and concate with +as ususal and put resulted string variable in Toast.

只需从两个 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();

回答by kstachniuk

Second parameter of method makeText accepts stringResId or just String

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

For example:

例如:

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();

回答by dongwq

you can use MessageFormat.

您可以使用 MessageFormat。

set username_or_password_incorrectfull as the format string

将 username_or_password_incorrectfull 设置为格式字符串

... forget to tap {0} and fill the URL...

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

then use MessageFormat.format(username_or_password_incorrectfull, nux_add_selfhosted_blog)

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

回答by César Mu?oz

You can do so by using this library I've created: https://github.com/LikeTheSalad/android-string-referenceit will concatenate all of you strings you want to based on a template string you define, and then it generates a final XML string at build time. So for your case, you can define your strings like so:

您可以使用我创建的这个库来做到这一点: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>

And then after building your project, the following string will be generated:

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

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

The tool will keep this auto-generated string updated for any changes you make to the template and or the values. More info on the repo's page.

对于您对模板和/或值所做的任何更改,该工具将更新此自动生成的字符串。回购页面上的更多信息。

回答by Sheriff

You can simply add/concatenate string as you do in C#/Java,

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

Here Address1 and Address2 are EditText(TextBox)

这里 Address1 和 Address2 是 EditText(TextBox)

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