Deleting part of string - asp.net

I want to delete the part of string existing after "|". For Example String s = HelloWorld|ABCD i want to delete ABCD.
I want to do it in asp.net. Help Will be much appreciated.
subString method might be used for this but i dont know how to do it.
Kindly help me out with this.

int pipeIndex = s.IndexOf("|");
s = pipeIndex == - 1 ? s : s.Substring(0, pipeIndex);
This is the most efficient way, maybe you find this more readable:
s = s.Split('|')[0];

Related

add keys together in a dictionary

Hei guys! I need help in a python program. I wanna make a method which returns the sum of the keys as a dictionary. But I get a error "object is not iterable".
def totaltAntallSalg (dic) :
s = sum (dic.keys)
return s
call_function = totaltAntallSalg({"Ahmed":2,"Nada":1, "hala":3 })
How can I solve this problem?
thanks in advance
How can you add strings ? It might be values that you want to add.
To add values you may use following code:-
def totaltAntallSalg(dic):
D={}
D['sum']=sum(dic.values())
return D

How to replace " switch...case... " with "dictionary" for a better maintainability

In my code, i have switch case such as
switch(iSortCol) {
case1: if(iSortDir="desc"){order1 = order1.OrderByDescending(x=>x.GROUPNAME);
elseif(iSortDir="asc")order1 = order1.OrderBy (x=>x.GROUPNAME);
case2: if(iSortDir="desc"){order1 = order1.OrderByDescending(x=>x.GROUPMASTERID);
elseif(iSortDir="asc")order1 = order1.OrderBy (x=>x.GROUPMASTERID);}
....
case80:
case81:
}
The growing of "switch...case..." leads more maintain work for the code, i am thinking how to covert the code to Dictionary or strategy pattern for a better maintainability.
By the way, the property in order 1 such as "GROUPNAME","GROUPMASTERID" has different type
E.g. the "GROUPNAME" in (x=>x.GROUPNAME) is string type, but the "GROUPMASTERID" in (x=>x.GROUPMASTERID) int type.
Could anyone give me some clues, really appreciated

vb listview - reading a value into a string

I've searched thoroughly before asking, I'd like to find out how to take some text from a listview and pass it into a string. Theres only one column in the listview and only one row. The value in it is an integer.
The closest I came was this:
teststring = ListView1.Items.ToString
but that just filled the string with "System.Collections.Generic.List`1[System.Web.UI.WebControls.ListViewDataItem]"
Update: I'm working under ASP.NET
Use this,
teststring = ListView1.Items(ListView1.Items.Count - 1)
Instead of
teststring = ListView1.Items.ToString
Because your attempt will get the ListViewItemCollection (Non-Technically: group of items), But actually your need is to get a single item from that group. If you need further clarifications, Please refer this.
Update,
teststring = Ctype(ListView1.Items(ListView1.Items.Count - 1),Control).text
Try this:
teststring = ListView1.Items(0).Text

Replace part of string

I'm having difficulty with the following.
In VB.Net, I have the following line:
Dim intWidgetID As Integer = CType(Replace(strWidget, "portlet_", ""), Integer)
where strWidget = portlet_n
where n can be any whole number, i.e.
portlet_5
I am trying to convert this code to C#, but I keep getting errors, I currently have this:
intTabID = Convert.ToInt32(Strings.Replace(strTabID, "tab_group_", ""));
which I got using an online converter
But it doesn't like Strings
So my question is, how to I replace part of a string, so intTabID becomes 5 based on this example?
I've done a search for this, and found this link:
C# Replace part of a string
Can this not be done without regular expressions in c#, so basically, I'm trying to produce code as similar as possible to the original vb.net example code.
It should be like this strTabID.Replace("tab_group_", string.Empty);
int intTabID = 0;
string value = strTabID.Replace("tab_group_", string.Empty);
int.TryParse(value, out intTabID);
if (intTabID > 0)
{
}
And in your code i think you need to replace "tab_group_" with "portlet_"
Instead of Strings.Replace(strTabID, "tab_group_", ""), use strTabID.Replace("tab_group_", "").
This should work
int intWidgetID = int.Parse(strTabID.Replace("tab_group_",""));//Can also use TryParse
Their is no Strings class in Vb.Net so please use the string class instead http://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
you can achieve it by this way
string strWidget = "portlet_n";
int intWidgetID = Convert.ToInt32(strWidget.Split('_')[1]);

Replace string from character onwards

I've got a string like so
Jamie(123)
And I'm trying to just show Jamie without the brackets etc
All the names are different lengths so I was wondering if there was a simple way of replacing everything from the first bracket onwards?
Some others are displayed like this
Tom(Test(123))
Jack ((4u72))
I've got a simple replace of the bracket at the moment like this
mystring.Replace("(", "").Replace(")","")
Any help would be appreciated
Thanks
VB.NET
mystring.Substring(0, mystring.IndexOf("("C)).Trim()
C#
mystring.Substring(0, mystring.IndexOf('(')).Trim();
One logic; get the index of the ( and you can trim the later part from that position.
public static string Remove(string value)
{
int pos = value.IndexOf("(");
if (pos >= 0)
{
return value.Remove(pos, remove.Length);
}
return value;
}
aneal's will work. The alternative I generally use because it's a bit more flexible is .substring.
string newstring = oldstring.substring(0,oldstring.indexof("("));
If you aren't sure that oldstring will have a "(" you will have to do the test first just as aneal shows in their answer.
String.Remove(Int32) will do what you need:
Deletes all the characters from this string beginning at a
specified position and continuing through the last position.
You will also have to .Trim() as well given the data with padding:
mystring = mystring.Remove(mystring.IndexOf("("C))).Trim()

Resources