Is there a nice way to extract part of a string with linq, example:
I have
string s = "System.Collections.*";
or
string s2 = "System.Collections.Somethingelse.*";
my goal is to extract anything in the string without the last '.*'
thankx I am using C#
The simplest way might be to use String.LastIndexOf followed by String.Substring
int index = s.LastIndexOf('.');
string output = s.Substring(0, index);
Unless you have a specific requirement to use LINQ for learning purposes of course.
You might want a regex instead. (.*)\.\*
With the regex:
string input="System.Collections.Somethingelse.*";
string output=Regex.Matches(input,#"\b.*\b").Value;
output is:
"System.Collections.Somethingelse"
(because "*" is not a word) although a simple
output=input.Replace(".*","");
would have worked :P
Related
I need to extract parts of string, I found 2 methods which one is better in coding practices and is it a good coding practice to use string manipulation? is there any other way in which this task can be done?
string test = "name.jpg_add1_srcimages_pagetest.htm";
string img_name = test.substring(0,str.LastIndexOf("_add"));
//or
string[] img_prop = test.Split('_');
string img_n = img_prop[0]
For performance string.Split is optimized.
string:
ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd
substring that i want to get:
https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C
i tried this but doesn't work
print (log.split("ecsdcsdcsdfvdfv",1)[1])
You can try this
log.split('":"')[1].split('","')[0]
But this is not the best way to do what you are trying to achieve. Better parse it and get what you want.
This yield the expected result.
The way you used .split() was wrong, you did not include the " character.
string = 'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'
substring = string.split('ecsdcsdcsdfvdfv":"')[1].split('","vsvdfvsfvcfvfdcvfd')[0]
If you want to get the string between the ":" and ",", then you can use regular expression to do it.
re.match('(.*\":\")([^\",\"]*)(\",\".*)', log).group(2)
Given your input
'ecsdcsdcsdfvdfv":"https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C","vsvdfvsfvcfvfdcvfd'
you will get
'https://scdsscdcsdent-mxp1-1.cdninstdscsdcagdssdcsdam.com/v/t51.283485-19/s320x320/79000872_1455436197941341_7513464347075543040_n.pnk?_nc_ht=scontent-mxp1-1.cdninadcdcdm.codcsdcm&_nc_ohc=0fehqjedb48AX8r72Hi&oh=eb1f6a78a2dcd67e443aa7f74eee91b4&oe=5E7F0A0C'
And if you input something like
'ecsdcsdcs":"dfvdfv":"https://s...F0A0C","vsvdfvsfvcfvfdc","vfd'
you will get
'https://s...F0A0C'
Dont forget to import re.
I have a string in excel that I need to extract a substring from
This is an example of the string:
<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>
I'm new to regex and powershell, but I'm trying to find a way to extract the "hostname here" portion of the string. It's variable length, so indexing won't be reliable.
since you changed the sample, the comment code i posted won't work. [grin] this will, tho ...
$InStuff = '<\Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue>'
$InStuff.Split(':')[-1].Split('<')[0].Trim()
output = hostnamehere
if you have a set of sample strings, then you likely otta post them so the code can be arranged to handle the needed variants.
If that were xml, it would be straightforward
[xml]$xml = '<Text Name="Text5"><TextValue>Hostname: hostnamehere</TextValue></Text>'
(-split $xml.text.textvalue)[1]
hostnamehere
Something like that is coming in:
str="Hello;this;is;a;text"
What I do want as result is this:
result="72:101:108:108:111;116:104:105:115;..."
which should be the Text in ASCII.
You could use string matching to get each word separated by ; and then convert, concat:
local str = "Hello;this;is;a;text"
for word in str:gmatch("[^;]+") do
ascii = table.pack(word:byte(1, -1))
local converted = table.concat(ascii, ":")
print(converted)
end
The output of the above code is:
72:101:108:108:111
116:104:105:115
105:115
97
116:101:120:116
I'll leave the rest of work to you. Hint: use table.concat.
Here is another approach, which exploits that fact that gsub accepts a table where it reads replacements:
T={}
for c=0,255 do
T[string.char(c)]=c..":"
end
T[";"]=";"
str="Hello;this;is;a;text"
result=str:gsub(".",T):gsub(":;",";")
print(result)
Another possibility:
function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
Finding certain character or string (such as ";") can be done by using string.find - https://www.lua.org/pil/20.1.html
Converting character to its ASCII code can be done by string.byte - https://www.lua.org/pil/20.html
What you need to do is build a new string using two functions mentioned above. If you need more string-based functions please visit official Lua site: https://www.lua.org/pil/contents.html
Okay...I got way further, but I can't find how to return a string made up of two seperate strings like
str=str1&" "&str2
I have a string ="/show/search/All.aspx?Att=A1". How to get the last value after the 'Att=' in efficient way ?
You could do a split on the '=' character.
Example (in C#):
string line = "/show/search/All.aspx?Att=A1";
string[] parts = line.Split('=');
//parts[1] contains A1;
Hope this helps
If you're only dealing with this one URL then both of the other answers would work fine. I would consider using the HttpUtility.ParseQueryString method and just pull out the item you want by key.
Whatever an
efficient way
is...
Try this:
var str = "/show/search/All.aspx?Att=A1";
var searchString = "Att=";
var answer = str.Substring(str.IndexOf(searchString) + searchString.Length);