how get a specific value from a string? - java

i have have a string looks :
mystring = "<EQHO state="degraded"...> at /NE[1]/EQHO[2]/#state to <EQHO state="working"...> at /NE[1]/EQHO[1]/#state"
and i want to get this value :
value="NE[1]/EQHO[1]"
how can i achieve that ?
thanks

Try this:
mystring.substring(mystring.lastIndexOf("at /")+4, mystring.lastIndexOf("/#"))
but you probably should use a more generic solution. To extract all the section that have this format you can use something like this:
String mystring = "<EQHO state=\"degraded\"...> at /NE[1]/EQHO[2]/#state to <EQHO state=\"working\"...> at /NE[1]/EQHO[1]/#state";
ArrayList<String> values = new ArrayList<String>();
while(mystring.indexOf("at /") < mystring.indexOf("/#")){
String val = mystring.substring(mystring.indexOf("at /") + 4, mystring.indexOf("/#"));
values.add(val);
mystring = mystring.substring(mystring.indexOf("/#")+2);
}
System.out.println(values);

You can change the value of a string like this
mystring = "NE[1]/EQHO[1]";
Remember to include the semicolon!

Related

How to print an item from list

I have a list. I want print list item horizontally with delimiter.
I got output like this:
OderHistory: Item1-1$
OderHistory: Item2-1$
OderHistory: Item3-1$
But i need like this:
OderHistory: Item1-1$Item2-1$Item3-1
Code here:
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
Log.d("OderHistory",name+"-"+quanty+"$");
}
Anyone help to me!
Thanks in advance...
Try this,
String result="";
for(int i=0;i<itemList.size();i++){
String name = itemList.get(i).ItemName;
String quanty = itemList.get(i).Quantity;
result=result.concat(name+"-"+quanty+"$");
}
Log.d("OderHistory:",result);

Retrieve data from string using template

I'd like to retrieve data from string based on params from template.
For example:
given string -> "some text, var=20 another part param=45"
template -> "some text, var=${var1} another part param=${var2}"
result -> var1 = 20; var2 = 45
How could I achive that result in Java. Are there some libs or I need to use regex?
I tried different template processors, but they don't have needed functionality, I need something like inverse to them.
I hope below sample will serve your purpose -
String strValue = "some text, var=20 another part param=45";
String strTemplate = "some text, var=${var1} another part param=${var2}";
ArrayList<String> wildcards = new ArrayList<String>();
StringBuffer outputBuffer = new StringBuffer();
Pattern pat1 = Pattern.compile("(\\$\\{\\w*\\})");
Matcher mat1 = pat1.matcher(strTemplate);
while (mat1.find())
{
wildcards.add(mat1.group(1).replaceAll("\\$", "").replaceAll("\\{", "").replaceAll("\\}", ""));
strTemplate = strTemplate.replace(mat1.group(1), "(\\w*)");
}
if(wildcards!= null && wildcards.size() > 0)
{
Pattern pat2 = Pattern.compile(strTemplate);
Matcher mat2 = pat2.matcher(strValue);
if (mat2.find())
{
for(int i=0;i<wildcards.size();i++)
{
outputBuffer.append(wildcards.get(i)).append(" = ");
outputBuffer.append(mat2.group(i+1));
if(i != wildcards.size()-1)
{
outputBuffer.append("; ");
}
}
}
}
System.out.println(outputBuffer.toString());

Getting a part of a string in java

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!
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.
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] );

Randomizing in Java

I have 4 Strings to represent people and 4 Strings to represent names.
I'm trying to randomize them so that every time I start my application, my four people will have different names, but no one can have the same name during runtime.
Example:
String person_one;
String person_two;
String person_three;
String person_four;
String name_one = "Bob";
String name_two = "Jane";
String name_three = "Tim";
String name_four = "Sara";
Hope this makes some sense.
You can use Collections.shuffle():
List<String> names = new ArrayList<String>();
names.add("Bob");
names.add("Jane");
names.add("Tim");
names.add("Sara");
Collections.shuffle(names);
person_one = names.get(0);
person_two = names.get(1);
person_three = names.get(2);
person_four = names.get(3);
You can use Collections.shuffle().

How to manually set an array in Java?

How do you manually assign variables to an array? I have a code fragment below.
I don't want to manually put shortStraight[0] = "211100", shortStraight[1] = "021110", and so on. Any help?
private String [] shortStraight;
public Sample () {
shorty = new String [12];
shorty = {211100, 021110, 002111, 121100, 112100, 111200, 012110, 011210, 011120, 001211, 001121, 001112 } //this line doesn't work.
Any help?
String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;
String[] shorty = {"211100", "021110", "002111", "121100", "112100", "111200", "012110", "011210", "011120", "001211", "001121", "001112"} ;
http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Categories

Resources