Solved

String Spilt

Posted on 2004-04-25
8
406 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:Mayank S
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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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:Mayank S
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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

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 …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

809 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