Solved

String Spilt

Posted on 2004-04-25
8
386 Views
Last Modified: 2012-08-14
Hi ,
 
I have a prob,

I have a string variable with the follorwing data inside it :

49 16:19:19.868042 TCP 2567 > 139 [ACK] Seq=126 Ack=33543 Win=64240 Len=0  tcp.analysis.acks_frame == 45  ip.dst == 192.168.1.3  ip.src == 192.168.1.4  frame.time == "Apr 25, 2004 16:19:19.86804200"

I want to search the string variable and get the value between the "ip.src == "
and a white space.

I wrote the following code.
BufferedReader in = new BufferedReader(new FileReader(fileName));

while ((bufferinput2 = in.readLine()) != null)
{
// Check Source IP
sourceipStart = bufferinput2.indexOf("ip.src == ") + 11;
sourceipEnd = bufferinput2.lastIndexOf("  ");
sourceip = bufferinput2.substring(sourceipStart, sourceipEnd);
System.out.println("Source IP : " +sourceip);
}

But i am not able to search for a white space in the string.Can anybody help me to solve the prob.

Regards
Hari
            
0
Comment
Question by:sri_hari1986
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10911271
>> sourceipEnd = bufferinput2.lastIndexOf("  ");

- lastIndexOf is not correct. You have to search for the next space after the "ip.src == ". You could try this:

String search = "ip.src == " ;
sourceipStart = bufferinput2.indexOf ( search ) ;
sourceipEnd = bufferinput2.indexOf ( " ", sourceipStart + search.length () ) ;
sourceip = buffer2.substring ( sourceipStart, sourceipEnd ) ;
0
 
LVL 3

Accepted Solution

by:
fave_17 earned 500 total points
ID: 10911280
int sub = bufferinput2.indexOf("ip.src == ");
StringTokenizer st = new StringTokenizer(bufferinput2.substring(sub+10, bufferinput2.length()-1)," ");
    if (st.hasMoreTokens()) {
           System.out.println(st.nextToken());
     }
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 10911306
tyr this...

/** @return an ArrayList of 2-character Strings. */
   private static ArrayList wordLetterPairs(String str) {
       ArrayList allPairs = new ArrayList();
       // Tokenize the string and put the tokens/words into an array
       String[] words = str.split("s");
       // For each word
       for (int w=0; w < words.length; w++) {
           // Find the pairs of characters
           String[] pairsInWord = letterPairs(words[w]);
           for (int p=0; p < pairsInWord.length; p++) {
               allPairs.add(pairsInWord[p]);
           }
       }
       return allPairs;
   }


R.K
0
 
LVL 16

Expert Comment

by:gnoon
ID: 10911511
>I want to search the string variable and get the value between the "ip.src == " and a white space.

Searching result may be found or not ... so you should test it before get the value

while ((bufferinput2 = in.readLine()) != null) {
    sourceip = "";
    sourceipStart = bufferinput2.indexOf( "ip.src == " );
    if( sourceipStart > -1 ) {
        sourceipEnd = bufferinput2.indexOf ( " ", sourceipStart + 7) ;
        sourceip = buffer2.substring ( sourceipStart, sourceipEnd ) ;
    }
    System.out.println("Source IP : " +sourceip);
}

G noon
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 16

Expert Comment

by:gnoon
ID: 10911515
sourceip = buffer2.substring ( sourceipStart, sourceipEnd ) ;

should be

sourceip = bufferinput2.substring ( sourceipStart, sourceipEnd ) ;
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10912135
You can also use a regular expression:

            String x = "49 16:19:19.868042 TCP 2567 > 139 [ACK] Seq=126 Ack=33543 Win=64240 Len=0  tcp.analysis.acks_frame == 45  ip.dst == 192.168.1.3  ip.src == 192.168.1.4  frame.time == \"Apr 25, 2004 16:19:19.86804200\"";
            final String RE = ".+ip.src\\s==\\s([0-9\\.]+).+";
            Pattern p = Pattern.compile(RE);
            Matcher m = p.matcher(x);
            if (m.matches()) {
                  System.out.println(m.group(1));
            }
0
 
LVL 3

Expert Comment

by:fave_17
ID: 10914724
=)
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10915394
Sri Hari,

Do you know that sometimes it is good to do some thinking and application of programming on your own rather than let somebody give you completely working code. If you had tried my code a little, you would've known that I made a small mistake in:

>> sourceip = buffer2.substring ( sourceipStart, sourceipEnd ) ;

It should be: sourceip = bufferinput2.substring ( sourceipStart + search.length (), sourceipEnd ) ;

If you'd tried fiddling a little with it, you would've come to the solution yourself.

The reason why I don't feel comfortable using a StringTokenizer here is that you want to read only one IP address (only one token) so there's no use creating a new StringTokenizer object and loop through it.

BTW, the regular expression approach is really good for all cases.

Just a word of advice, nothing else. I don't mind which answer gets accepted.

Cheers~!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
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 …

747 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

13 Experts available now in Live!

Get 1:1 Help Now