Example usage for java.lang String split

List of usage examples for java.lang String split

Introduction

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

Prototype

public String[] split(String regex) 

Source Link

Document

Splits this string around matches of the given regular expression.

Usage

From source file:Main.java

public static void main(String[] args) {
    String givenstring = "this is a test this is a another test";
    String[] words = givenstring.split(" ");

    ArrayList<String> arr = new ArrayList<String>();
    for (int i = 0; i < words.length; i++) {
        arr.add(words[i]);// ww  w .  j a va2 s  .c  o  m
    }
    while (arr.size() != 0) {
        String word = arr.get(0);
        int frequency = Collections.frequency(arr, word);
        arr.removeAll(Collections.singleton(word));
        System.out.println(word + frequency);
    }
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {
  Set result = new HashSet();
  Provider[] providers = Security.getProviders();
  for (int i = 0; i < providers.length; i++) {
    Set keys = providers[i].keySet();
    for (Iterator it = keys.iterator(); it.hasNext();) {
      String key = (String) it.next();
      key = key.split(" ")[0];

      if (key.startsWith("Alg.Alias.")) {
        // Strip the alias
        key = key.substring(10);/*from w  w w.  ja  va  2 s  .c  o m*/
      }
      int ix = key.indexOf('.');
      result.add(key.substring(0, ix));
    }
  }
  System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {
    String input = "ThisIsATest";

    // split into words
    String[] words = input.split("(?=[A-Z])");

    words[0] = capitalizeFirstLetter(words[0]);

    // join//from   w w  w .ja  v  a 2 s.  co  m
    StringBuilder builder = new StringBuilder();
    for (String s : words) {
        builder.append(s).append(" ");
    }

    System.out.println(builder.toString());

}

From source file:Main.java

public static void main(String[] args) {
    String query = "name==p==?header=hello?aname=?????lname=lastname";
    String[] params = query.split("\\?");
    Map<String, String> map = new HashMap<String, String>();
    for (String param : params) {
        String name = param.split("=")[0];
        String value = param.substring(name.length(), param.length());
        map.put(name, value);//  w  ww  . j  a v a  2 s  .  c  om
        System.out.println(name);
        if (name.equals("")) {
            value += "?";
        }
        System.out.println(value.replaceAll(" ", ""));
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("thefile.csv"));
    String line = null;

    while ((line = br.readLine()) != null) {
        String[] values = line.split(",");
        for (String str : values) {
            System.out.println(str);
        }/*  w  ww  .  ja  v  a 2  s.  co m*/
    }
    br.close();
}

From source file:Main.java

public static void main(String[] args) {
    String str = "A,B,C,D";

    // Split str using a comma as the delimiter
    String[] parts = str.split(",");

    // Print the the string and its parts
    System.out.println(str);/*from w w w.  j a  v  a  2  s  . c  om*/

    for (String part : parts) {
        System.out.println(part);
    }
}

From source file:common.ReverseWordsCount.java

public static void main(String[] args) throws IOException {
    List<String> readLines = FileUtils.readLines(new File("G:\\\\LTNMT\\LTNMT\\sougou\\sougou2500.txt"));
    Map<String, Integer> words = new HashMap<>();

    for (String line : readLines) {
        String[] split = line.split(" ");
        for (String wprd : split) {
            Integer get = words.get(wprd);
            if (get == null) {
                words.put(wprd, 1);//from www  .j av a 2 s .  c  o  m
            } else {
                words.put(wprd, get + 1);
            }
        }
    }
    Set<Map.Entry<String, Integer>> entrySet = words.entrySet();
    List<Map.Entry<String, Integer>> reverseLists = new ArrayList<>(entrySet);
    Collections.sort(reverseLists, new Comparator<Map.Entry<String, Integer>>() {
        @Override
        public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue());
        }
    });
    PrintStream ps = new PrintStream("c:/reverseWords.txt");
    for (Map.Entry<String, Integer> teEntry : reverseLists) {
        ps.println(teEntry.getKey() + " " + teEntry.getValue());
    }
    ps.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String testStr = "J J@H.com";
    System.out.println("Original string: " + testStr);
    String[] result = testStr.split("[\\W && [^.@]]+");
    System.out.println(Arrays.toString(result));
}

From source file:Main.java

public static void main(String[] args) {

    String sentence = "is this a sentence this is a test";
    String[] myStringArray = sentence.split("\\s"); // Split the sentence by
                                                    // space.

    Map<String, Integer> wordOccurrences = new HashMap<String, Integer>(myStringArray.length);

    for (String word : myStringArray) {
        if (wordOccurrences.containsKey(word)) {
            wordOccurrences.put(word, wordOccurrences.get(word) + 1);
        } else {/*from w  w w  .ja v  a 2s .c o  m*/
            wordOccurrences.put(word, 1);
        }
    }
    for (String word : wordOccurrences.keySet()) {
        if (wordOccurrences.get(word) > 1) {
            System.out.println("1b. - Tokens that occurs more than once: " + word + "\n");
        }
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

    Document newDoc = domBuilder.newDocument();
    Element rootElement = newDoc.createElement("CSV2XML");
    newDoc.appendChild(rootElement);//from   ww w  .j av a  2  s.  c  o m

    BufferedReader csvReader = new BufferedReader(new FileReader("csvFileName.txt"));
    String curLine = csvReader.readLine();
    String[] csvFields = curLine.split(",");
    Element rowElement = newDoc.createElement("row");
    for (String value : csvFields) {
        Element curElement = newDoc.createElement(value);
        curElement.appendChild(newDoc.createTextNode(value));
        rowElement.appendChild(curElement);
        rootElement.appendChild(rowElement);
    }
    csvReader.close();
    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();
    Source src = new DOMSource(newDoc);
    Result dest = new StreamResult(new File("xmlFileName"));
    aTransformer.transform(src, dest);
}