Find text in string with C#
Use this method:
public static string getBetween(string strSource, string strStart, string strEnd)
{
if (strSource.Contains(strStart) && strSource.Contains(strEnd))
{
int Start, End;
Start = strSource.IndexOf(strStart, 0) + strStart.Length;
End = strSource.IndexOf(strEnd, Start);
return strSource.Substring(Start, End - Start);
}
return "";
}
How to use it:
string source = "This is an example string and my data is here";
string data = getBetween(source, "my", "is");
C# find words on a String
You need to chain your IndexOf
calls, like so:
var i = -1
while (true)
{
i = strFinal.IndexOf(textoAEncontrar, i+1);
if (i == -1) break;
Console.WriteLine("Found string at {0}", i);
}
You may need to improve the boundary checks above but this is the general idea.
How to get specific text from string in C#
I think best thing to do this is with Reguler Expressions, use code below:
Note: I do this like what is example was: Character casing, first and end
public static string GetProfileName(string input)
{
List<char> result = new List<char>();
bool startFinding = false;
foreach (char c in input.ToCharArray())
{
// Is match A to Z in upper case?
if (Regex.IsMatch(c.ToString(), "[A-Z]"))
{
result.Add(c);
startFinding = true;
}
else if(startFinding)
{
// End looping
break;
}
}
return new string(result.ToArray());
}
Explain this method
Variables:
At first, I define List<char>
as variable, Becuase it will be the result.
And startFinding
defines did method start finding characters from A-Z or not.
Method:
I made loop in every character (foreach
)
If character was from A-Z
in uppercase (using Regex
):
Add this character to
result
Change
startFinding
totrue
Else if startFinding
was true
End this loop (
break
)
Results:
ST
S
WT
UBT
How to search a string in String array
Every method, mentioned earlier does looping either internally or externally, so it is not really important how to implement it. Here another example of finding all references of target string
string [] arr = {"One","Two","Three"};
var target = "One";
var results = Array.FindAll(arr, s => s.Equals(target));
Using C# to check if string contains a string in string array
Here is how you can do it:
string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
if (stringToCheck.Contains(x))
{
// Process...
}
}
Maybe you are looking for a better solution... Refer to Anton Gogolev's answer which makes use of LINQ.
Related Topics
Smoothly Connecting Circle Centers
Prevent Using Dispatcher.Invoke in Wpf Code
Mail Sending with Network Credential as True in Windows Form Not Working
Partial Generic Type Inference Possible in C#
Oledbparameters and Parameter Names
How to Set Properties on Struct Instances Using Reflection
Deserializing JSON with Unknown Object Names
Multiple Parallel.Foreach Loops in .Net
Unity How to Make a Visual Joystick in Unity
ASP.NET Core 2 API Post Objects Are Null
Find a Control in Windows Forms by Name
Deserialize a JSON Array in C#
What Does "Where T:Class, New()" Mean
How to Generate .Net 4.0 Classes from Xsd
Exact Time Measurement for Performance Testing
How to Pass an Object from Form1 to Form2 and Back to Form1
Escape Special Character in Regex
Executing Ssis 2012 Package That Has Script Components from External Application