Solved

how to extract a substring from a string

Posted on 2013-01-17
11
273 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
boolean module = s.contains("Create module");

.contains() takes a CharSequence
0
 

Author Comment

by:roy_sanu
Comment Utility
Thanks the above code is working
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Cool.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
Comment Utility
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
Comment Utility
  System.out.println(java.util.Arrays.toString(atoms));

Open in new window

is what you need
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now