Solved

how to extract a substring from a string

Posted on 2013-01-17
11
279 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
How do I remove an object from a 3 62
Java Restore security prompts not working 10 261
Strange router problem - can't access hotmail.com 14 108
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

739 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