String magically cuts by itself - c#

I don't understand what is going on. Recently I took this code and edited it a bit. I've found a bug so I've tried to debug it and met weird behaviour. Somewhy 2 strings created from 2 different char arrays consisting of same letters aren't equal and also debug string is cutted.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AlphanumericComparer : IComparer<string>
{
public int Compare(string s1, string s2)
{
if (s1 == null || s2 == null)
return 0;
int len1 = s1.Length;
int len2 = s2.Length;
int i1 = 0;
int i2 = 0;
while (i1 < len1 && i2 < len2)
{
char c1 = s1[i1];
char c2 = s2[i2];
char[] chunk1 = new char[len1];
int j1 = 0;
char[] chunk2 = new char[len2];
int j2 = 0;
do
{
Debug.Log("1: " + i1 + " _ " + j1 + " _ " + c1); // Seems to be OK.
chunk1[j1++] = c1;
i1++;
if (i1 < len1)
c1 = s1[i1];
else
break;
} while (char.IsDigit(c1) == char.IsDigit(chunk1[0]));
do
{
Debug.Log("2: " + i2 + " _ " + j2 + " _ " + c2); // Seems to be OK.
chunk2[j2++] = c2;
i2++;
if (i2 < len2)
c2 = s2[i2];
else
break;
} while (char.IsDigit(c2) == char.IsDigit(chunk2[0]));
string str1 = new string(chunk1);
string str2 = new string(chunk2);
Debug.Log(str1.CompareTo(str2) + " " + str1 + " " + str2); // "1"?! And also why is string cutted?!
int result;
if (char.IsDigit(chunk1[0]) && char.IsDigit(chunk2[0]))
{
result = int.Parse(str1).CompareTo(int.Parse(str2));
//Debug.Log(s1 + " _ " + s2 + " _ " + int.Parse(str1) + " _ " + int.Parse(str2) + " _ " + result);//tmp
Debug.Log(string.Format("{0}, {1}, {2}, {3}, {4}", s1, s2, int.Parse(str1), int.Parse(str2), result));//tmp
}
else
{
result = str1.CompareTo(str2);
//Debug.Log(s1 + " _ " + s2 + " _ " + str1 + " _ " + str2 + " _ " + result);//tmp
Debug.Log(string.Format("{0}, {1}, {2}, {3}, {4}", s1, s2, str1, str2, result));//tmp
}
if (result != 0)
return result;
}
return len1 - len2;
}
}
I've tried to reload all IDEs but it has brought nothing. My strings which I compare:
string[] test = new string[] { "qwe45", "qwe13a" };
For the next string sorting works as expected and behaviour differs but cutted debug string bug is still there:
string[] test = new string[] { "qwe45", "qwe13" };
What am I doing wrong or if I am not, how to workaround this?
UPDATE:
Also if I split like this:
Debug.Log(str1);
Debug.Log(str2);
it shows right stuff, but CompareTo still returns some garbage.

I struggle to understand what the code is trying to accomplish, but it seems to me that your declaration char[] chunk1 = new char[len1]; creates a variable that is one character too short.
len1 is the length of the first input string, in your example ("qwe45") 5. So chunk1 will have five chars of memory space, which allows four chars of text plus the terminating 0.
Then you copy it into str1 with string str1 = new string(chunk1);, which means that str1 will now be one char shorter. Isn't that the char you are missing?

Related

Loop to get all probabilities of combining words

