com.sta.mutils.StringUtils Maven / Gradle / Ivy
package com.sta.mutils;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* Name: StringUtils
* Description: Diverse String-Hilfsmethoden.
*
* Comment: ...
*
* Copyright: Copyright (c) 2012, 2017-2019, 2023
* Company: >StA-Soft<
* @author StA
* @version 1.0
*/
public final class StringUtils
{
/**
* Datei zeilenweise auslesen und zu einem String zusammensetzen.
* @param fn Dateiname
* @return Ergebnis-String
* @throws IOException im Fehlerfall
*/
public static String readFile2String(String fn) throws IOException
{
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fn)));
try
{
String line;
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append('\n');
}
}
finally
{
br.close();
}
return sb.toString();
}
/**
* String zeilenweise in eine Datei schreiben.
* @param fn Dateiname
* @param s der String
* @throws IOException in Fehlerfall
*/
public static void writeString2File(String fn, String s) throws IOException
{
StringTokenizer st = new StringTokenizer(s, "\n");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fn)));
try
{
while (st.hasMoreTokens())
{
String line = st.nextToken();
bw.write(line);
bw.newLine();
}
}
finally
{
bw.close();
}
}
//===========================================================================
/**
* Gro?-/Kleinschreibung genau vertauschen.
* @param pStrg Text
* @return Text mit vertauschter Gro?-/Kleinschreibung
*/
public static String toggleCase(String pStrg)
{
if (pStrg == null)
{
return null;
}
int len = pStrg.length();
StringBuilder toggled = new StringBuilder(len);
for (int i = 0; i < len; i++)
{
toggled.append(CharUtils.toggleCase(pStrg.charAt(i)));
}
return toggled.toString();
}
//===========================================================================
/**
* Compares two Strings, and returns the portion where they differ.
* (More precisely, return the remainder of the second String, starting from where it's different from the first.)
* @param str1 the first String, may be null
* @param str2 the second String, may be null
* @return the portion of str2 where it differs from str1; returns the empty String if they are equal
*/
public static String difference(String str1, String str2)
{
if (str1 == null)
{
return str2;
}
if (str2 == null)
{
return str1;
}
int at = indexOfDifference(str1, str2);
if (at == -1)
{
return null;
}
return str2.substring(at);
}
/**
* Compares two Strings, and returns the index at which the Strings begin to differ.
* @param cs1 the first String, may be null
* @param cs2 the second String, may be null
* @return the index where str2 and str1 begin to differ; -1 if they are equal
*/
public static int indexOfDifference(CharSequence cs1, CharSequence cs2)
{
if (cs1 == cs2)
{
return -1;
}
if ((cs1 == null) || (cs2 == null))
{
return 0;
}
int i;
for (i = 0; (i < cs1.length()) && (i < cs2.length()); ++i)
{
if (cs1.charAt(i) != cs2.charAt(i))
{
break;
}
}
if ((i < cs2.length()) || (i < cs1.length()))
{
return i;
}
return -1;
}
//===========================================================================
/**
* Dummy-Constructor.
*/
private StringUtils()
{
}
}