Click here to Skip to main content
Click here to Skip to main content

Simple string parsing in Java

, 19 Mar 2012
Rate this:
Please Sign up or sign in to vote.
How to parse a string in Java.

Introduction

When I started to use the Java Native Interface (JNI) on my projects, I had the need to return a lot of information from a function, keeping the code simple and updatable.

Background

It came to my mind to use a simple way to organize information the same way as the MUMPS programming language uses: in pieces, separated by a specific character (separator).

info1|info2||info4...|infon

If some piece doesn't exist (like in the previous example, info3 doesn't exist), the function will return a null string.

Using the code 

To get the piece of information, you just need to give as argument the string that contains all the information, the separator, and the index of the piece.

public class MainClass {

    public static void main(String[] args) {
        
        String str = "info1|info2||info4|info5";
        
        System.out.println("1:" + str_piece(str, '|', 1));
        System.out.println("2:" + str_piece(str, '|', 2));
        System.out.println("3:" + str_piece(str, '|', 3));
        System.out.println("4:" + str_piece(str, '|', 4));
        System.out.println("5:" + str_piece(str, '|', 5));
        System.out.println("6:" + str_piece(str, '|', 6));
    
    }
    
    private static String str_piece(String str, char separator, int index) {
        String str_result = "";
        int count = 0;
        for(int i = 0; i < str.length(); i++) {
            if(str.charAt(i) == separator) {
                count++;
                if(count == index) {
                    break;
                }
            }
            else {
                if(count == index-1) {
                    str_result += str.charAt(i);
                }
            }
        }
        return str_result;
    }
}

The output is:

1:info1
2:info2
3:
4:info4
5:info5
6: 

Conclusion

I hope this tip can help you. Any comments are welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Filipe Marques
Software Developer (Junior)
Portugal Portugal
No Biography provided

Comments and Discussions

 
QuestionThe same can be done with String.split(String)... PinmemberLito19-Mar-12 13:50 
AnswerRe: The same can be done with String.split(String)... PinmemberFilipe Marques20-Mar-12 0:04 
GeneralRe: The same can be done with String.split(String)... PinmemberLito20-Mar-12 3:27 
GeneralRe: The same can be done with String.split(String)... PinmemberFilipe Marques20-Mar-12 11:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 19 Mar 2012
Article Copyright 2012 by Filipe Marques
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid