简体   繁体   English

如果语句字符串=用于在国家/地区显示不同内容的文本

[英]If Statement String = Text for displaying different things on country

Maybe a stupid question but i want to use a if statement in my code, but i won't work. 也许是一个愚蠢的问题,但是我想在我的代码中使用if语句,但是我无法工作。

    String Country = getResources().getConfiguration().locale.getLanguage();

    if (Country = nl);

So i want to use the if statement to do a function when the Country String is "nl" but i don't know how to use the if statement i think. 因此,当Country String为“ nl”时,我想使用if语句执行一个函数,但是我不知道如何使用我认为的if语句。

It's 它的

if (Country.equals("nl"))
   ...

Now: 现在:

  1. Equality in general is tested with double == . 通常,使用double ==来测试相等性。
  2. String equality should be tested with equals . 字符串相等性应使用equals进行测试。

Some more info for future use: using == to test equality checks if the references are the same (ie they are the same object in memory), which usually is not what you need (of course, using it for primitive values works as expected). 供将来使用的更多信息:使用==测试相等性检查引用是否相同(即它们是内存中的相同对象),通常这不是您所需要的(当然,将其用于原始值可以按预期工作)。 For cases when you want to have custom equality checks on types you can override the equals method and place your logic there. 对于想要对类型进行自定义相等性检查的情况,可以覆盖equals方法并将逻辑放在此处。 This is what happens in case of the String class. String类的情况下会发生这种情况。

Because String's are objects and not primitives, then you can't use == 因为字符串是对象而不是基元,所以不能使用==

But you have to use .equals instead, like Tudor has shown 但是您必须改用.equals,例如Tudor所示

    code something like this

     String Country = getResources().getConfiguration().locale.getLanguage();
    if(Country!=null)
    {
       if (Country.equals("nl")){ //something } else { }

    }


for string comparing use .equals method instead of == operator because == compare object are same or not instead of object value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM