0

I have a very simple question about JAVA string filter I to want input this command to console.

add 100@10

which means execute add method input arguments 100 and 10 respectively.

I used

String[] s = str.split("@");

String a = s[0];// here can get 100

String b=s1];// get can get 10

I have two questions

  • first is how to do delete the at (@) character before put it to string.split()

  • second can anyone can provide some better solution to handle this kind of input ?

such as

add 1000@10   //only add,1000,10 need to take 
times 1000@10  //only add,1000,10 need to take 
8
  • 1
    if you remove the @ character, how do you want to split your command? Commented Nov 11, 2015 at 15:51
  • you can see my command is add 1000@10,which means 1000+10 i want get the three arguments in the string seperately ,could be [add][1000][10] Commented Nov 11, 2015 at 15:54
  • 1
    JavaScript has nothing to do with Java Commented Nov 11, 2015 at 15:54
  • and apples are not cherries....he posted java code and tagged the question as "Java" - whats the problem Commented Nov 11, 2015 at 15:56
  • @messerbill I removed the JavaScript tag Commented Nov 11, 2015 at 15:56

2 Answers 2

3

You can split on multiple tokens because it's a regex parameter:

String a[] = "aadd 100@200".split("[ @]");

returns ["aadd", "100", "200"]

Then you can do

String command = a[0];
String operand1 =  a[1];
String operand2 = a[2];
Sign up to request clarification or add additional context in comments.

Comments

1

not as sexy as ergonaut's solution, but you also can use two splits:

String[] arr1 = "add 100@10".split(" ");
String[] arr2 = arr1[1].split("@");
String[] result = [arr1[0], arr2[0], arr2[1]];

should be

["add","100","10"]; // this is result

greetings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.