I have text that contains spaces.
I want all the possibilities. For example, merge a space and leave another.
For example, this is the first possibility:
the first word, then a space, the second word, then a space, then the third word.
The second possibility:
the first word, then without a space, then the second word, then a space, then the third word.
The third possibility:
the first word, then a space, then the second word, then without a space, then the third word ... etc..
I want to do this, but in a loop, especially if the number of words is more than five, six, or more.
static void Main(string[] args)
{
string test = "aaa bbb ccc";
var ch = test.Split(' ');
var t1 = ch[0] + " " + ch[1] + " " + ch[2];
var t2 = ch[0] + "" + ch[1] + " " + ch[2];
var t3 = ch[0] + " " + ch[1] + "" + ch[2];
var t4 = ch[0] + "" + ch[1] + "" + ch[2];
Console.WriteLine(t1);
Console.WriteLine(t2);
Console.WriteLine(t3);
Console.WriteLine(t4);
string test2 = "aaa bbb ccc ddd";
var ch2 = test2.Split(' ');
var z1 = ch2[0] + " " + ch2[1] + " " + ch2[2] + " " + ch2[3];
var z2 = ch2[0] + "" + ch2[1] + " " + ch2[2] + " " + ch2[3];
var z3 = ch2[0] + " " + ch2[1] + "" + ch2[2] + " " + ch2[3];
var z4 = ch2[0] + " " + ch2[1] + " " + ch2[2] + "" + ch2[3];
var z5 = ch2[0] + "" + ch2[1] + "" + ch2[2] + " " + ch2[3];
var z6 = ch2[0] + " " + ch2[1] + "" + ch2[2] + "" + ch2[3];
var z7 = ch2[0] + "" + ch2[1] + "" + ch2[2] + "" + ch2[3];
Console.WriteLine(z1);
Console.WriteLine(z2);
Console.WriteLine(z3);
Console.WriteLine(z4);
Console.WriteLine(z5);
Console.WriteLine(z6);
Console.WriteLine(z7);
Console.ReadLine();
}
So if we have n words we have n - 1 possible spaces. Let 0 be absence of space, when 1 be a presence of space:
[0, 0, 0] => Word1Word2Word3Word4
[0, 0, 1] => Word1Word2Word3 Word4
[0, 1, 0] => Word1Word2 Word3Word4
[0, 1, 1] => Word1Word2 Word3 Word4
...
[1, 1, 1] => Word1 Word2 Word3 Word4
So far so good we should enumerate all 2 ^ (n - 1) binary masks:
using System.Collections.Generic;
using System.Linq;
using System.Text;
...
private static IEnumerable<string> Solution(params string[] words) {
return Enumerable
.Range(0, 1 << (words.Length - 1))
.Select(mask => {
StringBuilder sb = new StringBuilder(words[0]);
for (int i = 0; i < words.Length - 1; ++i) {
if ((mask & (1 << i)) != 0)
sb.Append(' ');
sb.Append(words[i + 1]);
}
return sb.ToString();
});
}
Demo (fiddle):
var result = string.Join(Environment.NewLine, Solution("A", "B", "C", "D"));
Console.WriteLine(result);
Output:
ABCD
A BCD
AB CD
A B CD
ABC D
A BC D
AB C D
A B C D
The code you provided works for a limited number of words, but becomes impractical as the number of words increases. A more efficient way to generate all possibilities of a string containing spaces is to use loops and recursion. Here's an example of a function that takes a string and generates all possible combinations of that string (with and without spaces):
private static List<string> GenerateCombinations(string[] words, int index)
{
if (index == words.Length)
{
return new List<string> { "" };
}
var result = new List<string>();
var subCombinations = GenerateCombinations(words, index + 1);
foreach (var subCombination in subCombinations)
{
result.Add(words[index] + subCombination);
result.Add(words[index] + " " + subCombination);
}
return result;
}
You can call this function with the string you want to generate combinations for:
string test = "aaa bbb ccc ddd";
var words = test.Split(' ');
var combinations = GenerateCombinations(words, 0);
foreach(var combination in combinations)
{
Console.WriteLine(combination);
}
The function uses recursion to generate all possible combinations of strings. The index parameter is used to keep track of the current word in the word array, and the recursion stops when the index equals the length of the word array. The function uses two lists to store combinations: one for the current index and one for the next index. The current list is added to the next list in two ways, with and without spaces.
This approach will work for any number of words and will generate all possible combinations efficiently.

How to replace variable in user input with math operation?

