how to get the string value with out iterating for loop for twice using java

srikotesh
srikotesh used Ask the Experts™
on
Hi Experts,

String str = "rptFormat:xls|grpid:1234|langid:en|SchTime:00:00|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";
            
String str2 = "rptFormat:xls|langid:en|SchTime:00:00|grpid:1234|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";


how to get the string value with out iterating | then :
here two for loop iterations are happended
is there any to get direct value from the string without iterating

i have to get only grpid value as-->1234
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NorieAnalyst Assistant

Commented:
You could convert the strings to dictionaries and easily extract the value for grpid.
		String str = "rptFormat:xls|grpid:1234|langid:en|SchTime:00:00|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";
        
		String str2 = "rptFormat:xls|langid:en|SchTime:00:00|grpid:1234|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";

		Dictionary dic = new Hashtable();
		
		for(String pair:str.split("[|]")){
			
			//System.out.println(pair);
			dic.put(pair.split(":")[0],pair.split(":")[1]);
		}
		
		System.out.println(dic.get("grpid"));
		
		for(String pair:str2.split("[|]")){
			
			//System.out.println(pair);
			dic.put(pair.split(":")[0],pair.split(":")[1]);
		}
		
		System.out.println(dic.get("grpid"));

Open in new window

mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Simple answer:  NO

There would be no way to do this without some sort of iteration. Definitely, there may be solutions that are more efficient and some that are less efficient, but no way you can get away from iteration.

Why do you ask? Is this something that you are trying to make more efficient (not that that would mean much on this simple example, but maybe the real life is a bit different) or are you just asking for theoretical knowledge?

Author

Commented:
Hi Mccarl,

grpid fetching i am doing on bulk of records.
looking for more efficient way.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Analyst Assistant
Commented:
You could do it without iterating.
    String str = "rptFormat:xls|grpid:1234|langid:en|SchTime:00:00|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";
        
    String str2 = "rptFormat:xls|langid:en|SchTime:00:00|grpid:1234|ftype:xls|P_GENERATEDON:29-10-2019 18:27:55|custidval:10024|";

    int pos1 = str.indexOf("grpid")+6;
    int pos2 = str.indexOf("|",pos1);

    String grpid = str.substring(pos1, pos2);
    System.out.println(grpid);

    pos1 = str2.indexOf("grpid")+6;
    pos2 = str2.indexOf("|",pos1);

    grpid = str2.substring(pos1, pos2);
    System.out.println(grpid);

Open in new window

Author

Commented:
thanks

Author

Commented:
thanks
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015

Commented:
Just to let you know that the last code is still iterating the string, it's just that the iteration is hidden inside the indexOf() method.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial