Example usage for java.lang String length

List of usage examples for java.lang String length

Introduction

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

Prototype

public int length() 

Source Link

Document

Returns the length of this string.

Usage

From source file:StringDemo2.java

public static void main(String args[]) {
    String strOb1 = "First String";
    String strOb2 = "Second String";
    String strOb3 = strOb1;//from w ww .  j  a  v  a2 s .co m

    System.out.println("Length of strOb1: " + strOb1.length());

    System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3));

    if (strOb1.equals(strOb2))
        System.out.println("strOb1 == strOb2");
    else
        System.out.println("strOb1 != strOb2");

    if (strOb1.equals(strOb3))
        System.out.println("strOb1 == strOb3");
    else
        System.out.println("strOb1 != strOb3");
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    final Shell shell = new Shell(display);
    shell.setText("Snippet 94");
    shell.setLayout(new FormLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);

    Button copy = new Button(shell, SWT.PUSH);
    copy.setText("Copy");
    copy.addListener(SWT.Selection, e -> {
        String textData = text.getSelectionText();
        if (textData.length() > 0) {
            TextTransfer textTransfer = TextTransfer.getInstance();
            cb.setContents(new Object[] { textData }, new Transfer[] { textTransfer });
        }/* www  .  j av a  2  s .  c  o m*/
    });

    Button paste = new Button(shell, SWT.PUSH);
    paste.setText("Paste");
    paste.addListener(SWT.Selection, e -> {
        TextTransfer transfer = TextTransfer.getInstance();
        String data = (String) cb.getContents(transfer);
        if (data != null) {
            text.insert(data);
        }
    });

    FormData data = new FormData();
    data.left = new FormAttachment(paste, 0, SWT.LEFT);
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(0, 5);
    copy.setLayoutData(data);

    data = new FormData();
    data.right = new FormAttachment(100, -5);
    data.top = new FormAttachment(copy, 5);
    paste.setLayoutData(data);

    data = new FormData();
    data.left = new FormAttachment(0, 5);
    data.top = new FormAttachment(0, 5);
    data.right = new FormAttachment(paste, -5);
    data.bottom = new FormAttachment(100, -5);
    text.setLayoutData(data);

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

From source file:com.ok2c.lightmtp.examples.SendMailExample.java

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

    if (args.length < 3) {
        System.out.println("Usage: sender recipient1[;recipient2;recipient3;...] file");
        System.exit(0);//  ww w  .jav  a 2 s .co m
    }

    String sender = args[0];
    List<String> recipients = new ArrayList<String>();
    StringTokenizer tokenizer = new StringTokenizer(args[1], ";");
    while (tokenizer.hasMoreTokens()) {
        String s = tokenizer.nextToken();
        s = s.trim();
        if (s.length() > 0) {
            recipients.add(s);
        }
    }

    File src = new File(args[2]);
    if (!src.exists()) {
        System.out.println("File '" + src + "' does not exist");
        System.exit(0);
    }

    DeliveryRequest request = new BasicDeliveryRequest(sender, recipients, new FileSource(src));

    MailUserAgent mua = new DefaultMailUserAgent(TransportType.SMTP, IOReactorConfig.DEFAULT);
    mua.start();

    try {

        InetSocketAddress address = new InetSocketAddress("localhost", 2525);

        Future<DeliveryResult> future = mua.deliver(new SessionEndpoint(address), 0, request, null);

        DeliveryResult result = future.get();
        System.out.println("Delivery result: " + result);

    } finally {
        mua.shutdown();
    }
}

