![](/img/trans.png)
[英]How can I remove non-breaking spaces from a JSoup 'Document'?
[英]Why when I try to remove non-breaking space from the string, I do not get the expected result?
我正在編寫代碼,首先使用移調,然后使用替換來創建密文。 到目前為止,我所做的一切都很好(沒有替代),除了從換位中獲得的文本包含不間斷的空格。
在線搜索后,解決方案之一是使用text.replace("\ ", "")
,我在我的代碼中使用了它,但是它不能幫助我刪除那些不間斷的空格。 您的幫助和建議將不勝感激。 我將Java與Netbeans IDE 7.4一起使用。
public class Cipher {
static java.util.HashMap<String, String> HMOperators = new java.util.HashMap<String, String>();
public static char[] Alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
/**
* @param args the command line arguments
*/
static int locateindex = 0;
static int[] WhiteSpaceSeparator = new int[100];
public static String TCipher(String text)
{
char[] stringtoCharArray = text.toCharArray();
char ch;
int loc = 0;
for (int i = 0; i < text.length(); i++)
{
ch = stringtoCharArray[i];
if (Character.isSpaceChar(ch))
{
WhiteSpaceSeparator[loc] = i;
}
loc++;
}
text = text.trim();
String PlainTextNoSpace = text.replaceAll(" ", "");
char[] charArray = PlainTextNoSpace.toCharArray();
int NewIndexArray[] = {2, 7, 5, 3, 0, 4, 6, 1};
int Rows = 6;
int Cols = 8;
char TArray[][] = new char[Rows][Cols];
int i = 0;
for (int row = 0; row <= 5; row++)
{
if (i == charArray.length)
{
break;
}
TArray[row][NewIndexArray[0]] = charArray[i]; //@ column number index == 2
if ((i + 1) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[1]] = charArray[i + 1]; //@ column number index == 7
if ((i + 2) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[2]] = charArray[i + 2]; //5
if ((i + 3) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[3]] = charArray[i + 3]; //3
if ((i + 4) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[4]] = charArray[i + 4]; //0
if ((i + 5) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[5]] = charArray[i + 5]; //4
if ((i + 6) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[6]] = charArray[i + 6]; //6
if ((i + 7) == charArray.length)
{
break;
}
TArray[row][NewIndexArray[7]] = charArray[i + 7]; //1
i = i + 8;
}
//printing the new 2d array with columns organized in increasing order
//the new plain text has to be read from the firt column (column 0) to the last one (column 7)
for (int rows = 0; rows < 6; rows++)
{
System.out.println();
for (int col = 0; col < 8; col++)
{
System.out.print(TArray[rows][col]);
}
}
//Reading the new plain test per column
char[] NewTChar = new char[Rows * Cols];
int k = 0;
for (int col = 0; col < 8; col++)
{
System.out.println();
for (int rows = 0; rows < 6; rows++)
{
ch = TArray[rows][col];
NewTChar[k] = ch; //storing all the columns into a 1d array to obtain a new text (new cipher)
k++;
}
}
int n = 0;
String SeconPlainText = new String(NewTChar);
SeconPlainText = SeconPlainText.trim();
SeconPlainText = SeconPlainText.replace("\u00A0", ""); //It does not work, I am not sure why
System.out.println(SeconPlainText);
System.out.println(SeconPlainText);
StringTokenizer st = new StringTokenizer(SeconPlainText);//it seems like the string is only one token
while (st.hasMoreTokens())
{
System.out.println(st.nextToken()); //it prints the whole string, instead of each token
}
int a = st.countTokens();
return SeconPlainText;
}
public static void main(String[] args)
{
String PlainText = "Machines take me by surprise with great frequency";
String Ttext = TCipher(PlainText);
// SCipher(Ttext);
}
}
實際上,我編輯了一個for循環,以便在遇到不間斷空格時必須強制存儲實際的空白字符。 因此,當將char數組轉換為字符串時,稍后可以使用text.replace(“”,“”);從字符串中刪除空格。 現在,它可以按預期工作。 但是,在IDE(Netbeans)中,“ isJavaLetter”被刪除了。 我不確定它是否已過時,但這可以幫助我完成我想做的事情。 以下是我更新的for循環:
for (int col = 0; col < 8; col++)
{
System.out.println();
for (int rows = 0; rows < 6; rows++)
{
ch = TArray[rows][col];
if(Character.isJavaLetter(ch))//
NewTChar[k] = ch; //storing all the columns into a 1d array to obtain a new text (new cipher)
else
NewTChar[k] =' '; //storing an actual white space character
k++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.