This question already has answers here:
check does string contain more than one white space [duplicate]
(2 answers)
Closed 7 years ago.
I want to check how many successing spaces are in my string. It should be no more than one.
For example:
this is ok
this is NOT ok
thisisok
this is NOT ok
You can test to see if two consecutive spaces exist in the string, as that will also cover any string of spaces longer than 2. You can do this using the Contains method:
string testString = "this is not OK";
if (testString.Contains(" "))
{
// Bad
}
This should get you the logic for succession spaces in string literal
static void Main(string[] args)
{
string str = "this is NOT ok";
int index = str.IndexOf(' ');
//check if next character of space is also a space
if (index >= 0 && str[index + 1] == ' ')
Console.WriteLine("Not Ok String");
else
Console.WriteLine("OK String");
}
Related
This question already has answers here:
How do I replace the *first instance* of a string in .NET?
(15 answers)
Closed 3 years ago.
I need to replace the first instance of a word in a string with another string. The problem is that the word being replaced sometimes appears more than once in the string that is being changed. When that happens we only want the first instance to be replaced. How can I do that?
string s1 = "something replace replace replace replace something";
string s2 = "replace";
string newString=s1;
int index = s1.IndexOf(s2);
if (index > -1)
{
newString = s1.Substring(0, index) + "newWord" + s1.Substring(index + s2.Length);
}
Console.WriteLine(newString);
This question already has answers here:
Regular Expression Groups in C#
(5 answers)
Closed 4 years ago.
string ABC = "This is test AZ12346";
Need value that occurs after AZ. AZ will always be present in the string and it will always be the last word. But number of characters after AZ will vary.
Output should be : AZ123456
Simply :
ABC.Substring(ABC.LastIndexOf("AZ"));
You can try LastIndexOf and Substring:
string ABC = "This is test AZ12346";
string delimiter = "AZ";
// "AZ123456"
string result = ABC.Substring(ABC.LastIndexOf(delimiter));
In case delimiter can be abscent
int p = ABC.LastIndexOf(delimiter);
string result = p >= 0
? ABC.Substring(p)
: result; // No delimiter found
If you are looking for whole word which starts from AZ (e.g. "AZ123", but not "123DCAZDE456" - AZ in the middle of the word) you can try regular expressions
var result = Regex
.Match(ABC, #"\bAZ[A-Za-z0-9]+\b", RegexOptions.RightToLeft)
.Value;
This question already has answers here:
C#: how to get first char of a string?
(15 answers)
Closed 4 years ago.
for example, i have some string
var a = '2,,2,';
var b = ',,,'
how to get only first character,
so i will get
a = '2'
b = ','
Use the index of the character you want, so:
a[0]
For a complete answer:
char getFirstChar(string inString, char defaultChar)
{
if (string.IsNullOrEmpty(inString)) return defaultChar;
return inString[0];
}
string s = "abcdefg";
string sB = s[0];
sB = "a"
string a= "abc";
string subString = a.Substring(0,1);
Characters in Strings can be accessed in the same way as arrays.
So for example, to get the first character in a string variable name you use the expression name[0]
This question already has answers here:
C# string replace does not actually replace the value in the string [duplicate]
(3 answers)
Closed 6 years ago.
I have this method that takes a string and only gets the numbers from it and trims the white spaces but it is not working:
public static string ToNumericOnlyFAX(this string input)
{
if (input == "OFF6239750")
{
input = Regex.Replace(input, "[^0-9']", " "); - becomes " 6239750"
input.TrimStart().Trim().TrimEnd(); - after any of these its still the above, I want it to be "6239750"
if (input.Length <= 10 && input == "6239750") - if the above works then I can padleft
{
input.PadLeft(10, '0');
}
}
return input;
}
how to I trim the string if the white spaces are in between the numbers such ass 603 123 4321???
Additionally, why do you need to replace with spaces?
For instance, you could change it to:
input = Regex.Replace(input, "[^0-9']", ""); - becomes "6239750".
Please note: This regular expression will remove all characters, including spaces.
Try to use:
string subjectString = "OFF6239750";
string resultString = null;
try {
resultString = Regex.Match(subjectString, #"\d+").Value;
} catch (ArgumentException ex) {
// Syntax error in the regular expression
}
This question already has answers here:
Regex split string but keep separators
(2 answers)
Closed 7 years ago.
Let's say I have the following string: "This is a test. Haha.". I want to split it so that it becomes these there lines:
Hey.
This is a test.
Haha.
(Note that the space after the dot is preserved).
I tried to split the string using the Split method, and split by the dot, but it returns 3 new strings with the space before the string, and it removes the dots. I want to keep the space after the dot and keep the space.
How can I achieve this?
EDIT: I found a workaround, but I'm sure there's a simpler way:
string a = "Hey. This is a test. Haha.";
string[] splitted = a.Split('.');
foreach(string b in splitted)
{
if (b.Length < 3)
{
continue;
}
string f = b.Remove(0, 1);
Console.WriteLine(f + ". ");
}
I can't test this but due to the post of Darin Dimitrov :
string input = "Hey. This is a test. Haha.";
string result = input.Replace(". ", ".\n");