I am writing a code to if characters from 1 string are present in another string. I used string.Contains but its not solving my case
string str = "abc";
string str2 = "ac";
if(str.Contains(str2))
{
Console.WriteLine("true");
}
I want result to be true. but its not returning true
How about
string str = "abc";
string str2 = "ac";
bool containsAll = !str2.Except(str).Any();
because
I am writing a code to if characters from 1 string are present in another string.
I guess you want to check: " if All characters from 1 string are present in another string."
string str = "abc";
string str2 = "ac";
Console.WriteLine(str2.All(x => str.Contains(x)));
Related
Using ASP.NET C#, I need to find and replace the word string1 between the last two slashes and replace with string2
Example:
string fullStr = "/this/is/string1/part";
string subStr = "function";
string finalStr = "/this/is/" + subStr + "/part";
And a regex solution:
string fullStr = "this/is/string1/part";
string subStr = "function";
var newstr = Regex.Replace(fullStr, #"/[^/]+/(?=[^/]+$)", m => "/" + subStr + "/");
I don't feel a need of regex here.
string fullStr = "/this/is/string1/part";
string subStr = "function";
string[] fullStrParts = fullStr.Split('/');
fullStrParts[fullStrParts.Length - 2] = subStr;
string finalStr = string.Join("/", fullStrParts);
I have a string and i want to detect the value next to the string .
for eg:
string s="the #awb 58981140743 is the number";
The value next to #awb needs to be detected.I want the result string to be (after #awb) .
string output=58981140743;
Use below regex to solve your problem.
string s = "the #awb 58981140743 is the number";
string result = Regex.Replace(s, #"[^\d]", "");
If you wan't specific string after #awb then use below code
string s = "the #awb 58981140743 is the number";
Regex.Match(s, #"#awb\s*(\d+)").Groups[1].Value;
string s = "the #awb 58981140743 is the number";
if (s.IndexOf("#awb") >= 0)
{
var temp = s.Split(new string[] { "#awb" }, StringSplitOptions.None)[1];
}
this code snippet will give you the result.
string s="the #awb 58981140743 is the number";
var splittedValue= s.Split(new string[] { "#awb" }, StringSplitOptions.None)[1];
This is how simply you can split and show your value
am new in c# so how to replace the string
for example:-
label1.Content = "Bal.Rs." + 100;
How to get the 100 only while we save from label1.Text???
Here you go:
string OnlyNumbered = Regex.Match(label1.Content.ToString(), #"\d+").Value;
try doing this
string input="abc 123"
string result = Regex.Replace(input, #"[^\d]", "");
//output result=123
^\d specify not number
Hope this will help
I have a text called
string path = "Default/abc/cde/css/";
I want to compare a text.
string compare = "abc";
I want a result
string result = "Default/abc";
The rest of the path /cde/css is useless.Is it possible to grab the desire result in asp.net c#. Thanks.
Is this what you looking for?:
string result = path.Substring(0, path.IndexOf(compare)+compare.Length);
Try this. This will loop through the different levels (assuming these are directory levels) until it matches the compare, and then exit the loop. This means that if there is a folder called abcd, this won't end the loop.
string path = "Default/abc/cde/css";
string compare = "abc";
string result = string.Empty;
foreach (string lvl in path.Split("/")) {
result += lvl + "/";
if (lvl == compare)
{
break;
}
}
if (result.Length>0)
{
result = result.substring(0, result.length-1);
}
string path = "Default/abc/cde/css/";
string answer = "";
string compare = "abc";
if (path.Contains(compare ))
{
answer = path.Substring(0, path.IndexOf(stringToMatch) + compare.Length);
}
Something like the above should work.
I suggest that if you meet questions of this kind in the future, you should try it yourself first.
string result = path.Contains(compare) ? path.Substring(0, (path.IndexOf(compare) + compare.Length)) : path;
The following bit of C# code does not seem to do anything:
String str = "{3}";
str.Replace("{", String.Empty);
str.Replace("}", String.Empty);
Console.WriteLine(str);
This ends up spitting out: {3}. I have no idea why this is. I do this sort of thing in Java all the time. Is there some nuance of .NET string handling that eludes me?
The String class is immutable; str.Replace will not alter str, it will return a new string with the result. Try this one instead:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
String is immutable; you can't change an instance of a string. Your two Replace() calls do nothing to the original string; they return a modified string. You want this instead:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
It works this way in Java as well.
Replace actually does not modify the string instance on which you call it. It just returns a modified copy instead.
Try this one:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
Str.Replace returns a new string. So, you need to use it as follows:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
The Replace function returns the modified string, so you have to assign it back to your str variable.
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
You'll have to do:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
Look at the String.Replace reference:
Return Value Type: System.String
A String equivalent to this instance but
with all instances of oldValue
replaced with newValue.
I believe that str.Replace returns a value which you must assign to your variable. So you will need to do something like:
String str = "{3}";
str = str.Replace("{", String.Empty);
str = str.Replace("}", String.Empty);
Console.WriteLine(str);
The Replace method returns a string with the replacement. What I think you're looking for is this:
str = str.Replace("{", string.Empty);
str = str.Replace("}", string.Empty);
Console.WriteLine(str);
Besides all of the suggestions so far - you could also accomplish this without changing the value of the original string by using the replace functions inline in the output...
String str = "{3}";
Console.WriteLine(str.Replace("{", String.Empty).Replace("}", String.Empty));