From source file:KeyEventPost.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    final Text text = new Text(shell, SWT.BORDER);
    text.setSize(text.computeSize(150, SWT.DEFAULT));
    shell.pack();//from www  . j  a  v  a 2s  . c  om
    shell.open();
    new Thread() {
        public void run() {
            String string = "Love the method.";
            for (int i = 0; i < string.length(); i++) {
                char ch = string.charAt(i);
                boolean shift = Character.isUpperCase(ch);
                ch = Character.toLowerCase(ch);
                if (shift) {
                    Event event = new Event();
                    event.type = SWT.KeyDown;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
                Event event = new Event();
                event.type = SWT.KeyDown;
                event.character = ch;
                display.post(event);
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                }
                event.type = SWT.KeyUp;
                display.post(event);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                }
                if (shift) {
                    event = new Event();
                    event.type = SWT.KeyUp;
                    event.keyCode = SWT.SHIFT;
                    display.post(event);
                }
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

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

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    Shell shell = new Shell(display);
    shell.setText("Snippet 122");
    shell.setLayout(new FillLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
    Menu menu = new Menu(shell, SWT.POP_UP);
    final MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
    copyItem.setText("Copy");
    copyItem.addSelectionListener(widgetSelectedAdapter(e -> {
        String selection = text.getSelectionText();
        if (selection.length() == 0) {
            return;
        }/* w w w  .  ja  v  a 2s.c o m*/
        Object[] data = new Object[] { selection };
        Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
        cb.setContents(data, types);
    }));

    final MenuItem pasteItem = new MenuItem(menu, SWT.PUSH);
    pasteItem.setText("Paste");
    pasteItem.addSelectionListener(widgetSelectedAdapter(e -> {
        String string = (String) (cb.getContents(TextTransfer.getInstance()));
        if (string != null) {
            text.insert(string);
        }
    }));
    menu.addMenuListener(MenuListener.menuShownAdapter(e -> {
        // is copy valid?
        String selection = text.getSelectionText();
        copyItem.setEnabled(selection.length() > 0);
        // is paste valid?
        TransferData[] available = cb.getAvailableTypes();
        boolean enabled = false;
        for (int i = 0; i < available.length; i++) {
            if (TextTransfer.getInstance().isSupportedType(available[i])) {
                enabled = true;
                break;
            }
        }
        pasteItem.setEnabled(enabled);
    }));
    text.setMenu(menu);

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

From source file:Main.java

public static void main(String[] args) throws IOException {
    String input = "this is a test";
    StringBuilder result = new StringBuilder();
    char currentCharacter;
    int count;// ww  w  .j a  v  a  2 s.  c om

    for (int i = 0; i < input.length(); i++) {
        currentCharacter = input.charAt(i);
        count = 1;
        while (i < input.length() - 1 && input.charAt(i + 1) == currentCharacter) {
            count++;
            i++;
        }
        result.append(currentCharacter);
        result.append(count);
    }

    System.out.println("" + result);
}

From source file:com.redhat.poc.jdg.bankofchina.util.GenerateUserIdCsv.java

public static void main(String[] args) throws Exception {
    CommandLine commandLine;/*from   w ww. j  av a  2  s .  com*/
    Options options = new Options();
    options.addOption("s", true, "The start csv file number option");
    options.addOption("e", true, "The end csv file number option");
    BasicParser parser = new BasicParser();
    parser.parse(options, args);
    commandLine = parser.parse(options, args);
    if (commandLine.getOptions().length > 0) {
        if (commandLine.hasOption("s")) {
            String start = commandLine.getOptionValue("s");
            if (start != null && start.length() > 0) {
                csvFileStart = Integer.parseInt(start);
            }
        }
        if (commandLine.hasOption("e")) {
            String end = commandLine.getOptionValue("e");
            if (end != null && end.length() > 0) {
                csvFileEnd = Integer.parseInt(end);
            }
        }
    }

    for (int i = csvFileStart; i <= csvFileEnd; i++) {
        ReadCsvFile(i);
    }

    System.out.println();
    System.out.println("%%%%%%%%%  " + csvFileStart + "-" + csvFileEnd
            + " ? userid.csv ,? %%%%%%%%%");
    System.out.println("%%%%%%%%% userid.csv  " + userIdList.size() + " ? %%%%%%%%%");
    CSVWriter writer = new CSVWriter(new FileWriter(CSV_FILE_PATH + "userid.csv"));
    writer.writeAll(userIdList);
    writer.flush();
    writer.close();
}

From source file:MainClass.java

public static void main(String[] arg) {
    StringBuffer phrase = new StringBuffer("one two three four");
    String substring = "two";
    String replacement = "twenty";
    int position = phrase.lastIndexOf(substring); // Find start of "two"
    phrase.replace(position, position + substring.length(), replacement);

    System.out.println(phrase);//from   w w  w .  j  a  v  a2 s  .c  o m
}

From source file:ToStringDemo.java

public static void main(String[] args) {
    double d = 858.48;
    String s = Double.toString(d);

    int dot = s.indexOf('.');

    System.out.println(dot + " digits before decimal point.");
    System.out.println((s.length() - dot - 1) + " digits after decimal point.");
}

From source file:MenuDisableEnableClipBoard.java

public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
    Menu menu = new Menu(shell, SWT.POP_UP);
    final MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
    copyItem.setText("Copy");
    copyItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            String selection = text.getSelectionText();
            if (selection.length() == 0)
                return;
            Object[] data = new Object[] { selection };
            Transfer[] types = new Transfer[] { TextTransfer.getInstance() };
            cb.setContents(data, types);
        }/*  w ww.  j ava 2s.c o  m*/
    });
    final MenuItem pasteItem = new MenuItem(menu, SWT.PUSH);
    pasteItem.setText("Paste");
    pasteItem.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
            String string = (String) (cb.getContents(TextTransfer.getInstance()));
            if (string != null)
                text.insert(string);
        }
    });
    menu.addMenuListener(new MenuAdapter() {
        public void menuShown(MenuEvent e) {
            // is copy valid?
            String selection = text.getSelectionText();
            copyItem.setEnabled(selection.length() > 0);
            // is paste valid?
            TransferData[] available = cb.getAvailableTypes();
            boolean enabled = false;
            for (int i = 0; i < available.length; i++) {
                if (TextTransfer.getInstance().isSupportedType(available[i])) {
                    enabled = true;
                    break;
                }
            }
            pasteItem.setEnabled(enabled);
        }
    });
    text.setMenu(menu);

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