how to remove string after specific character in list - list

I have a list as given below.
List<string> testing = new List<string>();
testing.Add("1(1)(r)");
testing.Add("2(4)");
testing.Add("3(w)");
testing.Add("33(4)");
testing.Add("5(4)");
testing.Add("6(6)");
Now my problem is that i want to remove full string on or after "(" my list should be something like as given below.
1
2
3
33
5
6
How can i solve this problem with lambda expression or is ther any other way to remove string after some specific character.
Thanks in advance

var result = testing.Select(x => x.Split('(')[0]);
Another way:
var result = testing.Select(x => x.Substring(0, x.IndexOf('(')));
Second approach will throw exception if one of your strings doesn't contain any (

Related

Removing a String from a String REGEX

I have an automatically generated string which looks as follows:
["January","February",null,"April"]
I need to remove any match of ",null" from the string, ie:
["January","February",null,"April"] --> ["January","February","April"]
How can I find everything except for ",null"?
I have tried variations of "^(?!,null).*" without success.
To answer your question as stated, you don't need regex:
str = str.replace(",null", "");
However, to handle the edge cases too:
["January","February",null,"April"] --> ["January","February","April"]
["January",null,null,"April"] --> ["January","April"]
[null,"January","February","April"] --> ["January","February","April"]
["January","February","April",null] --> ["January","February","April"]
[null] --> []
you would be better served with regex:
str = str.replaceAll("(?<=\\[)null,?|,null", "");
The replacement regex caters for null the first (and potentially only) position, and any other case.
Please review this one, we can use filter function
var arr = [null, "January","February",null,"April", null];
var arr2 = arr.filter(function(x){return x !== null})
console.log(arr,arr2);

Adding numbers from a list of integer to listbox

I am currently having issues with trying to get a list of integers to show in a listbox.
I have more numbers to show, however i cant even get one number to show.
There is no error, the listbox but shows this System.Collections.Generic.List'1[System.Int32]
Dim URL = New Uri("http://www.hurriyet.com.tr/sans-oyunlari/sans-topu-sonuclari/")
Dim WebClient As New HttpClient
Dim Source = Await WebClient.GetStringAsync(URL)
Dim ListofNumber As List(Of Integer)
ListofNumber = New List(Of Integer)
Dim WebCode1 As String = "<span id=""_ctl0_ContentPlaceHolder1_lblresutone"" class=""hurriyet2010_so_sanstopu_no_text"">([^>]*)</span></div>"
For Each item As Match In (New Regex(WebCode1)).Matches(Source)
ListofNumber.Add(item.Groups(1).Value)
Next
listBox1.Items.Add(ListofNumber)
Currently you're adding a single item to the list, which is the List(Of Integer) object. You need to add each item in the list separately, like this:
For Each i As Integer In ListOfNumber
listBox1.Items.Add(i)
Next
Or, more simply:
listBox1.Items.AddRange(ListOfNumber)
As was already mentioned in the comments, but bears repeating, regex is typically the wrong tool for the job when you're parsing HTML. Using an HTML parser/DOM would be preferable in most cases.
Instead of:
listBox1.Items.Add(ListofNumber)
...it should be:
listBox1.DataSource = ListofNumber
This way you are binding your list of objects (in your case ListofNumber) to the listBox.
In fact you can bind any type of list, and the result shown in the listBox will be the .ToString() of each one of the items (in your case: int.ToString(), which is the string of the number).
An alternative to bind the data source would be: listBox1.Items.Clear(), and then add your items one by one through listBox1.Items.Add(yourItem), or as a group with listBox1.Items.AddRange(ListofNumber).
I believe the issue is with the ID on WebCode1 as ID's are meant to be used once and that is the case in the source downloaded.
Please try this
Dim URL = New Uri("http://www.hurriyet.com.tr/sans-oyunlari/sans-topu-sonuclari/")
Dim WebClient As New HttpClient
Dim Source = Await WebClient.GetStringAsync(URL)
Dim WebCode1 As String = "class=""hurriyet2010_so_sanstopu_no_text"">([^>]*)</span></div>"
ListBox1.DataSource =
(
From T In (New Regex(WebCode1)).Matches(Source) _
.Cast(Of System.Text.RegularExpressions.Match)() _
Select T.Groups(1).Value) _
.ToList

