Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.regex.*;

public class Main {
    public static final Pattern regex_ws = Pattern.compile("\\s+"), regex_csv = Pattern.compile("\\s*(,\\s*)+"),
            regex_norm = Pattern.compile("[^a-z_0-9\\Q-_+,.\\E]+"), regex_path = Pattern.compile("[^^]/+");

    public static String wrap(String str, int w) {
        StringBuffer sb = new StringBuffer();
        String line = "";

        for (String s : regex_ws.split(str)) {
            if (!line.isEmpty() && line.length() + s.length() >= w) {
                if (sb.length() != 0)
                    sb.append('\n');
                sb.append(line);
                line = s;
            } else {
                line = (line.isEmpty()) ? s : line + ' ' + s;
            }
        }

        if (!line.isEmpty()) {
            if (sb.length() != 0)
                sb.append('\n');
            sb.append(line);
        }

        return sb.toString();
    }
}