Example usage for java.lang String indexOf

List of usage examples for java.lang String indexOf

Introduction

In this page you can find the example usage for java.lang String indexOf.

Prototype

public int indexOf(String str, int fromIndex) 

Source Link

Document

Returns the index within this string of the first occurrence of the specified substring, starting at the specified index.

Usage

From source file:MainClass.java

public static void main(String[] arg) {
    String str = "abcdea";
    int startIndex = 3;

    int index = 0;
    index = str.indexOf('a', startIndex);

    System.out.println(index);/*www . j  av  a 2s  .com*/
}

From source file:MainClass.java

public static void main(String[] arg) {
    String str = "abcdeabcdef";
    int startIndex = 3;

    int index = 0;
    index = str.indexOf("ab", startIndex);

    System.out.println(index);/*w w w.j a  v  a 2  s .  c o m*/
}

From source file:Main.java

public static void main(String args[]) {
    String s = "this is a test from java2s.com.";

    System.out.println(s);/*  ww  w . ja  v a  2  s.  co m*/
    System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));

}

From source file:Main.java

public static void main(String args[]) {
    String s = "this is a test from java2s.com.";

    System.out.println(s);/* w w w .j av  a  2s.  co m*/
    System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));

}

From source file:MainClass.java

public static void main(String args[]) {
    String s = "public static void main(String args[]) {";
    System.out.println(s);/*from  w  w w  .  j  a v a  2s .com*/
    System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
}

From source file:MainClass.java

public static void main(String args[]) {
    String s = "public static void main(String args[]) {";
    System.out.println(s);//from  w  w w . j  a  v  a  2  s  . c  o  m
    System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
}

From source file:ExtractSubstring.java

public static void main(String[] args) {
    String text = "To be or not to be";
    int count = 0;
    char separator = ' ';

    int index = 0;
    do {//  ww  w .  j a v  a 2 s.  co m
        ++count;
        ++index;
        index = text.indexOf(separator, index);
    } while (index != -1);

    String[] subStr = new String[count];
    index = 0;
    int endIndex = 0;
    for (int i = 0; i < count; i++) {
        endIndex = text.indexOf(separator, index);
        if (endIndex == -1)
            subStr[i] = text.substring(index);
        else
            subStr[i] = text.substring(index, endIndex);
        index = endIndex + 1;
    }

    for (int i = 0; i < subStr.length; i++)
        System.out.println(subStr[i]);
}

From source file:org.eclipse.swt.snippets.Snippet212.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 212");
    shell.setLayout(new GridLayout());
    styledText = new StyledText(shell, SWT.WRAP | SWT.BORDER);
    styledText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    styledText.setText(text);//from  w w  w.  j  a va 2s .  c  om
    int offset = text.indexOf("\uFFFC", 0);
    addImage(display.getSystemImage(SWT.ICON_QUESTION), offset);
    offset = text.indexOf("\uFFFC", offset + 1);
    addImage(display.getSystemImage(SWT.ICON_INFORMATION), offset);

    // use a verify listener to dispose the images
    styledText.addVerifyListener(event -> {
        if (event.start == event.end)
            return;
        String text = styledText.getText(event.start, event.end - 1);
        int index = text.indexOf('\uFFFC');
        while (index != -1) {
            StyleRange style = styledText.getStyleRangeAtOffset(event.start + index);
            if (style != null) {
                Image image = (Image) style.data;
                if (image != null)
                    image.dispose();
            }
            index = text.indexOf('\uFFFC', index + 1);
        }
    });
    // draw images on paint event
    styledText.addPaintObjectListener(event -> {
        StyleRange style = event.style;
        Image image = (Image) style.data;
        if (!image.isDisposed()) {
            int x = event.x;
            int y = event.y + event.ascent - style.metrics.ascent;
            event.gc.drawImage(image, x, y);
        }
    });
    styledText.addListener(SWT.Dispose, event -> {
        StyleRange[] styles = styledText.getStyleRanges();
        for (int i = 0; i < styles.length; i++) {
            StyleRange style = styles[i];
            if (style.data != null) {
                Image image = (Image) style.data;
                if (image != null)
                    image.dispose();
            }
        }
    });
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Add Image");
    button.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
    button.addListener(SWT.Selection, event -> {
        FileDialog dialog = new FileDialog(shell);
        String filename = dialog.open();
        if (filename != null) {
            try {
                Image image = new Image(display, filename);
                int offset1 = styledText.getCaretOffset();
                styledText.replaceTextRange(offset1, 0, "\uFFFC");
                addImage(image, offset1);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    shell.setSize(400, 400);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:com.intuit.tank.harness.functions.JexlStringFunctions.java

public static void main(String[] args) {
    String test = "Test";
    int indexOf = test.indexOf("s", -1);
    System.out.println(indexOf);/*from   w  w  w.  ja  v a  2s . c  o m*/
}

From source file:Main.java

/**
 * return either the first space or the first nbsp
 *///from  w w w  .  ja va 2  s. c  o m
public static int findSpace(String haystack, int begin) {
    int space = haystack.indexOf(' ', begin);
    int nbsp = haystack.indexOf('\u00A0', begin);
    if (space == -1 && nbsp == -1) {
        return -1;
    } else if (space >= 0 && nbsp >= 0) {
        return Math.min(space, nbsp);
    } else {
        // eg one is -1, and the other is >= 0
        return Math.max(space, nbsp);
    }
}