I tried using the Java String Replace() function to replace some text. But I can't return it because it is a function. I tried returning it anyway but it didn't compile. I tried looking at the Builtin Text blocks source, but it was in JS. I am having trouble returning the replaced text.
This is a test extension I am making to test some things out...
So my question how do I return it. I apologize if this is a really basic question...
I am still a novice in Java...
Thanks in advance.
@SimpleObject(description = "Replaces text")
public void ReplaceText(String txt, String fr, String to){
return txt.replace(fr, to).toString();
// return added for testing, and to return the text
// tostring added for debugging
}
It does not return, probably because it is a function, so main goal is how to return it?
@SimpleFunction(description = "Replaces text")
public String ReplaceText(String txt, String fr, String to){
return txt.replace(fr, to).toString();
// return added for testing, and to return the text
// tostring added for debugging
}
@SimpleFunction(description = "Replaces text")
public String ReplaceText(String txt, String fr, String to){
return txt.replace(fr, to);
// return added for testing, and to return the text
// tostring added for debugging
}
To return something from a function, use the return type of the object instead of "void". In your case it is String. And you don't have to use toString(). because replace() returns String.
@SimpleFunction(description = "Replaces text")
public String ReplaceText(String txt, String fr, String to){
return txt.replace(fr, to);
// return added for testing, and to return the text
// tostring added for debugging
}
The block will be purple, just like Tim showed.
There is no @SimpleObject annotation for functions. You can only use @SimpleFunction, @SimpleProperty or @SimpleEvent. @SimpleObject is something completely different.