import java.util.Arrays;
public final class LongestSubstringFinder {
private LongestSubstringFinder() {}
public static String findLongestSubstringAtString(String string) {
string = string.replaceAll("\\s+", " ");
int quantityOfSuffixes = string.length();
String[] suffixes = new String[quantityOfSuffixes];
for (int i = 0; i < quantityOfSuffixes; i++) {
suffixes[i] = string.substring(i, quantityOfSuffixes);
}
Arrays.sort(suffixes);
String longestRepeatedSubstring = "";
for (int i = 0; i < quantityOfSuffixes - 1; i++) {
String x = findLongestCommonPrefixOfTwoStrings(suffixes[i], suffixes[i + 1]);
if (x.length() > longestRepeatedSubstring.length()) {
longestRepeatedSubstring = x;
}
}
return longestRepeatedSubstring;
}
private static String findLongestCommonPrefixOfTwoStrings(String first, String second) {
int quantity = Math.min(first.length(), second.length());
for (int i = 0; i < quantity; i++) {
if (first.charAt(i) != second.charAt(i))
return first.substring(0, i);
}
return first.substring(0, quantity);
}
}
@Test
public void testFindLongestSubstringAtStringFirst() throws Exception {
String testString = "Сайт toster, бла бла бла Сайт toster";
String neededResult = "Сайт toster";
String longestSubstring = LongestSubstringFinder
.findLongestSubstringAtString(testString);
assertEquals(longestSubstring, neededResult);
}
@Test
public void testFindLongestSubstringAtStringSecond() throws Exception {
String testString = "aboibooolsabbooolsadf";
String neededResult = "booolsa";
String longestSubstring = LongestSubstringFinder
.findLongestSubstringAtString(testString);
assertEquals(longestSubstring, neededResult);
}
useDelimiter(Pattern)
иfindInLine(Pattern)
, которые позволяют моментально искать разделители, поэтому для .csv
файлов лучше использовать его.String pathToTor = "D:\\Tor\\Tor Browser\\Browser\\firefox.exe"; //путь к браузеру
String link = "http:\\stackoverflow.com\\"; //ссылка
String request = String.format("%s %s", pathToTor, link);
Runtime runtime = Runtime.getRuntime();
runtime.exec(request);
Класс реализации:
public class BrowserOpener {
private String os;
private Runtime runtime;
public BrowserOpener() {
os = System.getProperty("os.name").toLowerCase();
runtime = Runtime.getRuntime();
}
public void openLinkInBrowser(String url) {
try {
if (isWindows()) {
runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
} else if (isMac()) {
runtime.exec("open " + url);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean isWindows() {
return os.contains("win");
}
private boolean isMac() {
return os.contains("mac");
}
}
Класс c методом Main:
import java.io.IOException;
import java.net.URISyntaxException;
public class Main {
public static void main(String[] args) throws IOException, URISyntaxException {
BrowserOpener opener = new BrowserOpener();
opener.openLinkInBrowser("http://stackoverflow.com/");
}
}
import javafx.util.Pair;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
Pair<Integer, Integer> position = new Pair<Integer, Integer>(-1, 3);
Map<Pair<Integer, Integer>, Integer> myMatrix = buildMatrix();
myMatrix.put(position, 9);
outputValue(myMatrix, position);
}
private static Map<Pair<Integer, Integer>, Integer> buildMatrix() {
return new HashMap<>();
}
private static void outputValue(Map<Pair<Integer, Integer>, Integer> map, Pair<Integer, Integer> key) {
System.out.println(map.get(key));
}
}
import java.io.BufferedReader;
import java.io.Reader;
public class GetSet extends BufferedReader {
private int intVariable;
private double doubleVariable;
private String stringVariable;
public GetSet(Reader in, int sz) {
super(in, sz);
}
public GetSet(Reader in) {
super(in);
}
public int getIntVariable() {
return intVariable;
}
public void setIntVariable(int newIntVariable) {
intVariable = newIntVariable;
}
public double getDoubleVariable() {
return doubleVariable;
}
public void setDoubleVariable(double newDoubleVariable) {
doubleVariable = newDoubleVariable;
}
public String getStringVariable() {
return stringVariable;
}
public void setStringVariable(String newStringVariable) {
stringVariable = newStringVariable;
}
public void message() {
System.out.println("Int variable = " + intVariable +
"\nDouble variable = " + doubleVariable +
"\nString variable = " + "\"" + stringVariable + "\"");
}
}
getset.setIntVariable = getset.readLine();
getset.setIntVariable(Integer.parseInt(getset.readLine()));
public void setIntVariable(String s) {
intVariable = Integer.parseInt(s);
}