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.
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:
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
This question already has answers here:
Check that email address is valid for System.Net.Mail.MailAddress
(6 answers)
Best Regular Expression for Email Validation in C#
(6 answers)
Closed 5 years ago.
Currently I'm using this code:
public static bool IsEmailAddress(this string value)
{
try
{
new System.Net.Mail.MailAddress(value);
return true;
}
catch ()
{
return false;
}
}
Is there a way to do this without using exceptions?
You could use a regular expression.
private const string EmailRegularExpression = #"^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*#(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
private static Regex EmailValidRegex = new Regex(CommonValues.EmailRegularExpression, RegexOptions.Compiled | RegexOptions.IgnoreCase)
public static bool IsEmailValid(string emailAddress)
{
return !string.IsNullOrWhiteSpace(emailAddress) && EmailValidRegex.IsMatch(emailAddress);
}
See also http://www.regular-expressions.info/email.html which is where I snagged the expression from.
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:
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(','));