我从TextView
获取文本 String textString = textView.getText().toString();
问题是如果文本视图中的文本有这样的换行符,例如
My
Sentence has
line breaks
当我将文本记录到控制台时
Log.v("TAG",textString);
或者如果我将文本设置为TextView
textView.setText(textString);
结果始终显示实际换行符
My
Sentence has
line breaks
而不是我想要的转义字符
My\nSentence has\nline breaks
我知道这是一个非常糟糕的解释,但是如何从文本视图中获取带有代表换行符的实际字符的文本。
答案 0 :(得分:1)
试试这个
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string TelephoneNames[100];
int TelephoneNumbers[100];
void ModifyRecords(); //Function to Modify Records
void SearchRecords(); //Function to Search Records
void DeleteRecords(); //Function to Delete Records
int main()
{
fstream inputFile;
fstream outputFile;
char choice;
inputFile.open("Telephone Names.txt"); //To store
for (int count=0;count<100;count++) //file names
{ //into a
inputFile >> TelephoneNames[count]; //string
}
inputFile.close();
inputFile.open("Telephone Numbers.txt");//To store
for (int count=0;count<100;count++) //file #'s
{ //into a
inputFile >> TelephoneNumbers[count];//string
}
inputFile.close();
//Display options available
cout << " Hello, do you want to:\n";
cout << " ======================\n";
cout << "-Modify Records|Enter M\n";
cout << "-Search Records|Enter S\n";
cout << "-Delete Records|Enter D\n";
//Store choice
cin >> choice;
//Send to different function
if (choice=='M'||choice=='m')
{
ModifyRecords();
}
if (choice=='S'||choice=='s')
{
SearchRecords();
}
return 0;
}
void ModifyRecords()
{
string name;
string newname;
int newnumber;
int count=0;
cout << "Enter the name of the person: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Enter the new name of the person: ";
cin >> newname;
cout << "Enter the new number of the person: ";
cin >> newnumber;
TelephoneNames[count]={newname};
TelephoneNumbers[count]={newnumber};
count=0;
while (count<6)
{
cout << TelephoneNames[count] << endl;
cout << TelephoneNumbers[count] << endl;
cout << endl;
count++;
}
}
void SearchRecords()
{
string name;
int count=0;
cout << "Enter the name of the person you would like to find: ";
cin >> name;
for (count=0;TelephoneNames[count]!=name;count++)//To determine where in the strings the new numbers need to be
{
}
cout << "Name: " << TelephoneNames[count] << endl;
cout << "Number: " << TelephoneNumbers[count] << endl;
}
答案 1 :(得分:0)
问题是将我们在Java中表示的String格式化为
String test = "My\nSentence has\nline breaks";
如下:
My\nSentence has\nline breaks
即。用反斜杠替换每个换行符后跟一个'n'
字符。
一种解决方案是使用String.replaceAll
。例如,如果您知道换行符是严格的换行符,那么。
String unbroken = broken.replaceAll("\n", "\\\\n");
如果换行符可能是特定于平台的换行符,那么:
String unbroken = broken.replaceAll("\r\n|\n|\r", "\\\\n");
请注意,Java Pattern
会将正则表达式中的裸CR或NL视为文字字符。但是在替换字符串中,我们需要一个字面反斜杠字符...因此它需要双重转义。另一种写作方式是:
String unbroken = broken.replaceAll("\n", Matcher.quoteReplacement("\\n"));
有关详细信息,请参阅javadoc ...以及在替换字符串中处理特殊反斜杠需要的明确解释。