How can I get the first character of a string? [duplicate] - c#

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]

Related

How do I replace only first instance of a string [duplicate]

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);

Get the rest of the string after occurrence of particular letters [duplicate]

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;

Count a word inside a long string in c# [duplicate]

This question already has answers here:
How would you count occurrences of a string (actually a char) within a string?
(34 answers)
Closed 8 years ago.
Can you please explain me how do i find in a simple way the number of times that a certain string occurs inside a big string?
Example:
string longString = "hello1 hello2 hello546 helloasdf";
The word "hello" is there four times. how can i get the number four.
Thanks
EDIT: I would like to know how i find a two word string as well for example:
string longString = "hello there hello2 hello4 helloas hello there";
I want to know the number of occurrences of "hello there".
EDIT 2: The regex method was the best for me (with the counts) but it does not find a word like this for example: ">word<" . Somehow, if I want to search for a word containing "<>" it skips it. Help?
just use string.Split() and count the results:
string wordToFind = "hello";
string longString = "hello1 hello2 hello546 helloasdf";
int occurences = longString
.Split(new []{wordToFind}, StringSplitOptions.None)
.Count() - 1;
//occurences = 4
to answer your edit, just change wordToFind to hello there
string longString = "hello1 hello2 hello546 helloasdf";
var regex = new Regex("hello");
var matches = regex.Matches(longString);
Console.WriteLine(matches.Count);
to do this with LINQ:
int count =
longString.Split(' ')
.Count(str => str
.Contains("hello", StringComparison.InvariantCultureIgnoreCase));
The only assumption here is that your longString is delimited by a space.
You can also use Linq to do this.
string longString = "hello1 hello2 hello546 helloasdf";
string target = "hello";
var count = longString.Split(' ').ToList<string>().Count(w => w.Contains(target));
Console.WriteLine(count);

Extract number from end of string [duplicate]

This question already has answers here:
Find and extract a number from a string
(32 answers)
Closed 8 years ago.
I have some strings like "pan1", "pan2", and "pan20" etc. I need to extract number. I use it:
char ch = s[(s.Length) - 1];
int n = Convert.ToInt32(Char.GetNumericValue(ch));
But in case of, for example, "pan20" the result is not correct 0.
Index Approach
if you know where is the starting index of the number then simply you can do this :
string str = "pan20";
int number = Convert.ToInt32(str.Substring(3));
Note that "3" is the starting index of the number.
Fixed Prefix Approach
try to remove "pan" from the string; like this
string str = "pan20";
int number = Convert.ToInt32(str.Replace("pan", ""));
Regular Expression Approach
use regular expression only when string contains undetermined text inside
string str = "pan20";
int number = Convert.ToInt32(System.Text.RegularExpressions.Regex.Match(str, #"\d+").Value;
You can use for example regular expressions, for example [0-9]+$ to get the numbers in the end. See the Regex class in MSDN.

How to get parts of a string [duplicate]

This question already has answers here:
How do I convert a string into an array?
(7 answers)
Closed 9 years ago.
I have this string:
string var = "x1,x2,x3,x4";
I want to get x1, x2, x3, x4 separately in four different strings.
This is a basic split
string yourString = "x1,x2,x3,x4";
string[] parts = yourString.Split(',');
foreach(string s in parts)
Console.WriteLine(s);
I renamed the original variable into yourString. While it seems that it is accepted in LinqPad as a valid identifier I think that it is very confusing using this name because is reserved
Split it up.
var tokens = someString.Split(',');
Now you can access them by their index:
var firstWord = tokens[0];
String.Split
var foo = "x1,x2,x3,x4";
var result = foo.Split(',');
There's also the "opposite: String.Join
Console.WriteLine(String.Join('*', result));
Will output: x1*x2*x3*x3
To separate values with comma between them, you can use String.Split() function.
string[] words = var.Split(',');
foreach (string word in words)
{
Console.WriteLine(word);
}

Categories

Resources