How to get string from whole [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a string whose text is something like:
Ebook/AspNetBook.Pdf/.NetBook
or
Ebook/Asp.Net/AspNetBook.Pdf/.NetBook
or may be this
Ebook/Asp.Net/.NetBook/AspNetBook.Pdf
That means string may be come with many forward slashes and it will not come in fix format. I want to get file name and its ext AspNetBook.Pdf from this string.
I have tried to get string to using sub string or replace character before period but do not get proper result.

If your strings always have that particular form string1/string2/string3 you can either use a simple string split or a regular expression.
var xx = input.Split('/')[1];
Or
var xx = Regex.Match(input, #".+/(.+)/.+").Groups[1].Value;
Be aware, this contains no error checking, for instance if the input does not match the given form

This might help you.
string st = "Ebook/AspNetBook.Pdf/.NetBook";
string newst = st.Substring(st.IndexOf('/') + 1);
string outptst = newst.Substring(0,newst.IndexOf('/'));
since you wanted to get the string between 2 forward slashes. This would give you the correct output required.

Try with this:
string st = "Ebook/AspNetBook.Pdf/.NetBook";
string out = st.Split('/')[1];
Be aware that this will only work if you always have a / in your input and if it is always the second value.

Here p1 will find first index of '/' and p2 will index of '/' right after p1.
String text= "Ebook/AspNetBook.Pdf/.NetBook";
int p1=text.indexOf('/');
int p2=-1;
if(p1!=-1)
p2=text.indexOf('/',p1+1);
if(p1!=-1&&p2!=-1)
System.out.print(text.substring(p1+1,p2);

Related

Get string between two complicated string in a string C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
Improve this question
I have shared the relevant image below.
Text in image ;
"C:\Users\Hp\OneDrive-blablablabla\Masaustu\EdaGorsel\4.PNG\tmanager\tmanager\t2022/12/12\r\nddd\tV1.0"
I only want 4.PNG from the above post
2.I should not use special names or filenames
3.Ex: \t between \ like this
I hope I was able to explain my problem
you could use a Regex match to get this part of the url :
private Regex _regex = new Regex(#"^.*\\(?<Image>.*\.PNG)\\.*$");
(You can test regex here)
Then use this to read the match :
var result = _regex.Match(url).Groups["Image"].Value;
Another, simplier solution, would be to use :
url.Split("\\")[6];
This works only if the searched part is always at the same spot.
You can split your strings into many shorter ones using .Split function:
string[] words = path.Split("\\");
Using the double \ to escape the character..
If you don't guarantee that the string you need is always at the same position, you can use this to fetch it from the list:
string neededString = words .FirstOrDefault(str => str.Contains(".PNG"));
This will return the first instance of any word from your string that contains ".png" in it.
there is sevral ways
if you know number of "" in your string then split it(str.Split(' \ ')) and get wanted index
use regular expersion to extract filename

Getting a number part from a url string in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a URL string that is something like below:
https://example.com/app/1095600/example2234
I want to only get "1095600"
this number is variable and can be three or more digits long.
If the number is always in the same position (following https://example.com/app/), you could split the string by the slash (/) character and extract it:
string input = "https://example.com/app/1095600/example2234";
string result = input.Split("/")[4];
You can try matching the required substring with a help of regular expressions, esp. if you have elaborated criteria
three or more digits
Code:
using System.Text.RegularExpressions;
...
string url = "https://example.com/app/1095600/example2234";
string number = Regex.Match(url, #"(?<=\/)[0-9]{3,}(?=\/|$)").Value;
Mureinik's answer will work fine, but can quickly break if your URL is missing the https:// part. It would be much better to convert the string to a Uri and use the Uri.Segments property to extract the second segment of the path.
Uri address = new Uri("https://example.com/app/1095600/example2234");
string id = address.Segments[2].Replace("/", "");
The segments include the ending slash, so you need to remove it manually.
Identify the begining, and the ending string marks.
Beginning = "/app/", Ending = "/".
Cut it out of there.
String input = "https://example.com/app/1095600/example2234";
String str_number = input.Substring(input.IndexOf("/app/")+5).Split('/')[0];
int int_number = Convert.ToInt32(str_number);
Always try to write the simplest code you can get. And have fun!

Find a word between 2 words and replace them [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
How to find between 2 words and replace them into new string?
I wanna replace same.
for example given string :
string s = "word1-word2same-word3-word4";
same is = "word2";
What i wanna :
string s = "word1-word2word2-word3-word4";
But what if given string is:
string s = "word1same-word2-word3-word4";
same is -> word1;
What i wanna in this example:
string s = "word1word1-word2-word3-word4";
How to find what word is containing same? How to do it?
I think the following regular expression replacement is what you want
var input = "word1same-word2same-word3";
var result = Regex.Replace(input, "(?<=^|-)([^-]*)same", "$1$1");
Console.WriteLine(result);
would give you
word1word1-word2word2-word3
So it will replace all the instances of "same" with whatever comes before it up to a hyphen or the beginning of the string.
Note that something like "whatsamesamesame" will give you a result of "whatsamesamewhatsamesame", basically it will only replace the last "same" after a hyphen or the beginning of the string.

Why String Format does not work in c# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
string test="add number here {0}";
int number=7;
Why this does not work
String.Format(test,number);
string res=test;
but this work
String.Format("add number here {0}",number);
Because you test it wrong. In thise 2 lines:
String.Format(test,number);
string res=test;
String.Format returns a string. As can be seen in the documentation And this new string is the composed string with your number that you expect. If you assign the format string test to res and check whether string res has now the desired output. You will (of course) not find it but only
"add number here {0}";
You need to use the return value of the method String.Format. There you will find the desired output:
string res = String.Format(test,number);
Console.WriteLine(res);
Let me add few notes for you regarding the String.Format.
From the first code that you have added with the question, I hope that you misunderstand the usage of String.Format, As you expected it won't replace the placeholders in the input string with the objects. Actually It Replaces the placeholders with the arguments/objects and returns another string. So you have to look into the return value from the .Format() method for the actual formatted string.
That is why this doesn't work
String.Format(test,number);
string res=test;
Whereas this works fine:
string res = String.Format(test,number);
test = String.Format(test,number);

extract a word found in a String [] [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
string [] leerArchivo;
good evening, i have to get a sub-string from a string. I have a file type "string []" that contains a string like this.
//start class ClassName xxxxx
where ClassName can be any name, then this is a description that does not interest me to capture, so if I need in the optener (ClassName)
thank you very much beforehand, I hope and make me understand.
To do this you call the "Substring" method in which you specify the first and last indexes of the characters you want to get.
string input = "OneTwoThree";
//Get first three characters
string sub = input.Substring(0, 3);
The output would be just the first 3 letters (in this case "One").
You can also specify just the first index of and the method will take everything from that character onwards
string input = "OneTwoThree";
string sub = input.Substring(3);
the output in this case will be: "TwoThree"
but it looks like you want to get the name of a class, and there is a much simpler way to do that :
typeof(MyProgram).Name
Hope it helps :)

Categories

Resources