簡體   English   中英

可選/條件字符串連接

[英]Optional / Conditional String Concatination

我正在嘗試有條件地連接String 例如,在一個方法中,我有兩個參數和一個本地String變量。 如果我為這些參數提供值,那么這些值將被添加到本地String變量中。

public Object concatinateString(String a, String b){

   String xyz = "firstValue";

   if((a == null || a == "") && (b != null || b != "")) {

        xyz = xyz.concat(".").concat(b);
   }

   if((b == null || b == "") && (a != null || a != "")) {

        xyz = xyz.concat(".").concat(a);
   }

   xyz = xyz.concat(".").concat(a).concat(".").concat(b);
} 

我的期望:

  concatinateString(null, b) --> xyz = firstValue.b;

  concatinateString("", b) --> xyz = firstValue.b

  concatinateString(a, null) --> xyz = firstValue.a

  concatinateString(a, "") --> xyz = firstValue.a

  concatinateString("", "") --> xyz = firstValue

  concatinateString(null, null) --> xyz = firstValue

  concatinateString(a, b) --> xyz = firstValue.a.b

我也嘗試了以下代碼,但沒有得到預期的結果。

public Object concatinateString(Optional<String> stageName, Optional<String> systemName) {
        Optional<String> property = Optional.of("firstValue");

        if ((!stageName.isPresent() || stageName.equals("")) && systemName.isPresent()) {

            property = Stream.of(property, systemName).flatMap(x -> x.map(Stream::of).orElse(null)).reduce((a, b) -> a + "." + b);

        }
        if (((!systemName.isPresent() || systemName.equals(""))) && stageName.isPresent()) {
            property = Stream.of(property, stageName).flatMap(x -> x.map(Stream::of).orElse(null)).reduce((a, b) -> a + "." + b);
        }
        property = Stream.of(property, stageName, systemName)
                .flatMap(x -> x.map(Stream::of).orElse(null))
                .reduce((a, b) -> a + "." + b);

        return property;

    }


 public static void main(String[] args) {
        Person person = new Person();
        System.out.println(person.concatinateString(Optional.ofNullable(null),Optional.ofNullable(null)));
}

結果我得到:

  concatinateString(null, b) --> xyz = firstValue.b.b

  concatinateString("", b) --> xyz = firstValue..b

  concatinateString(a, null) --> xyz = firstValue.a.a

  concatinateString(a, "") --> xyz = firstValue.a.

  concatinateString("", "") --> xyz = firstValue..

  concatinateString(null, null) --> xyz = firstValue

  concatinateString(a, b) --> xyz = firstValue.a.b 

如果我正確理解了您的問題,那么您的問題出在條件上。 事實上,如果你想得到 firstvalue + b,a 必須等於 "" 或 null,但 b 必須不同於 "" AND null。 但是你的條件是 b.= "" || b:= 空。 這是我的看法:

if ((a != "" && a != null) && (b == "" || b == null)){

    firstvalue + a;

} else if ((b != "" && b != null) && (a == "" || a == null)) {

    firstvalue + b;

} else if ((a != "" && a != null) && (b != "" && b != null)) {

    firstvalue + a + b;

} else if ((a == "" || a == null) && (b == "" || b == null)) {

    firstvalue;

}

它應該返回您所期望的:

concatinateString(a, null) --> xyz = firstValue.a

concatinateString(a, "") --> xyz = firstValue.a

concatinateString(null, b) --> xyz = firstValue.b;

concatinateString("", b) --> xyz = firstValue.b

concatinateString(a, b) --> xyz = firstValue.ab

concatinateString("", "") --> xyz = firstValue

concatinateString(null, null) --> xyz = firstValue

也添加這個:

concatinateString(null, "") --> xyz = firstValue

concatinateString("", null) --> xyz = firstValue

我希望這個解決方案可以。 再見 !

如果您需要更多解釋,讓我們解碼您的第一個條件:

if((a == null || a == "") && (b != null || b != "")) {

        xyz = xyz.concat(".").concat(b);
   }

這里,重要的是這部分:(b.= null || b,= "")。 確實,讓我們看看 b == null 的情況。

If b == null, (b != null) returns 0, and (b != "") returns 1.

In this case, (0 || 1) returns 1. But, it should return 0.

現在如果 b == ""。

If b == "", (b != null) returns 1, and (b != "") returns 0.

Here, (1 || 0) returns 1. But again, it should return 0.

現在如果你放 && 而不是 || 在 (b,= null || b != ""),

If b == null, (b != null) returns 0, and (b != "") returns 1.

(1 && 0) returns 0.

If b == "", (b != null) returns 1, and (b != "") returns 0.

(0 && 1) returns 0.

那么只有當 b 與 "" 和 null 都不同時,您將擁有: (1 && 1) 返回 1 並將 b 連接到 xyz。

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM