如何从字符串中删除剩余的不需要的字符
How to remove remaining unwanted char from a string
我的字符串输出是
String mystring="My Name¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥";
我怎么把那个¥¥¥¥¥¥从我的话中去掉。
我正在使用 C#。
我试过了
mystring.Trim( '¥' );
但是没用
我正在聊天,这是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Chat
{
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote;
public Form1()
{
InitializeComponent();
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
textLocalIp.Text= GetLocalIP();
textFriendsIp.Text = GetLocalIP();
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
string c = Caesar(receivedMessage, -1); // String recibido se cambia alreves
//Console.WriteLine(c.Trim( new Char[] { '¥' } )); //Here is where it doesnt work////////////////////
listMessage.Items.Add("Anonymous: "+c.Trim( '¥' ));
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer,
0,
buffer.Length,
SocketFlags.None,
ref epRemote,
new AsyncCallback(MessageCallBack),
buffer);
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string b = Caesar(textMessage.Text, 1); // Sending the Chipper
byte[] msg = new byte[1500];
msg = enc.GetBytes(b);
sck.Send(msg);
listMessage.Items.Add("You: "+textMessage.Text);
textMessage.Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//epLocal = new IPEndPoint(IPAddress.Parse(textLocalIP.Text, Convert.ToInt32(textLocalPort.Text));
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text),Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textFriendsIp.Text),
Convert.ToInt32(textFriendsPort.Text));
sck.Connect(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer,
0,
buffer.Length,
SocketFlags.None,
ref epRemote,
new AsyncCallback(MessageCallBack),
buffer);
button1.Text = "Connected!";
button1.Enabled = false;
button2.Enabled = true;
textMessage.Focus();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
static string Caesar(string value, int shift)
{
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
// Letter.
char letter = buffer[i];
// Add shift to all.
letter = (char)(letter + shift);
// Subtract 26 on overflow.
// Add 26 on underflow.
if (letter > 'z')
{
letter = (char)(letter - 26);
}
else if (letter < 'a')
{
letter = (char)(letter + 26);
}
// Store.
buffer[i] = letter;
}
return new string(buffer);
}
}
}
错误是我标记的地方,我的输出结果是我的名字¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥所以我只想把它分开
如果您希望变量具有新值,您需要确保将结果赋回给它:
mystring = mystring.Trim( '¥' );
你可以试试
mystring = mystring.Remove(mystring.IndexOf('¥'));
我相信这会删除该字符的第一个实例之后的所有内容。在此处查找文档。 https://msdn.microsoft.com/en-us/library/d8d7z2kk%28v=vs.110%29.aspx
我认为它可以只用一个参数。
这是另一种删除尾随字符的方法。
根据Microsoft:
The TrimEnd method removes from the current string all trailing
characters that are in the trimChars parameter. The trim operation
stops when the first character that is not in trimChars is encountered
at the end of the string.
mystring = mystring.TrimEnd( '¥' );
我的字符串输出是
String mystring="My Name¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥";
我怎么把那个¥¥¥¥¥¥从我的话中去掉。 我正在使用 C#。
我试过了
mystring.Trim( '¥' );
但是没用
我正在聊天,这是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
namespace Chat
{
public partial class Form1 : Form
{
Socket sck;
EndPoint epLocal, epRemote;
public Form1()
{
InitializeComponent();
sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
textLocalIp.Text= GetLocalIP();
textFriendsIp.Text = GetLocalIP();
}
private string GetLocalIP()
{
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
return "127.0.0.1";
}
private void MessageCallBack(IAsyncResult aResult)
{
try
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] receivedData = new byte[1464];
receivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string receivedMessage = eEncoding.GetString(receivedData);
string c = Caesar(receivedMessage, -1); // String recibido se cambia alreves
//Console.WriteLine(c.Trim( new Char[] { '¥' } )); //Here is where it doesnt work////////////////////
listMessage.Items.Add("Anonymous: "+c.Trim( '¥' ));
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer,
0,
buffer.Length,
SocketFlags.None,
ref epRemote,
new AsyncCallback(MessageCallBack),
buffer);
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
string b = Caesar(textMessage.Text, 1); // Sending the Chipper
byte[] msg = new byte[1500];
msg = enc.GetBytes(b);
sck.Send(msg);
listMessage.Items.Add("You: "+textMessage.Text);
textMessage.Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//epLocal = new IPEndPoint(IPAddress.Parse(textLocalIP.Text, Convert.ToInt32(textLocalPort.Text));
epLocal = new IPEndPoint(IPAddress.Parse(textLocalIp.Text),Convert.ToInt32(textLocalPort.Text));
sck.Bind(epLocal);
epRemote = new IPEndPoint(IPAddress.Parse(textFriendsIp.Text),
Convert.ToInt32(textFriendsPort.Text));
sck.Connect(epRemote);
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer,
0,
buffer.Length,
SocketFlags.None,
ref epRemote,
new AsyncCallback(MessageCallBack),
buffer);
button1.Text = "Connected!";
button1.Enabled = false;
button2.Enabled = true;
textMessage.Focus();
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
static string Caesar(string value, int shift)
{
char[] buffer = value.ToCharArray();
for (int i = 0; i < buffer.Length; i++)
{
// Letter.
char letter = buffer[i];
// Add shift to all.
letter = (char)(letter + shift);
// Subtract 26 on overflow.
// Add 26 on underflow.
if (letter > 'z')
{
letter = (char)(letter - 26);
}
else if (letter < 'a')
{
letter = (char)(letter + 26);
}
// Store.
buffer[i] = letter;
}
return new string(buffer);
}
}
}
错误是我标记的地方,我的输出结果是我的名字¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥ ¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥¥所以我只想把它分开
如果您希望变量具有新值,您需要确保将结果赋回给它:
mystring = mystring.Trim( '¥' );
你可以试试
mystring = mystring.Remove(mystring.IndexOf('¥'));
我相信这会删除该字符的第一个实例之后的所有内容。在此处查找文档。 https://msdn.microsoft.com/en-us/library/d8d7z2kk%28v=vs.110%29.aspx
我认为它可以只用一个参数。
这是另一种删除尾随字符的方法。
根据Microsoft:
The TrimEnd method removes from the current string all trailing characters that are in the trimChars parameter. The trim operation stops when the first character that is not in trimChars is encountered at the end of the string.
mystring = mystring.TrimEnd( '¥' );