C#里面的换行符号是 \r\n
(Java的换行符号就不是 \r\n 了?应该这是windows的换行符号,mac并不如此,)
以下内容并没在mac 和 linux测过(一般linux也不需要字符串)
网上小哥的错误代码
因为换行符是占两个位的
//永远判断错的代码
var a = textBox1.Text;
var c = Environment.NewLine;
string b = "";
foreach(var ch in a)
{
if (ch.ToString() ==c)
b += c;
else
b = ch + b;
b += "\n";
}
textBox2.Text = b;
Clipboard.SetText(b);
写了一小段代码测试一个个字符提取
确实是第一个是 \r,第二个是 \n (不是"\\r")
IEnumerator StartTypeText()
{
int count = sourceText.Length;
int curr = 1;
var newLine = Environment.NewLine;
while (curr<=count)
{
var code = sourceText.Substring(curr - 1, 1);
if (code == newLine)//同理,获取不到newLine的
{
Debug.LogError("newLine newLine newLine newLine");
}
if (code == "\r")
{
var next = sourceText.Substring(curr, 1);
//Debug.LogError("is \\\\\\\r next="+sourceText.Substring(curr, 1) );
if (sourceText.Length > curr)
{
if (sourceText.Substring(curr, 1) == "\n")
{
Debug.LogError("is refresh.....");
//碰到换行符号
code = "\r\n";
curr++;
Refresh(true,true);
}
}
}
inputField.text = inputField.text + code;
Debug.LogError(sourceText.Substring(curr-1,1));
yield return new WaitForSeconds(typeTextInterval);
curr++;
}
yield return null;
}
小哥的小哥的建议方法,
一行行读取也是可行的,因为绕开了逐个字扫描+自行判断文章来源:https://www.toymoban.com/news/detail-526724.html
String a = textBox1.Text;
String result = String.Empty;
String[] lines = a.Split(new String[] { Environment.NewLine }, StringSplitOptions.None);
foreach(String line in lines.Reverse())
{
// inverse text
foreach(char ch in line.Reverse())
{
result += ch;
}
// insert a new line
result += Environment.NewLine;
}
// remove last NewLine
result = result.Substring(0, result.Length - 1);
示例:输入,如果您有:
test
yopla
结果将是:
alpoy
tset
其他方法:
懂了原理,就能调试了,其他请自行发挥文章来源地址https://www.toymoban.com/news/detail-526724.html
到了这里,关于UnityC#字串如何获取换行符号-最基础的字串处理方法,也最容易常见错误的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!