?
Solved

String Spilt

Posted on 2004-04-25
8
Medium Priority
?
433 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
[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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

770 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