-2

I am getting value from a Bluetooth device, values separated by : colon. I want to get the first value and add it to the same string:

public String process(String raw) {
    if (raw != null) {
        String[] str_array = raw.split(":");
        String humid1 = str_array[0];
        if (humid1 != null) {
            return raw.add(humid1);
        }
    } else {
        Log.w(TAG, "provided string was null");
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
nIoT
  • 1
  • 1
  • 1
    Possible duplicate of [How do I concatenate two strings in Java?](https://stackoverflow.com/questions/3753869/how-do-i-concatenate-two-strings-in-java) – Tom Jul 03 '19 at 08:17
  • 1
    This check `if (humid1 != null)` is useless. – Tom Jul 03 '19 at 08:17
  • Thanks! this is not duplicated. I am asking a how to insert first splitted value to the same string. – nIoT Jul 03 '19 at 09:07
  • You want to "combine" two Strings, badly failed with `raw.add(humid1)` and the duplicate question tells you how to do that correctly. How to get the substring is not important anymore, since you solved that part already. – Tom Jul 03 '19 at 09:11
  • No, I don't want to combine any string. I just want to grab the 1st value from raw and insert it to the raw again. Then when we call raw it should only contain the 1st value of string. thanks! – nIoT Jul 03 '19 at 09:16
  • 1
    You should include example input and output, both current (incorrect) and desired (correct) output to make your question clearer. – Lomtrur Jul 03 '19 at 09:20
  • Thank you for your advice. I will add them – nIoT Jul 03 '19 at 09:45

1 Answers1

0

There is no add() method in String class. One can perform String concatenation in Java using + operator.

String s = "abc:def:ghi";
s = s + s.split(":")[0]; // or s += s.split(":")[0]
System.out.println(s);

The above snippet will print abc:def:ghiabc Add null checks and index bounds checks as per your requirements.

Please check alternative ways at String Concatenation | StackOverflow

EDIT (As per OP's comment on question)

If you're looking to make changes to the input String object 'raw' and expect it to reflect in caller method, then it's not possible as String is immutable in Java. Correct way to achieve that would be to return the result from your method and assign that to String in caller method.

public void myMethod() {
  String s = "abc:def:ghi";
  s = process(s);
  System.out.println(s);
}

public String process(String raw) {
  if (raw != null) {
    String[] str_array = raw.split(":");
    String humid1 = str_array[0];
    if (humid1 != null) {
      return humid1;
    }
  }
  return null; \\ or throw exception as per your choice.
}

The above snippet should print abc. More details String Immutability in Java | StackOverflow.

Dhruvil Vaghela
  • 580
  • 6
  • 13
  • No, this is not what I mean. if the code is as mentioned you String s = "abc:def:ghi"; s = s + s.split(":")[0]; // or s += s.split(":")[0] System.out.println(s); then the print s answer should be abc in this case that is not the answer. Thanks! – nIoT Jul 03 '19 at 09:12
  • Please find my updated answer and let me know if it helps you. – Dhruvil Vaghela Jul 03 '19 at 15:23
  • Yes, It make some sense. Thank you for your answer. – nIoT Jul 04 '19 at 06:48
  • @nIoT I guess the best SO action from your end is to upvote helpful answers and accept answers which solved your issue. – Dhruvil Vaghela Jul 04 '19 at 08:28