This question already has answers here:
Find text in string with C#
(17 answers)
Closed 3 years ago.
How do I find out if one of my strings occurs in the other in C#?
For example, there are 2 strings:
string 1= "The red umbrella";
string 2= "red";
How would I find out if string 2 appears in string 1 or not?
you can use String Contains I guess. pretty sure there is similar question have been asked before How can I check if a string exists in another string
example :
if (stringValue.Contains(anotherStringValue))
{
// Do Something //
}
You can do it like this:
public static bool CheckString(string str1, string str2)
{
if (str1.Contains(str2))
{
return true;
}
return false;
}
Call the function:
CheckString("The red umbrella", "red") => true
Related
This question already has answers here:
Regex to match a word with + (plus) signs
(5 answers)
Search and replace method using regular expressions
(1 answer)
Closed 3 months ago.
Created an extension method and it works for normal words but When I added an # as part of search keyword it stopped working. Can someone help?
public static class Extensions
{
public static string ReplaceWholeWord(this string inputString, string oldValue, string newValue)
{
string pattern = #"\b#test\b"; // #"\b$\b".Replace("$",oldValue);
return Regex.Replace(inputString, pattern, newValue);
}
}
static void Main(string[] args)
{
string input = "#test, and #test but not testing. But yes to #test";
input = input.ReplaceWholeWord("#test", "text");
Console.WriteLine(input);
}
This question already has answers here:
When to use in vs ref vs out
(17 answers)
Closed 6 years ago.
I have a question: is it possible to edit the entry of the function without returning any value and those entries will be edited?
Idea:
void AddGreeting(string value)
{
value = "Hi " +value;
}
and calling this function like this:
string test = "John";
AddGreeting(test);
//and here the test will be "Hi John"
is that possible? and how to do it if it is?
You could easily use the ref parameter as so:
void AddGreeting(ref string value){}
and this would do what you want:
void AddGreeting(ref string value)
{
value = "Hi " +value;
}
string test = "John";
AddGreeting(ref test);
Alternatively, you could return a string, which i would consider neater and cleaner to look at
This question already has answers here:
Best way to reverse a string
(51 answers)
Closed 6 years ago.
In C# want to do something like this:
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("123");
And then some code
//Outputs 321 (works for any text value)
In C#
public string Reverse(string s) {
return new string(Array.Reverse(s.ToCharArray()));
}
From above 3.5 Framework:
public string Reverse(string s) {
return new string(s.Reverse().ToArray());
}
See reference.
This question already has answers here:
Convert a string to an enum in C#
(29 answers)
Match string to enumeration?
(11 answers)
Closed 7 years ago.
I have my enum defined like this.
public enum Places : long
{
World = (long)1,
India = (long)23424848,
USA = (long)23424977
}
Now I get a string of value like 'India'. I want the corresponding value of the enumerator.
for instance if i get the string 'World'(or world - case insensitive ), I need the value 1 to be returned.
I tried this way:
long woeid = ((long)(typeof(Places)country));
this doesnot work.
Is there a simple way to do?
The method you want is Enum.Parse.
You would use it like this:
string country = "India";
Places myplace = (Places)Enum.Parse(typeof(Places), country);
long placeID = (long)Enum.Parse(typeof(Places), country);
This question already has answers here:
Split comma-separated values
(6 answers)
Closed 8 years ago.
I have a string which is in following format or comma separated format
s1 ,s2,s3,s4,and so on. I want to convert it into
s1
s2
s3
s4
..
..
..
I know how I can do this by using a loop but I want to do this by regex in c# and in java without using the loop can I achive this ???
Here is how you can do it in Java.
public class MainProg {
public static void main(String[] args) {
String s = "s1,s2,s3,s4";
String z = s.replaceAll(",", "\n");
System.out.println(z);
}
}
find : \s*,\s*
replace with : \n
String.Join(Environment.NewLine, myString.Split(','));