code does not insert all elements of array to string - c#

i have this code which should insert all elements of array which is some html on random places in a string. but it only inserts last element to that string. please help
Random insertPos = new Random();
int pos = insertPos.Next(txtInput.Text.Length);
int firSpace= txtInput.Text.IndexOf(" ", pos);
int secSpace = txtInput.Text.IndexOf(" ", firSpace+1);
int wLen = secSpace - firSpace;<br/>
string word = txtInput.Text.Substring(firSpace,wLen);
foreach (string url in urlArray)
{
txtOutput.Text =
txtInput.Text.Replace(word, "" + word + "");
}

You are replacing (=) the contents of txtOutput.Text on every iteration so of course you are only going to see the last result. Consider using +=1 first to get it working and then a StringBuilder if your performance is suffering and this is a bottleneck.
1: It's not clear exactly how you want it formatted, but at least += will append and assign the result of the append instead of just the result of the current iteration.

You overwrite the Text property of the textbox in every step of the foreach loop. Only the result of last loop will be left.

You are repeatedly saying "figure out what happens when I insert the HTML into txtInput 's text, and assign the result to the txtOutput text". But this does not actually change the text in txtInput, so you are starting fresh each time; and you throw away the txtOutput text each time to replace it with the new stuff.

As Jaison said you are using = instead of +=, but there are better solutions. Remember that strings are immutable. Use string.Format or StringBuilder where you're concatenating strings. Examples:
string[] strArray = {"a", "b", "c"};
string word = "word";
//1st solution +=
string output = "";
foreach (string str in strArray)
output += "" + word + "";
Console.WriteLine(output);
//better solution string.Format
output = "";
foreach (string str in strArray)
output += string.Format("{1}", str, word);
Console.WriteLine(output);
//StringBuilder
StringBuilder sb = new StringBuilder();
foreach (string str in strArray)
sb.AppendFormat("{1}", str, word);
output = sb.ToString();
Console.WriteLine(output);
//linq & string.Join
output = string.Join("", strArray.Select( str => string.Format("{1}", str, word)).ToArray());
Console.WriteLine(output);
Console.Read();

Related

Get first word on every new line in a long string?

