+ 2
I want to remove everything after / in domain for each line it's erasing everything after first occurance.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; using System.IO; namespace SoloLearn { class Program { string str = "something@google.com/123 something@google.com/xyz something@google.com/abc"; if (str.Contains('/')) { int index = str.IndexOf('/'); str = str.Substring(0, index); } Console.WriteLine(str); } }
4 Réponses
+ 1
Use a loop :
First just print
str.Substring(0,index) ;
form a new string from value of next space like
str = str.Substring(str.IndexOf(' ', index) ;
Now repeat the same steps until end of string..
May be code already given above by @jacob
Hope it helps..
+ 1
I was doing this for single domain so I fixed in my existing code where it takes the domain instead of doing I after getting the results. So it is only happening in the beginning once which I had to remove instead haha.. thanks everyone!
+ 1
Jojan
If you feel like learning Regex (not too complicated).
You can parse strings very efficiently to match the desired patterns.
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex?view=net-6.0
Use this to generate some Regex. https://regex-generator.olafneumann.org/?sampleText=Something%40test.com%2Fxyz&flags=i&onlyPatterns=true&matchWholeLine=false&selection=9%7CCharacter,14%7CCharacter,18%7CCharacter
Your pattern would be something like @.*\..*/
Sample code from link:
using System;
using System.Text.RegularExpressions;
public class Sample
{
public static bool useRegex(String input)
{
const Regex regex = new Regex("@.*\\..*/", RegexOptions.IgnoreCase);
return regex.IsMatch(input);
}
}
Then all you need is to split the string into substrings. C# has a split method.
https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.split?view=net-6.0
0
Do you mean you need output 123 xyz abc?