"src/Solution.cs(9,12): error CS1002: ; expected"
I cannot work out why I am being shown this error? I have put my code below. I am very inexperienced at c-sharp, I normally work in c++, so maybe a small syntax error somewhere?
public class Challenge
{
public static string MarkdownParser( string markdown )
{
string inputText = markdown;
string outputText;
unsigned int indexOfFirstSpace = 0;
unsigned int inputTextLength = 0;
//Trim Spaces Before & After String
inputText = inputText.Trim();
if (inputText.StartsWith("#")){
//Find Index oF First Space & Store
indexOfFirstSpace = inputText.IndexOf(" ");
if (indexOfFirstSpace > 6){
return inputText;
}
else{
//Find Length Of Input String
inputTextLength = inputText.Length;
//Store Hashes In Own String
string hashes = inputText.Substring(0, indexOfFirstSpace);
//Store header text in string
string headerText = inputText.Substring(indexOfFirstSpace,(inputTextLength - indexOfFirstSpace));
//Trim Spaces From Front
headerText = headerText.TrimStart();
//Build Output
outputText = "<h" + indexOfFirstSpace + ">" + headerText + "</h" + indexOfFirstSpace + ">";
return outputText;
}
}
else {
return inputText;
}
}
}
There are 2 problems here:
You tried to use 'unsigned int'. in C# the keyword for that is uinit.
The returned type for inputText.Length is actually an int, so the fixed code should be:
public class Challenge
{
public static string MarkdownParser(string markdown)
{
string inputText = markdown;
string outputText;
// ---- Changed these variables to Int ----
int indexOfFirstSpace = 0;
int inputTextLength = 0;
//Trim Spaces Before & After String
inputText = inputText.Trim();
if (inputText.StartsWith("#"))
{
//Find Index oF First Space & Store
indexOfFirstSpace = inputText.IndexOf(" ");
if (indexOfFirstSpace > 6)
{
return inputText;
}
else
{
//Find Length Of Input String
inputTextLength = inputText.Length;
//Store Hashes In Own String
string hashes = inputText.Substring(0, indexOfFirstSpace);
//Store header text in string
string headerText = inputText.Substring(indexOfFirstSpace, (inputTextLength - indexOfFirstSpace));
//Trim Spaces From Front
headerText = headerText.TrimStart();
//Build Output
outputText = "<h" + indexOfFirstSpace + ">" + headerText + "</h" + indexOfFirstSpace + ">";
return outputText;
}
}
else
{
return inputText;
}
}
}
I have this code which allows me to get the string between "Global." and " ".
private string getGlobalVariableName(string text)
{
int pFrom = text.IndexOf("Global.") + "Global.".Length;
int pTo = text.LastIndexOf(" ");
string name = text.Substring(pFrom, pTo - pFrom);
return name;
}
I want to modify it so that it gets the string between "Global." and any non-alphanumeric character. How could I do this?
Example:
this is true for what I have now
getGlobalVariableName(" foo Global.bar1 foobar") == "bar1"
this is what I want to be able to do
getGlobalVariableName(" foo Global.bar1>foobar") == "bar1"
You can use Regex...
string input = "Global.bar1>foobar";
var output = Regex.Match(input, #"Global.([\w]+)").Groups[1].Value;
help i want to get the specific string from my string x="Glass 1 1000"; i want to get the string "Glass" only and save it to my string type.
int[] quanvalue = new int[2];
int x1 = 0;
string type = "";
string x="Glass 1 1000";
string[] numbers = Regex.Split(x, #"\D+");
foreach (string value in numbers)
{
if (!string.IsNullOrEmpty(value))
{
int ib = int.Parse(value);
quanvalue[x1] = ib;
MessageBox.Show(quanvalue[0].ToString() + " " + quanvalue[1].ToString());
x1++;
}
else
{
// i want to get string from here
}
string sub = x.Substring(0, 5);
You can use a substring function to fetch the first 5 characters from x.
And save it in x itself
Is there a simple way for masking E-Mail addresses using Regular Expressions in C#?
My E-Mail:
myawesomeuser#there.com
My goal:
**awesome****#there.com (when 'awesome' was part of the pattern)
So it's more like an inverted replacement where evertyhing that does not actually match will be replaced with *.
Note: The domain should never be replaced!
From a performance side of view, would it make more sense to split by the # and only check the first part then put it back together afterwards?
Note: I don't want to check if the E-Mail is valid or not. It's just a simple inverted replacement and only for my current needs, the string is an E-Mail but for sure it can be any other string as well.
Solution
After reading the comments I ended up with an extension-method for strings which perfectly matches my needs.
public static string MaskEmail(this string eMail, string pattern)
{
var ix1 = eMail.IndexOf(pattern, StringComparison.Ordinal);
var ix2 = eMail.IndexOf('#');
// Corner case no-#
if (ix2 == -1)
{
ix2 = eMail.Length;
}
string result;
if (ix1 != -1 && ix1 < ix2)
{
result = new string('*', ix1) + pattern + new string('*', ix2 - ix1 - pattern.Length) + eMail.Substring(ix2);
}
else
{
// corner case no str found, all the pre-# is replaced
result = new string('*', ix2) + eMail.Substring(ix2);
}
return result;
}
which then can be called
string eMail = myawesomeuser#there.com;
string maskedMail = eMail.MaskEmail("awesome"); // **awesome****#there.com
string email = "myawesomeuser#there.com";
string str = "awesome";
string rx = "^((?!" + Regex.Escape(str) + "|#).)*|(?<!#.*)(?<=" + Regex.Escape(str) + ")((?!#).)*";
string email2 = Regex.Replace(email, rx, x => {
return new string('*', x.Length);
});
There are two sub-regular expressions here:
^((?!" + Regex.Escape(str) + "|#).)*
and
(?<!#.*)(?<=" + Regex.Escape(str) + ")((?!#).)*
They are in | (or)
The first one means: from the start of the string, any character but stop when you find str (escaped) or #
The second one means: there mustn't be a # before the start of this matching and, starting from str (escaped), replace any character stopping at the #
Probably faster/easier to read:
string email = "myawesomeuser#there.com";
string str = "awesome";
int ix1 = email.IndexOf(str);
int ix2 = email.IndexOf('#');
// Corner case no-#
if (ix2 == -1) {
ix2 = email.Length;
}
string email3;
if (ix1 != -1 && ix1 < ix2) {
email3 = new string('*', ix1) + str + new string('*', ix2 - ix1 - str.Length) + email.Substring(ix2);
} else {
// corner case no str found, all the pre-# is replaced
email3 = new string('*', ix2) + email.Substring(ix2);
}
This second version is better because it handle corner cases like: string not found and no domain in the email.
(awesome)|.(?=.*#)
Try this.Replace by *$1.But there will be an extra * at the start.So remove a * from the masked email from the start.See demo.
https://regex101.com/r/wU7sQ0/29
Non RE;
string name = "awesome";
int pat = email.IndexOf('#');
int pname = email.IndexOf(name);
if (pname < pat)
email = new String('*', pat - name.Length).Insert(pname, name) + email.Substring(pat);
How can the first letter in a text be set to capital?
Example:
it is a text. = It is a text.
public static string ToUpperFirstLetter(this string source)
{
if (string.IsNullOrEmpty(source))
return string.Empty;
// convert to char array of the string
char[] letters = source.ToCharArray();
// upper case the first char
letters[0] = char.ToUpper(letters[0]);
// return the array made of the new char array
return new string(letters);
}
It'll be something like this:
// precondition: before must not be an empty string
String after = before.Substring(0, 1).ToUpper() + before.Substring(1);
polygenelubricants' answer is fine for most cases, but you potentially need to think about cultural issues. Do you want this capitalized in a culture-invariant way, in the current culture, or a specific culture? It can make a big difference in Turkey, for example. So you may want to consider:
CultureInfo culture = ...;
text = char.ToUpper(text[0], culture) + text.Substring(1);
or if you prefer methods on String:
CultureInfo culture = ...;
text = text.Substring(0, 1).ToUpper(culture) + text.Substring(1);
where culture might be CultureInfo.InvariantCulture, or the current culture etc.
For more on this problem, see the Turkey Test.
If you are using C# then try this code:
Microsoft.VisualBasic.StrConv(sourceString, Microsoft.VisualBasic.vbProperCase)
I use this variant:
private string FirstLetterCapital(string str)
{
return Char.ToUpper(str[0]) + str.Remove(0, 1);
}
If you are sure that str variable is valid (never an empty-string or null), try:
str = Char.ToUpper(str[0]) + str[1..];
Unlike the other solutions that use Substring, this one does not do additional string allocations. This example basically concatenates char with ReadOnlySpan<char>.
I realize this is an old post, but I recently had this problem and solved it with the following method.
private string capSentences(string str)
{
string s = "";
if (str[str.Length - 1] == '.')
str = str.Remove(str.Length - 1, 1);
char[] delim = { '.' };
string[] tokens = str.Split(delim);
for (int i = 0; i < tokens.Length; i++)
{
tokens[i] = tokens[i].Trim();
tokens[i] = char.ToUpper(tokens[i][0]) + tokens[i].Substring(1);
s += tokens[i] + ". ";
}
return s;
}
In the sample below clicking on the button executes this simple code outBox.Text = capSentences(inBox.Text.Trim()); which pulls the text from the upper box and puts it in the lower box after the above method runs on it.
Take the first letter out of the word and then extract it to the other string.
strFirstLetter = strWord.Substring(0, 1).ToUpper();
strFullWord = strFirstLetter + strWord.Substring(1);
text = new String(
new [] { char.ToUpper(text.First()) }
.Concat(text.Skip(1))
.ToArray()
);
this functions makes capital the first letter of all words in a string
public static string FormatSentence(string source)
{
var words = source.Split(' ').Select(t => t.ToCharArray()).ToList();
words.ForEach(t =>
{
for (int i = 0; i < t.Length; i++)
{
t[i] = i.Equals(0) ? char.ToUpper(t[i]) : char.ToLower(t[i]);
}
});
return string.Join(" ", words.Select(t => new string(t)));;
}
string str = "it is a text";
// first use the .Trim() method to get rid of all the unnecessary space at the begining and the end for exemple (" This string ".Trim() is gonna output "This string").
str = str.Trim();
char theFirstLetter = str[0]; // this line is to take the first letter of the string at index 0.
theFirstLetter.ToUpper(); // .ToTupper() methode to uppercase the firstletter.
str = theFirstLetter + str.substring(1); // we add the first letter that we uppercased and add the rest of the string by using the str.substring(1) (str.substring(1) to skip the first letter at index 0 and only print the letters from the index 1 to the last index.)
Console.WriteLine(str); // now it should output "It is a text"
static String UppercaseWords(String BadName)
{
String FullName = "";
if (BadName != null)
{
String[] FullBadName = BadName.Split(' ');
foreach (string Name in FullBadName)
{
String SmallName = "";
if (Name.Length > 1)
{
SmallName = char.ToUpper(Name[0]) + Name.Substring(1).ToLower();
}
else
{
SmallName = Name.ToUpper();
}
FullName = FullName + " " + SmallName;
}
}
FullName = FullName.Trim();
FullName = FullName.TrimEnd();
FullName = FullName.TrimStart();
return FullName;
}
string Input = " it is my text";
Input = Input.TrimStart();
//Create a char array
char[] Letters = Input.ToCharArray();
//Make first letter a capital one
string First = char.ToUpper(Letters[0]).ToString();
//Concatenate
string Output = string.Concat(First,Input.Substring(1));
Try this code snippet:
char nm[] = "this is a test";
if(char.IsLower(nm[0])) nm[0] = char.ToUpper(nm[0]);
//print result: This is a test