I am trying to add a leaderboard in my unity app
I have a long string as below(just an example, actual string is http pipe data from my web service, not manually stored):
string str ="name1|10|junk data.....\n
name2|9|junk data.....\n
name3|8|junk data.....\n
name4|7|junk data....."
I want to get the first word (string before the first pipe '|' like name1,name2...) from every line and store it in an array and then get the numbers (10,9,8... arter the '|') and store it in an other one.
Anyone know whats the best way to do this?
Fiddle here: https://dotnetfiddle.net/utp4HK
code below, you may want to revisit the algorithm for performance, but if that is not an issue, this will do the trick;
using System;
public class Program
{
public static void Main()
{
string str ="name1|10|junk data.....\nname2|9|junk data.....\nname3|8|junkdata.....\nname4|7|junk data.....";
foreach (var line in str.Split('\n'))
{
Console.WriteLine(line.Split('|')[0]);
}
}
}
First split by new-line characters:
string[] lines = str.Split(new string[]{Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
Then you can use LINQ to get both arrays:
var data = lines.Select(l => l.Trim().Split('|')).Where(arr => arr.Length > 1);
string[] names = data.Select(arr => arr[0].Trim()).ToArray();
string[] numbers = data.Select(arr => arr[1].Trim()).ToArray();
Check out this link on splitting strings: http://msdn.microsoft.com/en-us/library/ms228388.aspx
You could first create an array of strings (one for each line) by splitting the long string with \n as the delimeter.
Then, you could split each line with | as the delimeter. The name would be the 0th index of the array and the number would be the 1st index of the array.
First of all, you can't have a multi line string without using verbatim string literal. With using verbatim string literal, you can split your string based on \r\n or Environment.NewLine like;
string str = #"name1|10|junk data.....
name2|9|junk data.....
name3|8|junk data.....
name4|7|junk data.....";
var array = str.Split(new []{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries);
foreach (var item in array)
{
Console.WriteLine(item.Split(new[]{"|"},
StringSplitOptions.RemoveEmptyEntries)[0].Trim());
}
Output will be;
name1
name2
name3
name4
Try this:
string str ="name1|10|junk data.....\n" +
"name2|9|junk data.....\n" +
"name3|8|junk data.....\n" +
"name4|7|junk data.....";
string[] tempArray1 = str.Split('\n');
string[] tempArray2 = null;
string[,] newArray = null;
for (int i = 0; i < tempArray1.Length; i++)
{
tempArray2 = tempArray1[i].Split('|');
if (newArray[0, 0].ToString().Length == 0)
{
newArray = new string[tempArray1.Length, tempArray2.Length];
}
for (int j = 0; j < tempArray2.Length; j++)
{
newArray[i,j] = tempArray2[j];
}
}

How to remove comma on a for loop?

I want to remove the first comma. Is there a way to remove the comma of the loop?
Here's my code:
foreach (var field in tablefields.Items)
{
if (m_datacount < datatype.Items.Count)
{
d_type = datatype.Items[m_datacount].ToString();
m_datacount++;
}
richTextBox1.Text = richTextBox1.Text + "\t,#" + field + " " + d_type + "\n";
datatype.ResetText();
}
m_datacount = 0;
Output:
,#id uniqueidentifier
,#title varchar
,#active_flag bit
,#created_by_id uniqueidentifier
,#created_by_system_code varchar
,#created_date_time datetime
,#modified_by_id uniqueidentifier
,#modified_by_system_code varchar
,#modified_date_time datetime
Why reinvent the wheel....
Simply use String.Join
string result = String.Join(", ", myarray);
for e.g.
string[] myarray = { "Firstuser", "Seconduser" };
So result will be "Firstuser, Seconduser",
I would use String.Join. Since you have commented on another answer that tablefields is a ListBox this works:
var strings = tablefields.Items.Cast<object>().Select(o => o.ToString());
richTextBox1.Text = String.Join(Environment.NewLine + ",", strings);
Tested with this sample data:
tablefields.Items.AddRange(new ListBox.ObjectCollection(tablefields, new[]{"id","spid","charge_type_rcd","name_e","active_status"}));
Output:
id
,spid
,charge_type_rcd
,name_e
,active_status
You can also use a StringBuilder which is less readable but can be more efficient if you have many, many items:
StringBuilder sb = new StringBuilder();
foreach (var item in tablefields.Items)
sb.AppendLine(item.ToString()).Append(',');
if (sb.Length > 0) sb.Length--; // to remove the last comma
richTextBox1.Text = sb.ToString();
TrimEnd method can be used to remove trailing characters from a string:
richTextBox1.Text = richTextBox1.Text.TrimEnd(',');
you can use below menioned code
richTextBox1.Text = richTextBox1.Text.Substring(0,richTextBox1.Text.Length-1);
What is the purpose of field2? It isn't used in your code...
Why use a counter (m_fieldcount) if you're using a foreach loop?
What does tablefields.ResetText(); do?
Anyway, try this:
richTextBox1.Text = String.Join("\n\t,", tablefields.Items.Select(a=>a.ToString()).ToArray());
richTextBox1.Text + "" + t_fields + "\n\t,".Remove(richTextBox1.Text.Lenght - 1);
check out also removing the last index. using Remove(index)

Extract node value from xml resembling string C#

I am having strings like below
<ad nameId="\862094\"></ad>
or comma seprated like below
<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad nameId="\865599\"></ad>
How to extract nameId value and store in single string like below
string extractedValues ="862094";
or in case of comma seprated string above
string extractedMultipleValues ="862593,862094,865599";
This is what I have started trying with but not sure
string myString = "<ad nameId="\862593\"></ad>,<ad nameId="\862094\"></ad>,<ad
nameId="\865599\"></ad>";
string[] myStringArray = myString .Split(',');
foreach (string str in myStringArray )
{
xd.LoadXml(str);
chkStringVal = xd.SelectSingleNode("/ad/#nameId").Value;
}
Search for:
<ad nameId="\\(\d*)\\"><\/ad>
Replace with:
$1
Note that you must search globally. Example: http://www.regex101.com/r/pL2lX1
Please see code below to extract all numbers in your example:
string value = #"<ad nameId=""\862093\""></ad>,<ad nameId=""\862094\""></ad>,<ad nameId=""\865599\""></ad>";
var matches = Regex.Matches(value, #"(\\\d*\\)", RegexOptions.RightToLeft);
foreach (Group item in matches)
{
string yourMatchNumber = item.Value;
}
Try like this;
string s = #"<ad nameId=""\862094\""></ad>";
if (!(s.Contains(",")))
{
string extractedValues = s.Substring(s.IndexOf("\\") + 1, s.LastIndexOf("\\") - s.IndexOf("\\") - 1);
}
else
{
string[] array = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string extractedMultipleValues = "";
for (int i = 0; i < array.Length; i++)
{
extractedMultipleValues += array[i].Substring(array[i].IndexOf("\\") + 1, array[i].LastIndexOf("\\") - array[i].IndexOf("\\") - 1) + ",";
}
Console.WriteLine(extractedMultipleValues.Substring(0, extractedMultipleValues.Length -1));
}
mhasan, here goes an example of what you need(well almost)
EDITED: complete code (it's a little tricky)
(Sorry for the image but i have some troubles with tags in the editor, i can send the code by email if you want :) )
A little explanation about the code, it replaces all ocurrences of parsePattern in the given string, so if the given string has multiple tags separated by "," the final result will be the numbers separated by "," stored in parse variable....
Hope it helps

Extracting parts of a string c#

In C# what would be the best way of splitting this sort of string?
%%x%%a,b,c,d
So that I end up with the value between the %% AND another variable containing everything right of the second %%
i.e. var x = "x"; var y = "a,b,c,d"
Where a,b,c.. could be an infinite comma seperated list. I need to extract the list and the value between the two double-percentage signs.
(To combat the infinite part, I thought perhaps seperating the string out to: %%x%% and a,b,c,d. At this point I can just use something like this to get X.
var tag = "%%";
var startTag = tag;
int startIndex = s.IndexOf(startTag) + startTag.Length;
int endIndex = s.IndexOf(tag, startIndex);
return s.Substring(startIndex, endIndex - startIndex);
Would the best approach be to use regex or use lots of indexOf and substring to do the extracting based on te static %% characters?
Given that what you want is "x,a,b,c,d" the Split() function is actually pretty powerful and regex would be overkill for this.
Here's an example:
string test = "%%x%%a,b,c,d";
string[] result = test.Split(new char[] { '%', ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s in result) {
Console.WriteLine(s);
}
Basicly we ask it to split by both '%' and ',' and ignore empty results (eg. the result between "%%"). Here's the result:
x
a
b
c
d
To Extract X:
If %% is always at the start then;
string s = "%%x%%a,b,c,d,h";
s = s.Substring(2,s.LastIndexOf("%%")-2);
//Console.WriteLine(s);
Else;
string s = "v,u,m,n,%%x%%a,b,c,d,h";
s = s.Substring(s.IndexOf("%%")+2,s.LastIndexOf("%%")-s.IndexOf("%%")-2);
//Console.WriteLine(s);
If you need to get them all at once then use this;
string s = "m,n,%%x%%a,b,c,d";
var myList = s.ToArray()
.Where(c=> (c != '%' && c!=','))
.Select(c=>c).ToList();
This'll let you do it all in one go:
string pattern = "^%%(.+?)%%(?:(.+?)(?:,|$))*$";
string input = "%%x%%a,b,c,d";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
// "x"
string first = match.Groups[1].Value;
// { "a", "b", "c", "d" }
string[] repeated = match.Groups[2].Captures.Cast<Capture>()
.Select(c => c.Value).ToArray();
}
You can use the char.IsLetter to get all the list of letter
string test = "%%x%%a,b,c,d";
var l = test.Where(c => char.IsLetter(c)).ToArray();
var output = string.Join(", ", l.OrderBy(c => c));
Since you want the value between the %% and everything after in separate variables and you don't need to parse the CSV, I think a RegEx solution would be your best choice.
var inputString = #"%%x%%a,b,c,d";
var regExPattern = #"^%%(?<x>.+)%%(?<csv>.+)$";
var match = Regex.Match(inputString, regExPattern);
foreach (var item in match.Groups)
{
Console.WriteLine(item);
}
The pattern has 2 named groups called x and csv, so rather than just looping, you can easily reference them by name and assign them to values:
var x = match.Groups["x"];
var y = match.Groups["csv"];

Getting rid of null/empty string values in a C# array

I have a program where an array gets its data using string.Split(char[] delimiter).
(using ';' as delimiter.)
Some of the values, though, are null. I.e. the string has parts where there is no data so it does something like this:
1 ;2 ; ; 3;
This leads to my array having null values.
How do I get rid of them?
Try this:
yourString.Split(new string[] {";"}, StringSplitOptions.RemoveEmptyEntries);
You could use the Where linq extension method to only return the non-null or empty values.
string someString = "1;2;;3;";
IEnumerable<string> myResults = someString.Split(';').Where<string>(s => !string.IsNullOrEmpty(s));
public static string[] nullLessArray(string[] src)
{
Array.Sort(src);
Array.Reverse(src);
int index = Array.IndexOf(src, null);
string[] outputArray = new string[index];
for (int counter = 0; counter < index; counter++)
{
outputArray[counter] = src[counter];
}
return outputArray;
}
You should replace multiple adjacent semicolons with one semicolon before splitting the data.
This would replace two semicolons with one semicolon:
datastr = datastr.replace(";;",";");
But, if you have more than two semicolons together, regex would be better.
datastr = Regex.Replace(datastr, "([;][;]+)", ";");
words = poly[a].Split(charseparators, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in words)
{
richTextBox1.Text += (d + 1)+ " " + word.Trim(',')+ "\r\n";
d++;
}
charseparators is a space

Categories