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
Solved

how to extract a substring from a string

Posted on 2013-01-17
11
277 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jar file executable 12 58
why cannot we forward request once the response is committed 2 46
object oriented programming comparison 5 77
SHA2 certs for IIS AND Java? 2 113
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

789 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