Solved

how to extract a substring from a string

Posted on 2013-01-17
11
278 Views
Last Modified: 2013-01-21
I have a string    

"Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54"

I would like to know how to pull 666 with 0,0,0,322,54

 thanks
0
Comment
Question by:roy_sanu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 38786251
populate it into a hashmap, and then it would be easier for you to pull values by key
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 38786265
for e.g.,

String[] tokens = "Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54".replace( "Create module ", "" ).split( ", " ) ;

Map<String, String> map = new HashMap<String, String>() ;
for( String token : tokens ){
  String[] temp = token.split( "=" ) ;
  map.put( temp[0], temp[1] ) ;
}

System.out.println( map.get( "ASID" ) ) ;
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38786290
String s = "Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54";

String newS = s.substring(s.indexOf("=")+1,s.indexOf(","))+" with  "+s.substring(s.lastIndexOf("=")+1);

System.out.println(newS);

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 38789268
I would like to know how to pull 666 with 0,0,0,322,54
        String s = "Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54";
        String[] atoms = s.replaceAll(".*ASID=([^ ]+).*RECT=(.+)", "$1$2")
                          .split(",", 2);

Open in new window

0
 

Author Comment

by:roy_sanu
ID: 38792317
Thanks to all.

 but i had a condition  but i do not why it goes to the first loop only, but when i try for create Banner input it still goes to the first loop

String s = "Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54"";
// String s = "Create Banner ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54"";

          
           boolean module = s.contains("Create module");
          
          
           if (module)   {
          
                 String[] tokens = s.replace( "Create module", "" ).split( ", " ) ;
                 Map<String, String> map = new HashMap<String, String>() ;
                      for( String token : tokens ){
                        String[] temp1 = token.split( "=" ) ;
                        map.put( temp1[0], temp1[1] ) ;
                      }  
                      s = map.get( "ASID" );
                      
                   
           }
          
          
           else  {
                 boolean  banner= s.contains("Create banner");
                 String[] tokens = s1.replace( "Create banner", "" ).split( ", " ) ;
                 Map<String, String> map1 = new HashMap<String, String>() ;
                      for( String token : tokens ){
                        String[] temp = token.split( "=" ) ;
                        map1.put( temp[0], temp[1] ) ;
                      }  
                      s1 = map1.get( "ASID" );
                      
                 
           }
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38792378
boolean module = s.contains("Create module");

.contains() takes a CharSequence
0
 

Author Comment

by:roy_sanu
ID: 38792456
Thanks the above code is working
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 38792478
Cool.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38792480
On first sight, it looks like the last code you posted is too verbose, but shall take a closer look

Update: yes it is too verbose. The code also doesn't work.

The code i posted will work for either case (module/banner)
0
 

Author Comment

by:roy_sanu
ID: 38792546
your code gives error  while i print "[Ljava.lang.String;@24c21495"

{
      String s = ""Create module ASID=666, ACIN=231, CTSID=205, RECT=0,0,322,54";";
//      String[] tokens = s.replace( "Create Banner ", "" ).split( ", " ) ;
      String[] atoms = s.replaceAll(".*ASID=([^ ]+).*RECT=(.+)", "$1$2")
            .split(",", 2);
      
      System.out.println(atoms);
      
      

}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38792551
  System.out.println(java.util.Arrays.toString(atoms));

Open in new window

is what you need
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question