Click here to Skip to main content
13,096,399 members (71,250 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 19 Mar 2012

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.


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.


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).


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) {
                if(count == index) {
            else {
                if(count == index-1) {
                    str_result += str.charAt(i);
        return str_result;

The output is:



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


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

You may also be interested in...

Comments and Discussions

QuestionThe same can be done with String.split(String)... Pin
Lito19-Mar-12 13:50
memberLito19-Mar-12 13:50 
AnswerRe: The same can be done with String.split(String)... Pin
Filipe Marques20-Mar-12 0:04
memberFilipe Marques20-Mar-12 0:04 
GeneralRe: The same can be done with String.split(String)... Pin
Lito20-Mar-12 3:27
memberLito20-Mar-12 3:27 
GeneralRe: The same can be done with String.split(String)... Pin
Filipe Marques20-Mar-12 11:33
memberFilipe Marques20-Mar-12 11:33 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.170813.1 | Last Updated 19 Mar 2012
Article Copyright 2012 by Filipe Marques
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid