Strips a mnemonic character out of a given text. - Java Swing

Java examples for Swing:Introduction

Description

Strips a mnemonic character out of a given text.

Demo Code

/*//from w  w  w  . ja  v a  2  s .co m
 *  Jajuk
 *  Copyright (C) The Jajuk Team
 *  http://jajuk.info
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *  
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String text = "java2s.com";
        System.out.println(getMnemonic(text));
    }

    /** The character to use as mnemonic indicator. */
    public static final char INDICATOR = '_';

    /**
     * Strips a mnemonic character out of a given text. A text's mnemonic is the
     * first character following a <code>'_'</code> character.
     * 
     * @param text The text to strip the mnemonic character from.
     * 
     * @return An <code>int</code> defining the mnemonic character for the given
     * text. If there was no mnemonic indicator found, <code>-1</code>
     * will be returned.
     */
    public static int getMnemonic(String text) {
        for (int i = 0; i < text.length() - 1; i++) {
            if (text.charAt(i) == INDICATOR) {
                return text.charAt(i + 1);
            }
        }
        return -1;
    }
}

Related Tutorials