Trying to remove a Dot from a String i´m experiencing a problem. With different Chars such as "," or "-" its working properly but the Dot won´t disappear.
Got a TextBox with the Name nr_tb. I Input Hello.World and my MessageBox outputs the same. Changing the Dot in the Replace function with a "," works.
Not working:
string line;
private void nr_tb_TextChanged(object sender, TextChangedEventArgs e)
{
line = nr_tb.Text.Replace(".","");
MessageBox.Show(line);
}
Working:
string line;
private void nr_tb_TextChanged(object sender, TextChangedEventArgs e)
{
line = nr_tb.Text.Replace(",","");
MessageBox.Show(line);
}
string number= "2.36";
string newNumber = number.Replace(".", "");
It might be another character that seems like a DOT. Char code of DOT is 46 so if you get this char code in your string you can easily Replace(".", ""); it.
This method show each char code of a string:
private static string DotRemover(string temp)
{
string _Result = string.Empty;
foreach (byte a in Encoding.UTF8.GetBytes(temp.ToCharArray()))
{
_Result += a.ToString() + Environment.NewLine;
}
return _Result;
}
OR
you can replace everything that look likes a DOT :
public static string CleanNumb(string numb)
{
foreach (char c in ".,'´")
numb = numb.Replace(c, ' ');
return numb.Replace(" ", "");
}
Related
Is it possible to remove every occurance of a character + one after it from entire string?
Here's a code that achieves what I described, but is rather big:
private static string FilterText(string text){
string filteredText = text;
while (true)
{
int comaIndex = filteredText.IndexOf('.');
if (comaIndex == -1)
{
break;
}
else if (comaIndex > 1)
{
filteredText = filteredText.Substring(0, comaIndex) + filteredText.Substring(comaIndex + 2);
}
else if (comaIndex == 1)
{
filteredText = filteredText[0] + filteredText.Substring(comaIndex + 2);
}
else
{
filteredText = filteredText.Substring(comaIndex + 2);
}
}
return filteredText;
}
This code would turn for example input .Otest.1 .2.,string.w.. to test string
Is it possible to achieve the same result using regex?
You want to use
var output = Regex.Replace(text, #"\..", RegexOptions.Singleline);
See the .NET regex demo. Details:
\. - matches a dot
. - matches any char including a line feed char due to the RegexOptions.Singleline option used.
Try this pattern: (?<!\.)\w+
code:
using System;
using System.Text.RegularExpressions;
public class Test{
public static void Main(){
string str = ".Otest.1 .2.,string.w..";
Console.WriteLine(FilterText(str));
}
private static string FilterText(string text){
string pattern = #"(?<!\.)\w+";
string result = "";
foreach(Match m in Regex.Matches(text, pattern)){
result += m + " ";
}
return result;
}
}
output:
test string
I split text from a text file and I have to compare 2 strings, one from a textbox and the other one from a text file from a specific line. That string from text has a space at the end and the comparison is always wrong.
Here is the code. Thank you!
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
t = r.Next(1,30);
StreamReader sr = new StreamReader("NomenA1.txt");
cuv = sr.ReadToEnd().Split('\n');
string original = cuv[t];
Random num = new Random();
// Create new string from the reordered char array.
string rand = new string(original.ToCharArray().
OrderBy(s => (num.Next(2) % 2) == 0).ToArray());
textBox2.Text = rand;
button1.Visible = false;
button2.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
button1.Visible = false;
string a =Convert.ToString(textBox1.Text.ToString());
string b = cuv[t];
if (a == b)
{
MessageBox.Show("Corect");
button1.Visible = true;
button2.Visible = false;
}
else
{
MessageBox.Show("Mai incearca"); button1.Visible = false;
}
}
You can use Regex to remove all last space:
string s = "name ";
string a = Regex.Replace(s, #"\s+$", "");
or Trim() function for all both end space:
string s = " name ";
string a = s.Trim();
or if you want to remove only one space from the end:
string s = "name ";
string a = Regex.Replace(s, #"\s$", "");
How could I remove the last character in a string
var s = "Some string ";
var s2 = s.Substring(0, s.Length - 1);
Alternatively a general solution is usually
var s3 = s.Trim(); // Remove all whitespace at the start or end
var s4 = s.TrimEnd(); // Remove all whitespace at the end
var s5 = s.TrimEnd(' '); // Remove all spaces from the end
Or a very specific solution
// Remove the last character if it is a space
var s6 = s[s.Length - 1] == ' ' ? s.Substring(0, s.Length - 1) : s;
Have you tried .Trim()
String myStringA="abcde ";
String myStringB="abcde";
if (myStringA.Trim()==myStringB)
{
Console.WriteLine("Matches");
}
else
{
Console.WriteLine("Does not match");
}
What about :
string s = "name ";
string a = s.Replace(" ", "");
That seems pretty simple to me, right?
How do I get the last entered word in RichEditControl
here my code
private void richEditControl1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ' ')
{
int wordEndPosition = richEditControl1.Document.CaretPosition.ToInt();
int currentPosition = wordEndPosition;
while (currentPosition > 0 && richEditControl1.Text[currentPosition - 1] != ' ')
{
currentPosition--;
}
string word = richEditControl1.Text.Substring(currentPosition, wordEndPosition - currentPosition);
this.Text = "Last char typed: " + word;
}
}
But when i press Enter create new line, it was wrong.
I guess you want to get a word whether it is surrounded by spaces or new lines, as long as it is the last one? Maybe you should include New Line check in your While loop, so it doesn't check just spaces.
richEditControl1.Text[currentPosition - 1] != "\n"
or something alike. Not sure if "\n" will pass , since I didn't work with such examples for some time. It probably just didn't know what to do whit new line.
Try:
public Form1()
{
InitializeComponent();
richEditControl1.KeyUp +=richEditControl1_Key;
}
private void richEditControl1_Key(object sender, KeyEventArgs e)
{
var currentText = richEditControl1.Text.Replace("\n", "");
currentText = richEditControl1.Text.Replace("\r", " ");
String result = currentText.Trim().Split(' ').LastOrDefault().Trim();
Console.WriteLine(String.Format("{0}| {1}", DateTime.Now.ToLongTimeString(), result));
}
Hello I am making an application to split the song text based on certain char. The application works when I type something such as "sampletext/ sampletext", then the result should be :
sampletext
sampletext
but, instead of above, the result is
sampletextsampletext
Below is the code :
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
string[] a = TextToSongFormatConverter(textBox1.Text, '/');
textBox2.Text += "\n"; textBox2.Text += "\n"; textBox2.Text += "\n";
foreach (var item in a)
{
textBox2.Text += item;
textBox2.Text += "\n";
}
}
public static string[] TextToSongFormatConverter(string text, char separator)
{
string[] result;
result = text.Split(separator);
for (int i = 0; i < result.Length; i++)
{
result[i] = result[i].Trim();
}
}
I am using visual studio 2012 and c# and windows form :
This is the screenshoot :
Any idea why? Thanks.
Use Environment.NewLine instead. It will give you proper new line characters for system your program is running on.
A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.
from Environment.NewLine Property
Try using Environment.NewLine instead of \n.
MSDN
I'm working on an assignment which wants me to have the user input a string with no spaces and to identify each word by having it start with a capital letter, so "IAmName" would convert to "I Am Name". I think I have that part down, my problem is the last step, which is displaying the new string in a label, here is my code so far:
private string ConvertText()
{
string str = inputTextBox.Text;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
}
string newstring = outputLabel.Text;
for (int i = 0; i < str.Length; i++)
{
if (char.IsUpper(str[i]))
newstring += " ";
newstring += str[i].ToString();
}
return newstring;
}
Any help would be greatly appreciated.
Just do it the other way around:
outputLabel.Text = ConvertText(); // Or any other label that should display it
I'm not sure why you have this (string newstring = outputLabel.Text;) inside your code though. You're not using it, you just overwrite it.
You might also want to use a StringBuilder to concatenate in a loop, it's a lot more efficient.
And this code probably should add a return;
if (str.Contains(" "))
{
MessageBox.Show("No spaces allowed");
return; // Return so it stops executing this method
}
I edited you sample a little
I think it correct as below.
public static class MyClass
{
public static string ConvertText(string inputText)
{
var newstring = "";
for (var i = 0; i < inputText.Length; i++)
{
if (char.IsUpper(inputText[i]))
newstring += " ";
newstring += inputText[i].ToString(CultureInfo.InvariantCulture);
}
return newstring;
}
}
string output = MyClass.ConvertText("MyNameIsBlaBla");
Console.WriteLine(output);
" My Name Is Bla Bla"