I would skip the text in the field, if there is no text, but if this is to add text to the beginning and the end. I'm trying like this but it add text every line.
private void zmien(string a, string b)
{
{
if (richTextBox1.Text.Length > 0)
{
string[] lines = richTextBox1.Lines;
for (int i = 0; i < lines.Length; i++)
{
if (richTextBox1.Text.Length == 0)
{
for (int j = 0; i < lines.Length; i++)
lines[i] = Environment.NewLine;
}
else
lines[i] = a + lines[i] + b;
}
richTextBox1.Lines = lines;
//richTextBox1.SelectedText = "test" + lines;
}
}
}
You can do that much simply as you think, try follow this code
private void zmien(string a, string b)
{
if (richTextBox1.Text.Length > 0)
{
richTextBox1.Text = a+" "+richTextBox1.Text+" "+b;
}
}
Related
This is program consumes 36.50 MB of memory but I want it to be less than 32 MB
public static void CreateText(string text)
{
if (Convert.ToInt32(text.Length) <= 80)
{
int n;
string str = "";
string count = "";
char[] mas = text.ToCharArray();
for (int i = 0; i < Convert.ToInt32(mas.Length); i++)
{
if (int.TryParse(mas[i].ToString(), out n))
{
count += mas[i].ToString();
}
else
{
if (String.IsNullOrEmpty(count))
{
str += mas[i].ToString();
}
else
{
for (int j = 0; j < Convert.ToInt32(count); j++)
{
str += mas[i].ToString();
}
count = "";
}
}
}
Console.WriteLine(str);
} else {
Console.WriteLine("Error");
}
}
To reduce memory footprint you need to get read of temporary string objects generated by applying operation += against a string. String is immutable object in C#, so += creates new string. StringBuilder is mutable, so use it instead of string. You also need to have count as an int, not string or StringBuilder.
public static void CreateText(string mas)
{
if (mas.Length <= 80)
{
StringBuilder str;
int count;
for (int i = 0; i < mas.Length; i++)
{
if (mas[i] >= '0' && mas[i] <= '9')
count = count * 10 + mas[i] - '0';
else
{
if (count == 0)
str.Append(mas[i]);
else
{
for (int j = 0; j < count; j++)
str.Append(mas[i]);
count = 0;
}
}
}
Console.WriteLine(str.ToString());
}
else
Console.WriteLine("Error");
}
This probably isn't possible. Most of the RAM in a 36MB program is just core framework libraries. 36MB is nothing.
But I do see some potential improvements, the biggest of which are maintaining count as an integer rather than a string and using a string constructor and StringBuilder instead of appending to a string all the time:
public static void CreateText(string text)
{
if (text != null && text.Length <= 80)
{
int n; int count = 0;
StringBuilder result = new StringBuilder();
char[] mas = text.ToCharArray();
foreach(char c in text)
{
if (int.TryParse(c.ToString(), out n))
{
count = (count * 10) + n;
}
else
{
if (count == 0)
{
result.Append(c);
}
else
{
result.Append(new string(c, count));
count = 0;
}
}
}
Console.WriteLine(result.ToString());
} else {
Console.WriteLine("Error");
}
}
There is a potential bug there if you want to be able to explicitly set 0 repetition in the input string. If that's the case, we'll need something that is slightly less efficient, but should still have a big improvement over the original:
public static void CreateText(string text)
{
if (text != null && text.Length <= 80)
{
int n; int count = -1;
StringBuilder result = new StringBuilder();
char[] mas = text.ToCharArray();
foreach(char c in text)
{
if (int.TryParse(c.ToString(), out n))
{
if (count == -1) count = 0;
count = (count * 10) + n;
}
else
{
if (count == -1)
{
result.Append(c);
}
else
{
result.Append(new string(c, count));
count = -1;
}
}
}
Console.WriteLine(result.ToString());
} else {
Console.WriteLine("Error");
}
}
Try this:
public static void CreateText(string text)
{
if (text.Length <= 80)
{
var str = new StringBuilder();
var count = new StringBuilder();
for (int i = 0; i < text.Length; i++)
{
int n;
if (int.TryParse(text[i].ToString(), out n))
{
count.Append(text[i]);
}
else
{
if (String.IsNullOrEmpty(count.ToString()))
{
str.Append(text[i]);
}
else
{
for (int j = 0; j < Convert.ToInt32(count.ToString()); j++)
{
str.Append(text[i].ToString());
}
count.Clear();
}
}
}
Console.WriteLine(str);
}
else
{
Console.WriteLine("Error");
}
}
What I did was:
Remove the string contetantions (since they are immutable) and used a StringBuilder instead.
No need to convert to a char[] since your can treat a String as one (it implements the indexer)
Moved the n closed to used to it's only allocated if needed
I've found several examples that do this but with richTextBox instead. Is it even possible to replace words in a multi-line TextBox?
you can do this for searchin' the next word automatically in textbox
int t = 0;
private void FindNext(object sender, RoutedEventArgs e)
{
for (int i = t; i < NoteText.Text.Length-SearchBar.Text.Length; i++)
{
string x = "";
for (int j = 0; j < SearchBar.Text.Length; j++)
{
if(SearchBar.Text[j] == NoteText.Text[i+j])
{
x += NoteText.Text[i + j] + "";
}
else
{
x = "";
}
}
if(x == SearchBar.Text)
{
t = i+1;
NoteText.Focus();
NoteText.SelectAll();
NoteText.Select(i, SearchBar.Text.Length);
break;
}
if(i==NoteText.Text.Length - SearchBar.Text.Length - 1)
{
MessageBox.Show("The search was completed");
t = 0;
}
s = t;
}
}
I need a method that returns every other character in a string starting with the first character. For example, a method call with ("Java-language") returns "Jv-agae."
private static void NewMethod(string word)
{
// here comes the code
}
var str = "Java-language";
var xx = new string(str.Where((ch, index) => index % 2 == 0).ToArray());
Console.WriteLine(xx);
Or this one:
var xx = string.Join<char>("", str.Where((ch, index) => (index & 1) == 0));
probably little different then everybody else: :-)
protected static IEnumerable<char> EverySecondChar(string word)
{
for(int i = 0; i < word.Length; i += 2)
yield return word[i];
}
string result = new string(EverySecondChar("Java-language").ToArray());
Here is my suggestion for you:
private string TakeEverySecondChar(string input)
{
var result = string.Empty;
for (int i = 0; i < input.Length; i+=2)
{
result += input.Substring(i, 1);
}
return result;
}
Console.Clear();
string Lang = "Java-language";
string[] LangArr = new string[Lang.Length];
char LangChar;
for (int i = 0; i < Lang.Length; i++)
{
LangChar = Lang[i];
LangArr[i] = LangChar.ToString();
}
for (int i = 0; i < LangArr.Length; i++)
{
Console.Write(LangArr[i]);
i++;
}
Console.ReadLine();
public String strip2ndchar(string text)
{
string final="";
int i = 0;
foreach (char a in text.ToCharArray())
{
if (i % 2 == 0)
final += a;
i++;
}
return final;
}
I have some string in format like that
XXXX-XXXX-X_X_
All "_" should be replaced with Letters and numberst to prodce sth like that:
XXXX-XXXX-XAXA
XXXX-XXXX-XAXB
XXXX-XXXX-XAXC
XXXX-XXXX-XAXD
XXXX-XXXX-XAXE
XXXX-XXXX-XAXF
XXXX-XXXX-XAXG
(...)
XXXX-XXXX-XZX8
XXXX-XXXX-XZX9
XXXX-XXXX-X0XA
(...)
XXXX-XXXX-X2XA
XXXX-XXXX-X2XB
I know hoe to make it with one "_".
string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
foreach (char letter in alphaLetters.ToCharArray())
{
Numbers.Add(number.Replace('_', letter)));
}
I want this code to be working with unknown number of "_".
Can you help?
IMHO it must be recursive. (Note: that does not mean it must use recursive method call, although I used recursive call in the following code, it can be easily converted to internal recursion stack. )
public static void RunSnippet()
{
var r = new List<string>();
Replace("asd_asd_asd_".ToCharArray(), 0, r);
foreach(var s in r) { Console.WriteLine(s); }
}
public static char[] possibilities = new char[] { 'A', 'B', 'C' };
public static void Replace(char[] chars, int startIndex, IList<string> result)
{
for (int i = startIndex; i < chars.Length; i++)
{
if (chars[i] != '_')
{
continue;
}
// we found first '_'
for (int j = 0; j < possibilities.Length; j++)
{
chars[i] = possibilities[j];
Replace(chars, i + 1, result);
}
chars[i] = '_'; // take back what we replaced
return; //we're done here
}
// we didn't find any '_', so all were replaced and we have result:
result.Add(new string(chars));
}
Try this one:
var alphaIndexes = new List<int>();
string alphaLetters = "ABCDEFGHIJKLMNOPQRSTUWXYZ0123456789ABCDEF";
for(int n = 0; n<Numbers.Count; n++) {
char[] numberLetters = Numbers[n].ToCharArray();
int position = 0;
for(int i = numberLetters.Length - 1; i>=0; i--) {
if(numberLetters[i] == '_') {
int alphaIndex = 0;
if(alphaIndexes.Count <= position)
alphaIndexes.Add(0);
else {
alphaIndex = alphaIndexes[position];
}
numberLetters[i] = alphaLetters[alphaIndex];
position++;
}
}
if(alphaIndexes.Count > 0) {
alphaIndexes[0]++;
for(int j = 0; j < alphaIndexes.Count; j++) {
if(alphaIndexes[j] >= alphaLetters.Length) {
alphaIndexes[j] = 0;
if (j < alphaIndexes.Count)
alphaIndexes[j+1]++;
}
}
}
Numbers[n] = new String(numberLetters);
Numbers[n].Dump();
}
I would like to split a string deliminated by a character such as ‘&’, but in the case where some values contain the deliminator I would like to escape with double quotes. What is an elegant approach to splitting while ignoring the deliminating characters that have been escaped while also accounting for escape character escapes?
For example split this string properly
var1=asdfasdf&var2=contain””quote&var3=”contain&delim”&var4=”contain””both&”
Into:
var1=asdfasdf
var2=contain"quote
var3=contain&delim
var4=contain"both&
Incidentally, I am thinking Regex...
My solution, with test:
void TestCharlesParse()
{
string s = #"var1=asdfasdf&var2=contain""""quote&var3=""contain&delim""&var4=""contain""""both&""";
string[] os = CharlesParse(s);
for (int i = 0; i < os.Length; i++)
System.Windows.Forms.MessageBox.Show(os[i]);
}
string[] CharlesParse(string inputString)
{
string[] escapedQuotes = { "\"\"" };
string[] sep1 = inputString.Split(escapedQuotes, StringSplitOptions.None);
bool quoted = false;
ArrayList sep2 = new ArrayList();
for (int i = 0; i < sep1.Length; i++)
{
string[] sep3 = ((string)sep1[i]).Split('"');
for (int j = 0; j < sep3.Length; j++)
{
if (!quoted)
{
string[] sep4 = sep3[j].Split('&');
for (int k = 0; k < sep4.Length; k++)
{
if (k == 0)
{
if (sep2.Count == 0)
{
sep2.Add(sep4[k]);
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep4[k];
}
}
else
{
sep2.Add(sep4[k]);
}
}
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep3[j];
}
if (j < (sep3.Length-1))
quoted = !quoted;
}
if (i < (sep1.Length - 1))
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + "\"";
}
string[] ret = new string[sep2.Count];
for (int l = 0; l < sep2.Count; l++)
ret[l] = (string)sep2[l];
return ret;
}