filter a List according to multiple contains

I want to filter a List, and I only want to keep a string if the string contains .jpg,.jpeg or .png:
scala> var list = List[String]("a1.png","a2.amr","a3.png","a4.jpg","a5.jpeg","a6.mp4","a7.amr","a9.mov","a10.wmv")
list: List[String] = List(a1.png, a2.amr, a3.png, a4.jpg, a5.jpeg, a6.mp4, a7.amr, a9.mov, a10.wmv)
I am not finding that .contains will help me!
Required output:
List("a1.png","a3.png","a4.jpg","a5.jpeg")
Use filter method.
list.filter( name => name.contains(pattern1) || name.contains(pattern2) )
If you have undefined amount of extentions:
val extensions = List("jpg", "png")
list.filter( p => extensions.exists(e => p.matches(s".*\\.$e$$")))
To select anything that contains one of an arbitrary number of extensions:
list.filter(p => extensions.exists(e => p.contains(e)))
Which is what #SergeyLagutin said above, but I thought I'd point out it doesn't need matches.
Why not use filter() with an appropriate function performing your selection/predicate?
e.g.
list.filter(x => x.endsWith(".jpg") || x.endsWith(".jpeg")
etc.

As3 Regex or alternative to split strings

i have a html page , i use regex to remove all html tags from the page and extract the text using the below code.
var foo = loader.data.replace(/<.*?>/g, "");
var bar:Array = foo.split("Total");
foo = foo.split(bar[0]);
trace(foo);
And using the same code lines below the replace method i remove every string before the word "TOTAL". It does the job perfectly but now i want to apply and other split to get contents after "TOTAL" and remove the Content after "BYTES".
So when i try to split it up again with
var bar2:Array = foo.split("BYTES");
foo = foo.split(bar2[0]);
Flash returns a error saying SPLIT is a not a valid method :S
I tried several other ways , ( REPLACE ) but still flash produces errors.
Can Anyone help me to get through this ?
Thank you
".split()" is a method of String. When you did the assignment below:
foo = foo.split(bar[0]);
foo became an array, and thus the call
var bar2:Array = foo.split("BYTES");
was being made to an array, which is invalid (no such method)
What you want instead is this:
var foo = loader.data.replace(/<.*?>/g, "");
trace(foo);
var result = foo.split("Total")[1].split("BYTES")[0];
trace(result);

Get Tridion Item ID with from URI Regex

If I have a Tridion URI like this 'tcm:1-23-8' and I want to get 23 with a Regular Expression.
The following works, but I know there is a better way. tcm: and '-8' are always there. The parts that change are 1 and 23.
var schemaUri = $display.getItem().getId(); // tcm:1-23-8
var re = /tcm:\d-/gi; // = 23-8
var schemaIdWithItemType = schemaUri.replace(re, "");
re = /-8/gi;
var schemaId = schemaIdWithItemType.replace(re, "");
If the number is always between the 2 dashes, you could do this:
var schemaId = schemaUri.split('-')[1];
This does the following:
split the string on the '-' character --> ['tcm:1', '23', '8'];
Get the second item from that array, '23'
Or, try this:
var schemaId = schemaUri.match(/-\d+-/)[0].replace(/-/g,'');
This'll find the number in between the dashes with .match(/-\d+-/), then remove the dashes.
Rather than calling $display.getItem().getId();, you can just call $display.getUri(); and then use the split()
var schemaId = $display.getUri().split('-')[1];
If you did want a pure Regex solution...
/^tcm:(\d+)-(\d+)(?:-(\d+))?$/i
Should validate your Tridion URI's format and provide you with 3 submatches, the second of which will be the Item ID