📄 stringutils.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/**
* Title: XELOPES Data Mining Library
* Description: The XELOPES library is an open platform-independent and data-source-independent library for Embedded Data Mining.
* Copyright: Copyright (c) 2002 Prudential Systems Software GmbH
* Company: ZSoft (www.zsoft.ru), Prudsys (www.prudsys.com)
* @author Valentine Stepanenko (valentine.stepanenko@zsoft.ru)
* @version 1.0
*/
package com.prudsys.pdm.Utils;
public class StringUtils
{
/**
* Quote string.
*
* @param string string to be quoted
* @return quoted string
*/
public static String quote( String string )
{
boolean quote = false;
// backquote the following characters
if((string.indexOf('\n') != -1) || (string.indexOf('\r') != -1) ||
(string.indexOf('\'') != -1) || (string.indexOf('"') != -1) ||
(string.indexOf('\\') != -1) ||
(string.indexOf('\t') != -1) || (string.indexOf('%') != -1))
{
string = backQuoteChars(string);
quote = true;
}
// Enclose the string in 's if the string contains a recently added
// backquote or contains one of the following characters.
if((quote == true) ||
(string.indexOf('{') != -1) || (string.indexOf('}') != -1) ||
(string.indexOf(',') != -1) || (string.equals("?")) ||
(string.indexOf(' ') != -1) || (string.equals("")))
{
string = ("'".concat(string)).concat("'");
}
return string;
}
/**
* Backquote characters ('\\', '\'', '\t', '"', '%')
* in string.
*
* @param string string containing characters to be backquoted
* @return new string with characters backquoted
*/
public static String backQuoteChars( String string )
{
int index;
StringBuffer newStringBuffer;
// replace each of the following characters with the backquoted version
char charsFind[] = {'\\', '\'', '\t', '"', '%'};
String charsReplace[] = {"\\\\", "\\'", "\\t", "\\\"", "\\%"};
for(int i = 0; i < charsFind.length; i++)
{
if (string.indexOf(charsFind[i]) != -1 )
{
newStringBuffer = new StringBuffer();
while((index = string.indexOf(charsFind[i])) != -1)
{
if (index > 0)
{
newStringBuffer.append(string.substring(0, index));
}
newStringBuffer.append(charsReplace[i]);
if((index + 1) < string.length())
{
string = string.substring(index + 1);
}
else
{
string = "";
}
}
newStringBuffer.append(string);
string = newStringBuffer.toString();
}
}
return convertNewLines(string);
}
/**
* Convert new lines.
*
* @param string string to be processed
* @return resulting string
*/
public static String convertNewLines( String string )
{
int index;
// Replace with \n
StringBuffer newStringBuffer = new StringBuffer();
while((index = string.indexOf('\n')) != -1)
{
if(index > 0)
{
newStringBuffer.append(string.substring(0, index));
}
newStringBuffer.append('\\');
newStringBuffer.append('n');
if((index + 1) < string.length())
{
string = string.substring(index + 1);
}
else
{
string = "";
}
}
newStringBuffer.append(string);
string = newStringBuffer.toString();
// Replace with \r
newStringBuffer = new StringBuffer();
while((index = string.indexOf('\r')) != -1)
{
if(index > 0)
{
newStringBuffer.append(string.substring(0, index));
}
newStringBuffer.append('\\');
newStringBuffer.append('r');
if((index + 1) < string.length())
{
string = string.substring(index + 1);
}
else
{
string = "";
}
}
newStringBuffer.append(string);
return newStringBuffer.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -