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) 

Source Link

Document

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

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String host = "host";
    int port = 25;
    String from = "from@from.net";
    String toAddr = "to@to.net";

    Socket servSocket = new Socket(host, port);
    DataOutputStream os = new DataOutputStream(servSocket.getOutputStream());
    DataInputStream is = new DataInputStream(servSocket.getInputStream());

    if (servSocket != null && os != null && is != null) {
        os.writeBytes("HELO\r\n");
        os.writeBytes("MAIL From:" + from + " \r\n");
        os.writeBytes("RCPT To:" + toAddr + "\r\n");
        os.writeBytes("DATA\r\n");
        os.writeBytes("X-Mailer: Java\r\n");
        os.writeBytes(//from   w w  w.  jav  a2s.  c  om
                "DATE: " + DateFormat.getDateInstance(DateFormat.FULL, Locale.US).format(new Date()) + "\r\n");
        os.writeBytes("From:" + from + "\r\n");
        os.writeBytes("To:" + toAddr + "\r\n");
    }

    os.writeBytes("Subject:\r\n");
    os.writeBytes("body\r\n");
    os.writeBytes("\r\n.\r\n");
    os.writeBytes("QUIT\r\n");
    String responseline;
    while ((responseline = is.readUTF()) != null) {
        if (responseline.indexOf("Ok") != -1)
            break;
    }
}

From source file:SortByHouseNo.java

public static void main(String[] args) {
    String houseList[] = { "9-11", "9-01", "10-02", "10-01", "2-09", "3-88", "9-03", "9-3" };
    HouseNo house = null;/*from   w  w  w  .ja v a2 s .c o  m*/
    ArrayList<HouseNo> sortedList = new ArrayList<>();
    for (String string : houseList) {
        String h = string.substring(0, string.indexOf('-'));
        String b = string.substring(string.indexOf('-') + 1);
        house = new HouseNo(h, b);
        sortedList.add(house);
    }

    System.out.println("Before Sorting :: ");
    for (HouseNo houseNo : sortedList) {
        System.out.println(houseNo);
    }

    Collections.sort(sortedList, new SortByHouseNo());
    System.out.println("\n\nAfter Sorting HouseNo :: ");
    for (HouseNo houseNo : sortedList) {
        System.out.println(houseNo);
    }
}

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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 80");
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.BORDER | SWT.MULTI);
    for (int i = 0; i < 2; i++) {
        TreeItem item = new TreeItem(tree, SWT.NONE);
        item.setText("item " + i);
        for (int j = 0; j < 2; j++) {
            TreeItem subItem = new TreeItem(item, SWT.NONE);
            subItem.setText("item " + j);
            for (int k = 0; k < 2; k++) {
                TreeItem subsubItem = new TreeItem(subItem, SWT.NONE);
                subsubItem.setText("item " + k);
            }/*from w ww  .j a va  2s.  c o  m*/
        }
    }

    tree.addSelectionListener(widgetSelectedAdapter(e -> {
        TreeItem[] selection = tree.getSelection();
        TreeItem[] revisedSelection = new TreeItem[0];
        for (int i = 0; i < selection.length; i++) {
            String text = selection[i].getText();
            if (text.indexOf('1') > 0) {
                TreeItem[] newSelection = new TreeItem[revisedSelection.length + 1];
                System.arraycopy(revisedSelection, 0, newSelection, 0, revisedSelection.length);
                newSelection[revisedSelection.length] = selection[i];
                revisedSelection = newSelection;
            }
        }
        tree.setSelection(revisedSelection);
    }));

    shell.setSize(300, 300);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:indexOfDemo.java

public static void main(String args[]) {
    String s = "Now is the time for all good men " + "to come to the aid of their country.";

    System.out.println(s);//from w ww. ja  v  a 2 s.c o  m
    System.out.println("indexOf(t) = " + s.indexOf('t'));
    System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t'));
    System.out.println("indexOf(the) = " + s.indexOf("the"));
    System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the"));
    System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10));
    System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60));
    System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10));
    System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60));
}

From source file:com.jd.common.hbase.page.PageRegionObserver.java

public static void main(String[] args) {
    String aa = "62.junggtest,,1370420757469.18876c31abbeddec3fcb0fed10e9445b";
    String bb = aa.substring(0, aa.indexOf(","));
    System.out.println(bb);//from   w w w .j  a  va  2s .  co m
}

From source file:com.adobe.aem.demomachine.RegExp.java

public static void main(String[] args) throws IOException {

    String fileName = null;/*from   w ww . j a  va  2 s .  c o m*/
    String regExp = null;
    String position = null;
    String value = "n/a";
    List<String> allMatches = new ArrayList<String>();

    // Command line options for this tool
    Options options = new Options();
    options.addOption("f", true, "Filename");
    options.addOption("r", true, "RegExp");
    options.addOption("p", true, "Position");
    CommandLineParser parser = new BasicParser();
    try {
        CommandLine cmd = parser.parse(options, args);

        if (cmd.hasOption("f")) {
            fileName = cmd.getOptionValue("f");
        }

        if (cmd.hasOption("f")) {
            regExp = cmd.getOptionValue("r");
        }

        if (cmd.hasOption("p")) {
            position = cmd.getOptionValue("p");
        }

        if (fileName == null || regExp == null || position == null) {
            System.out.println("Command line parameters: -f fileName -r regExp -p position");
            System.exit(-1);
        }

    } catch (ParseException ex) {

        logger.error(ex.getMessage());

    }

    String content = readFile(fileName, Charset.defaultCharset());

    if (content != null) {
        Matcher m = Pattern.compile(regExp).matcher(content);
        while (m.find()) {
            String group = m.group();
            int pos = group.indexOf(".zip");
            if (pos > 0) {
                group = group.substring(0, pos);
            }
            logger.debug("RegExp: " + m.group() + " found returning " + group);
            allMatches.add(group);
        }

        if (allMatches.size() > 0) {

            if (position.equals("first")) {
                value = allMatches.get(0);
            }

            if (position.equals("last")) {
                value = allMatches.get(allMatches.size() - 1);
            }
        }
    }

    System.out.println(value);

}

