Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to extract a substring from a string

Posted on 2013-01-17
11
Medium Priority
?
281 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 1000 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
Technology Partners: 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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