0

so, right now I have this String:

String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";

I want to be able to get just this part of the the string but I'm at a loss as to how to do this.

Needed Output:

String[] requiredOutput;
requiredOutput[1] =  48526; // csfo_offering
requiredOutput[2] = csfo_add_ind A; 

or

requiredOutput[2] = A; // csfo_add_ind

EDIT: I have used some of your suggestions and am trying out subString but it seems like its a temp fix because if the length of the original string changes then it will throw a wrench in my calls. I will try regex next because it seems to go by pattern matching and I might be able to figure something out with that. Thanks everyone for all your help.

Suggestions are still appreciated!

6
  • tutorialspoint.com/java/java_string_substring.htm Commented Apr 4, 2014 at 15:30
  • don't you need quotations marks? Commented Apr 4, 2014 at 15:31
  • String csfo = ([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A]); - i'm not ure that compiles Commented Apr 4, 2014 at 15:32
  • yes, sorry forgot about. obviously that wouldn't compile lol.. Commented Apr 4, 2014 at 15:38
  • 1
    How about using regular expressions if the string follows the same pattern ? Commented Apr 4, 2014 at 15:46

2 Answers 2

1

Are the numbers always the same length? If so, use String.subString. If not use String.indexOf("csfo_add") to find the locations of the "csfo_add" parts and then find the relative locations of the required information.

Sign up to request clarification or add additional context in comments.

Comments

1

Hi there you can also use split if you always have the same pattern for your string.

for example

    String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
    System.out.println(csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0]);

Would get the requiredOutput[2] = A; // csfo_add_ind

and this would get the first one

    String[] requiredOutput = new String[2];
    String csfo = "([csfo_num = 333015303][ csfo_minimum = 4044504600][ csfo_offering = 48526][csfo_add_ind A])";
    requiredOutput[0] = "csfo_add_ind " + csfo.split("csfo_add_ind ")[1].split("\\]\\)")[0];
    requiredOutput[1] = csfo.split("\\]\\[csfo_add_ind ")[0].split("csfo_offering = ")[1];
    //System.out.println(requiredOutput[0] + " et " + requiredOutput[1] );

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.