I need replace variable in string from user. For example:
User input: Ssad asdsdqwdq jdiqwj diqw jd qwld j {price-40%} asd asd asd
I know how replace only {price} but I don't know how to replace other cases.
I need support these cases:
{price}
{price-xx%}
{price+xx%}
{price-xx}
{price+xx}
{price/xx}
{price*xx}
And user can use variable {price} many times.
After user submit text, my app replace variable {price} or calc {price-xx%} and create a new string.
If I understood your problem correctly then I think you can evaluate the whole expression without Replacing variables (might you have to change placements of variables)
First, add 'System.Data' name space in your project
then:
double price = 110;
double xx = 15;
double result = 0;
result = Convert.ToDouble(new DataTable().Compute($"({price-(price*xx)/100})", null));
Console.WriteLine("{price - xx%} = " + result);
result = Convert.ToDouble(new DataTable().Compute($"({price + (price * xx) / 100})", null));
Console.WriteLine("{price + xx%} = " + result);
result = Convert.ToDouble(new DataTable().Compute($"({price}-{xx})", null));
Console.WriteLine("{price - xx} = " + result);
result = Convert.ToDouble(new DataTable().Compute($"({price}+{xx})", null));
Console.WriteLine("{price + xx} = " + result);
result = Convert.ToDouble(new DataTable().Compute($"({price}/{xx})", null));
Console.WriteLine("{price / xx} = " + result);
result = Convert.ToDouble(new DataTable().Compute($"({price}*{xx})", null));
Console.WriteLine("{price * xx} = " + result);
https://github.com/davideicardi/DynamicExpresso/
static void Main(string[] args)
{
int price = 100;
Regex regex = new Regex(#"(?<=\{).*?(?=\})");
string userInput = "Hi. I want to buy your computer. I can't offer {price} USD, but I can offer {price-(price/100)*10} USD";
string text = userInput;
foreach (var item in regex.Matches(text))
{
string expression = item.ToString().Replace("price", price.ToString());
var interpreter = new Interpreter();
var result = interpreter.Eval(expression);
text = regex.Replace(text, result.ToString(),1);
text = ReplaceFirst(text, "{", string.Empty);
text = ReplaceFirst(text, "}", string.Empty);
}
Console.WriteLine("Result: " + text);
}
public static string ReplaceFirst(string text, string search, string replace)
{
int pos = text.IndexOf(search);
if (pos < 0)
{
return text;
}
return text.Substring(0, pos) + replace + text.Substring(pos + search.Length);
}

C# multiple increments

I have, code who do for me "x + y = z"
if (command.Contains("+")) // string polecenie = "";'
{
polecenie = "{" + command + ")";
polecenie = polecenie.Replace("+", "}(");
double a = Convert.ToDouble(Between(polecenie, "{", "}"));
double b = Convert.ToDouble(Between(polecenie, "(", ")"));
double wyn = a + b;
richTextBox1.Text += a + " + " + b + " is " + wyn + "\r\n";
}
And when 'command' is "4+5","3 + 4" or something like this code works, but when i try to do "4 + 3 + 23" it don't work. Final string with starting 'command' "4+5+6", polecenie is: "{4}(5}(6)"... The Between Method:
public string Between(string content, string First, string Last)
{
string end = "";
int Plc1 = content.IndexOf(First) + First.Length;
int Plc2 = content.IndexOf(Last);
end = content.Substring(Plc1, Plc2 - Plc1);
return end;
}
How can I do that? (I want it to work with all possible additions ("4+4","34+5+54","43+4+65+54"...)
You could use the DataTable object to not re-invent the wheel.
richTextBox1.Text = string.Format("{0} is {1}\r\n", command,
(new System.Data.DataTable()).Compute(command, string.Empty));
This would support +, -, *, / and % (mod) operators. For more: https://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx

System.IO.File.WriteAllText outputs gibberish C#

I have a simple program in C# that is supposed to output a text file containing a sequence of character separated by commas. I also output the resulting sequence on the console and it looks fine, however, the text file is full of weird character and no commas.
This is the output :
㔳㌬ⰵⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰳⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰴⰰⰰⰰⰳⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰱⰱⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰰⰳⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰱⰱⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰳⰰⰴⰴⰴⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰱⰱⰱⰱⰱⰲⰰⰲⰱⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰰⰰⰰⰰⰰⰵⰰⰵⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰲⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰵⰰⰵⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰲⰰⰰⰵⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰰⰱⰰⰰⰰⰰⰰⰰⰰⰰⰰⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰵⰰⰰⰰⰴⰴⰰⰰⰰⰰⰱⰱⰱⰱⰱⰱⰰⰰⰱⰱⰱⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰰⰰⰰⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰵⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰳⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰵⰰⰰⰰⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰⰰⰰⰰⰰⰴⰴⰴⰴⰴⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰴⰴⰴⰴⰴⰴⰴⰴⰰⰰⰰⰰ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,0,0,1,0,0,0,0,0,0,0,0,0,1,4,4,0,0,4,4,4,4,0,
0,0,0,0,0,5,0,0,0,4,4,0,0,0,0,1,1,1,1,1,1,0,0,1,1,1,4,4,0,0,4,4,4,4,0,0,0,5,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,0,0,4,4,4,4,4,0,0,4,4,4,4,0,0,0,0,0,0,0,5,0,0,0,
0,0,0,0,0,0,0,4,4,4,4,0,0,0,0,0,0,0,0,0,4,4,4,4,0,0,0,0,0,5,0,0,0,0,0,0,0,0,0,0,
4,4,4,4,4,4,4,0,0,0,0,0,4,4,4,4,4,4,4,0,0,5,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,
4,4,4,0,0,3,0,4,4,4,4,4,4,4,0,0,0,0,0,5,0,0,0,4,4,4,4,0,0,4,4,4,4,4,4,4,4,0,0,0,
0,0,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,4,4,4,4,4,0,0,4,4,4,4,4,4,4,4,4,0,0,0,4,4,4,4,
4,4,4,4,0,0,0,0,0,0,0,0,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,0,
0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
As I said, what is supposed to be outputted is the string that is directly passed into File.WriteAllText.
Am I missing something ? I don't understand what i'm doing wrong. It seems to be working sometimes, but with other string it does not. I can find no relation between the sequences that works and those that do not.
Here is the whole code, it's a simple program that read a bitmap and output the sequence depending on the pixel. It's badly & quickly written but it's supposed to work, i see no reason it shouldn't.
class Program
{
static void Main(string[] args)
{
Console.WriteLine(args[0]);
Bitmap Map = new Bitmap(args[0]);
var MapRGB = Map.Clone(new Rectangle(0, 0, Map.Width, Map.Height), PixelFormat.Format32bppRgb);
int height = Map.Height;
int widht = Map.Width;
string MapText = height.ToString() + "," + widht.ToString() + ",";
for (int h = 0; h < height; h++)
{
for (int w = 0; w < widht; w++)
{
Color currentPixel =
MapRGB.GetPixel(w ,h );
if (currentPixel.ToArgb() == Color.White.ToArgb())
{
MapText = MapText + "0,";
}
if (currentPixel.ToArgb() == Color.Black.ToArgb())
{
MapText = MapText + "1,";
}
if (currentPixel.ToArgb() == Color.Gray.ToArgb())
{
MapText = MapText + "2,";
}
if (currentPixel.ToArgb() == Color.Yellow.ToArgb())
{
MapText = MapText + "3,";
}
if (currentPixel.ToArgb() == Color.Green.ToArgb())
{
MapText = MapText + "4,";
}
if (currentPixel.ToArgb() == Color.Red.ToArgb())
{
MapText = MapText + "5,";
}
Console.Clear();
Console.WriteLine(args[0] + " WROKING " + w.ToString() + " OF " + widht.ToString() + " IN " + h.ToString() + " OF " + height.ToString());
}
}
Console.Clear();
Console.Write(MapText);
System.IO.File.WriteAllText(Environment.CurrentDirectory + "/Map.bwn", MapText);
Console.ReadKey();
}
}
Not absolutely sure but could be an encoding issue. Use the other overload File.WriteAllText(String, String, Encoding) which takes a encoding type as argument like
File.WriteAllText(filePath, stringMessage, Encoding.UTF8);

Get this error when I try to debug. "An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code"

I am trying to make a application that makes all the first letters to capital letters and the others to small.
string navn = txtNavn.Text;
string delnavn1, delnavn2;
delnavn1 = navn.Substring(0, 1).ToUpper();
delnavn2 = navn.Substring(1).ToLower();
navn = delnavn1 + delnavn2;
if (navn.Contains(" "))
{
for (int i = 0; i < navn.Length; i++)
{
if (navn.Substring(i, 1) == " ")
{
delnavn1 = navn.Substring(i, 1).ToUpper();
delnavn2 = navn.Substring(i + 2).ToLower();
navn += delnavn1 + delnavn2 + " ";
}
}
}
else
{
delnavn1 = navn.Substring(0, 1).ToUpper();
delnavn2 = navn.Substring(1).ToLower();
navn = delnavn1 + delnavn2;
}
txbMelding.Text = "Du heter " + navn;
Sorry for weird variablenames, I am norwegian and did not have enough time to change them.
I suspect this is the immediate problem:
navn += delnavn1 + delnavn2 + " ";
You're concatenating with the previous value of navn. So if you start off with "Hello there", when you get to the first space you'll have:
delnavn1 = " "
delnavn2 = "There"
navn = "Hello there" + delnavn1 + delnavn2;
i.e. navn = "Hello there There".
Here's a rather simpler approach:
char[] text = txtNavn.Text.ToCharArray();
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
text[i] = i == 0 || text[i - 1] == ' '
? char.ToUpper(c)
: char.ToLower(c);
}
txbMelding.Text = "Du heter " + new string(text);
Replacing the letters in an array "in place" is much simpler than messing around with strings concatenation and substrings.
Note that this is still a very crude way of performing title-casing - you should probably look at TextInfo.ToTitleCase for a more comprehensive culture-sensitive code (and code that you don't need to write yourself!)

Categories