[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

String Spilt

Posted on 2004-04-25
8
Medium Priority
?
445 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 2000 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
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 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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

829 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