Dailycode.info

Short solution for short problems

How to count string in string

I was looking for a c# function to count the number of occurrences of a string in a string. But I couldn't find any by default.

So I wrote my own little function. Migth come in handy!

In this scenario I had a string that contains line feeds, I had to know how many to correctly calculate the length of the string. Here is the function that does the work:

public static int Count(string src, string find)
        {
            int ret = 0;
            int len = find.Length;
            for (int i=0; i < src.Length-len;i++)
            {
                if (src.Substring(i,len) == find)
                {
                    ++ret;
                }
            }
            return ret;
        }

So I was looking for "\r\n" in a string:

int countNewLines = Count(day.Remark,"\r\n");
 

It's Simple and easy, any person could find this, but I like to blog it so next time I won't have to look for it again.

This improved function by feedback of Tom is more performant:

private int countOccurences(string text, string toFind)
{
  return (toFind.Length > 0) ? 
  (text.Length - text.Replace(toFind, string.Empty).Length) / toFind.Length : 0;
}
 

I'll explain what happens: it returns the length of the text minus the lenght of the text without the string that you are looking for divided by the length of the search string.

Eg. text = aaaabgaabg to find = bg. string lenght = 10 - string lenght without bg = 6 makes 4. We divide 4 by the length of the search string bg = 2 makes 2 occurences: (10 - 6) /2=2


Short if structure

It keeps slippin my mind, so here an example, making it easy to find:

string test = i<0?"yes":"no";

 

If i < 0 then it returns string "yes" else "no".

Syntax:  Bool Res = Coparison ? returnIfTrue : returnIfFalse ;

You can have funny looking statements like this:

MyColumns.Add("has_sub_grades", hasSubGrades?1:0);

 

Then you can also nest these if structures, don't nest to much or it will be hard to read it. Here an example:

Genderoutcome = rbtnBoth.Checked?Gender.Unisex:rbtnMale.Checked?Gender.Male:Gender.Female


How to change Option item index with javascript

Again a small script that enables you to order items in a listbox or (html) optoin. Just include this script in your page:

function move(index,to,list) 
{
    var total = list.options.length-1;
    if (index == -1) return false;
    if (to == +1 && index == total) return false;
    if (to == -1 && index == 0) return false;
    var items = new Array;
    var values = new Array;
    for (i = total; i >= 0; i--) 
    {
        items[i] = list.options[i].text;
        values[i] = list.options[i].value;
    }
    for (i = total; i >= 0; i--) 
    {
        if (index == i) 
        {
            list.options[i + to] = new Option(items[i],values[i], 0, 1);
            list.options[i] = new Option(items[i + to], values[i + to]);
            i--;
        }
        else 
        {
            list.options[i] = new Option(items[i], values[i]);
        }
    }
    list.focus();
}

 

When you are using asp.Net you can set this script from the code behind like this:

btnUp.Attributes.Add("onclick", "move(" + lstOrder.ClientID + 
".selectedIndex, -1,"+lstOrder.ClientID+")");
btnDown.Attributes.Add("onclick", "move(" + lstOrder.ClientID + 
".selectedIndex,+1," + lstOrder.ClientID + ")");

 

You can now click on the up and down arrows to move an item in the list.

         

I had lots of help from this site: http://javascript.internet.com/forms/selection-order.html


SPQuery One or more field types are not installed properly. Go to the list settings page to delete these fields

I was trying to order a query on a document library to get all document names ordered in a dropdown. The code looked like this: 

SPList docLib = getDocLib();
 
SPQuery oQuery = new SPQuery();
oQuery.ViewXml = "<View><Query><OrderBy><FieldRef
 Name='Name'/></OrderBy><Where><Gt><FieldRef Name='ID'/>"
 + "<Value Type='Text'>0</Value></Gt></Where></Query></View>";
SPListItemCollection collListItems = docLib.GetItems(oQuery);

 

When I executed the code, I got the error: "One or more field types are not installed properly. Go to the list settings page to delete these fields.". So I first looked around but could not find the correct google words. I had to find it myself. I saved the list as a template, downloaded the stp file and opened it with winzip. The next thing I did was opening the manifest.xml file. I pressed ctrl-f and searched for the string: "FieldRef". Then you will find this in the manifest.xml file:

<ViewFields>
<FieldRef Name="DocIcon"/>
<FieldRef Name="LinkFilename"/>
<FieldRef Name="Modified"/>
<FieldRef Name="Editor"/>
<FieldRef Name="SOPAppro"/>
<FieldRef Name="_Status"/>
<FieldRef Name="SOP_x0020_type"/>
</ViewFields> 

 

I found a field called: LinkFileName.  I decided to use this in the query, and guess what, it worked. The names we see are not always the names SharePoint uses. This is mainly for fields that are defined by SharePoint itself. So my code looks like this now:

SPList docLib = getDocLib();
 
SPQuery oQuery = new SPQuery();
oQuery.ViewXml = "<View><Query><OrderBy><FieldRef
 Name='LinkFilename'/></OrderBy><Where><Gt><FieldRef Name='ID'/>"
 + "<Value Type='Text'>0</Value></Gt></Where></Query></View>";
SPListItemCollection collListItems = docLib.GetItems(oQuery);

 

And it does the trick. So if you are looking for field name in WSS, you can try it this way.