How to implement string manipulation in efficienct way? - c#-4.0

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);

Related

How to replace a string in javascript

Here is my data
data = "67.45"
want to remove the double quotes and wanted my out to be as
data = 67.45
What is the best way to achieve
Thanks in advance
var data = "67.45"
console.log(parseFloat(data))
data is having string value. if you want to convert string to float.
you just need to use parseFloat.
var data = "67.45"
parseFloat(data)
you will get float value
you can do this:
var ret = data.replace('"','');
console.log(ret); //should show your string with out quotes
or you can use this:
data.replace(/"/g,'');

How to remove string after and before specific chars

I have this string: https://2352353252142dsbxcs35#github.com/happy.git
I want to get result: https://github.com/happy.git (without random string after second / and after # but without #).
Now I have something like this:
var s = 'https://2352353252142dsbxcs35#github.com/happy.git';
var d = s.substring(s.indexOf('/')+2, s.indexOf('#')+1;
s = s.replace(d, "");
it works, but I know it's an ugly solution.
What is the most efficient and more universal solution?
Try this:
const indexOfAtSign: number = receivedMessage.indexOf('#')+1
const httpsString: string = 'https://'
const trimmedString: string = s.slice(indexOfAtSign)
const requiredURL: string = httpsString.concat(trimmedString)
// Print this value of requiredURL wherever you want.
So here what my code does is, it gets position of # and removes everything before it along with the sign itself. Then using the slice() function, we are left with the remaining part which I named as trimmedString. Now I have pre-defined the `https string, anf we just need to merge them now. Done :-)
I had tried this out in my telegram bot and here's how it works:

Typescript string manipulation not working

I have a ";" delimited string. I need to remove an entry from it. I tried to use slice but that does get sliced string but not the original-modified string.
Here is an example:
var str1: string = 'TY66447;BH31496;PA99001;';
var str2 = str1.slice(16, 23);
console.log(str1);
console.log(str2);
It gives:
TY66447;BH31496;PA99001;
PA99001
But what I want to achieve is TY66447;BH31496;
I am not sure if I am using the correct string method. Please guide how to achieve.
I don't understand what do you want, but it seems that you want:
str1.slice(0, 16);

extract string with linq

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

Is there any way to retrieve a appended int value to a String in javaScript?

I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number

Resources