Solved

String Spilt

Posted on 2004-04-25
8
423 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 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
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 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

690 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