From source file:GetWebPageDemo.java

public static void main(String args[]) throws Exception {
    String resource, host, file;
    int slashPos;

    resource = "www.java2s.com/index.htm";
    slashPos = resource.indexOf('/'); // find host/file separator
    if (slashPos < 0) {
        resource = resource + "/";
        slashPos = resource.indexOf('/');
    }/*from  w w w.  j a  v a  2 s . co  m*/
    file = resource.substring(slashPos); // isolate host and file parts
    host = resource.substring(0, slashPos);
    System.out.println("Host to contact: '" + host + "'");
    System.out.println("File to fetch : '" + file + "'");

    SocketChannel channel = null;

    try {
        Charset charset = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = charset.newDecoder();
        CharsetEncoder encoder = charset.newEncoder();

        ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
        CharBuffer charBuffer = CharBuffer.allocate(1024);

        InetSocketAddress socketAddress = new InetSocketAddress(host, 80);
        channel = SocketChannel.open();
        channel.connect(socketAddress);

        String request = "GET " + file + " \r\n\r\n";
        channel.write(encoder.encode(CharBuffer.wrap(request)));

        while ((channel.read(buffer)) != -1) {
            buffer.flip();
            decoder.decode(buffer, charBuffer, false);
            charBuffer.flip();
            System.out.println(charBuffer);
            buffer.clear();
            charBuffer.clear();
        }
    } catch (UnknownHostException e) {
        System.err.println(e);
    } catch (IOException e) {
        System.err.println(e);
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException ignored) {
            }
        }
    }

    System.out.println("\nDone.");
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 244");
    shell.setLayout(new FillLayout());
    final StyledText text = new StyledText(shell, SWT.NONE);
    StyleRange style = new StyleRange();
    style.borderColor = display.getSystemColor(SWT.COLOR_RED);
    style.borderStyle = SWT.BORDER_SOLID;
    StyleRange[] styles = { style };/*from   ww  w  .j  a  v a2  s  . c om*/
    String contents = "This demonstrates drawing a box\naround every occurrence of the word\nbox in the StyledText";
    text.setText(contents);
    int index = contents.indexOf(SEARCH_STRING);
    while (index != -1) {
        text.setStyleRanges(0, 0, new int[] { index, SEARCH_STRING.length() }, styles);
        index = contents.indexOf(SEARCH_STRING, index + 1);
    }
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:WordCount.java

public static void main(String args[]) throws Exception {
    int words = 0;
    int lines = 0;
    int chars = 0;

    FileReader fr = new FileReader("yourFile.txt");
    int c = 0;//from  ww  w . j a v  a2s.co m
    boolean lastWhite = true;
    String whiteSpace = " \t\n\r";

    while ((c = fr.read()) != -1) {
        chars++;
        if (c == '\n') {
            lines++;
        }
        int index = whiteSpace.indexOf(c);
        if (index == -1) {
            if (lastWhite == true) {
                ++words;
            }
            lastWhite = false;
        } else {
            lastWhite = true;
        }
    }
    if (chars != 0) {
        ++lines;
    }
}

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

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 332");
    GridLayout layout = new GridLayout();
    layout.marginHeight = layout.marginWidth = 10;
    shell.setLayout(layout);//from  ww w. j av  a  2s  . c  om
    StyledText text = new StyledText(shell, SWT.MULTI | SWT.BORDER);
    final String segment = "Eclipse";
    String string = "Force RTL direction on this segment \"" + segment + "\".";
    text.setText(string);
    int[] segments = { string.indexOf(segment), segment.length() };
    StyleRange[] ranges = { new StyleRange(0, 0, display.getSystemColor(SWT.COLOR_RED), null) };
    text.setStyleRanges(segments, ranges);
    Font font = new Font(display, "Tahoma", 16, 0);
    text.setFont(font);
    text.addBidiSegmentListener(event -> {
        String string1 = event.lineText;
        int start = string1.indexOf(segment);
        event.segments = new int[] { start, start + segment.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202C' };
    });
    Combo combo = new Combo(shell, SWT.SIMPLE);
    combo.setFont(font);
    combo.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    combo.setItems("Option 1...", "Option 2...", "Option 3...", "Option 4...");
    combo.select(1);
    combo.addSegmentListener(event -> {
        event.segments = new int[] { 0, event.lineText.length() };
        event.segmentsChars = new char[] { '\u202e', '\u202c' };
    });
    shell.setSize(500, 250);
    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    font.dispose();
    display.dispose();
}