0

I am trying to find the capitalized portion of a string, to then insert two characters that represent the Double Capital sign in the Braille language. My intention for doing this is to design a translator that can translate from regular text to Braille. I'll give an example belo. English String: My variable is of type IEnumerable. Braille: ,My variable is of type ,,IE-numberable. I also want the dash in IE-numerable to only break words that have upper and lower case, but not in front of punctuation marks, white spaces, numbers or other symbols.

Thanks a lot in advance for your answers.

  • 10
    What have you tried? What are you having trouble with? Are you asking how to loop through the characters in a string? How to check whether a character is uppercase? How to aggregate the results? – SLaks Jul 30 '13 at 21:12
  • I am trying to find the capital letters of a string like this one: – user2227354 Aug 02 '13 at 17:42
  • Basically, I am trying to find the capitalized portion of, say myVARIABLE or IEnumerable. I then want to take that portion of the string and insert ",,", which is the double-cap signs in the Braille language, so the string would look like this: ,,Ie-numerable. – user2227354 Aug 02 '13 at 17:45

6 Answers6

4

I had never heard of a "Double Capital" sign, so I read up on it here. From what I can tell, this should suit your needs.

You can use this to find any sequence of two or more uppercase (majuscule) Latin letters or hyphens in your string:

var matches = Regex.Matches(input, "[A-Z-]{2,}");

You can use this to insert the double-capital sign:

var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0");

For example:

var input = "this is a TEST";
var result = Regex.Replace(input, "[A-Z-]{2,}", ",,$0"); // this is a ,,TEST

You can use this to hand single and double capitals:

var input = "McGRAW-HILL";
var result = Regex.Replace(input, "[A-Z-]([A-Z-]+)?", 
        m => (m.Groups[1].Success ? ",," : ",") + m.Value); // ,Mc,,GRAW-HILL
p.s.w.g
  • 146,324
  • 30
  • 291
  • 331
  • Thank you very much for taking time to write up this answer. I greatly appreciate it. I really do. – user2227354 Aug 02 '13 at 17:51
  • @user2227354 Glad to help. Here's a tip for asking questions on SO: always show what you've tried and explain why it didn't work (error, bad result, etc.). For questions asking for regular expression patterns, it's **critical** to show what input strings you want to match and what strings you don't want to match, and in the case regular expression replacements, please show at least a few input strings with their desired outputs. That will help you avoid down votes and close votes in the future. – p.s.w.g Aug 02 '13 at 18:23
0

You can find them with a simple regex:

using System.Text.RegularExpressions;

// ..snip..

Regex r = new Regex("[A-Z]"); // This will capture only upper case characters
Match m = r.Match(input, 0);

The variable m of type System.Text.RegularExpressions.Match will contain a collection of captures. If only the first match matters, you can check its Index property directly.

Now you can insert the characters you want in that position, using String.Insert:

input = input.Insert(m.Index, doubleCapitalSign);
Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
0

Not sure if this is what you are going for?

var inputString = string.Empty; //Your input string here
var output = new StringBuilder();

foreach (var c in inputString.ToCharArray())
{
    if (char.IsUpper(c))
    {
        output.AppendFormat("_{0}_", c);
    }
    else
    {
        output.Append(c);
    }
}

This will loop through each character in the inputString if the characater is upper it inserts a _ before and after (replace that with your desired braille characters) otherwise appends the character to the output.

Jay
  • 6,224
  • 4
  • 20
  • 23
0

this code can solve your problema

        string x = "abcdEFghijkl";
        string capitalized = string.Empty;
        for (int i = 0; i < x.Length; i++)
        {
            if (x[i].ToString() == x[i].ToString().ToUpper())
                capitalized += x[i];
        }
rodolfoprado
  • 126
  • 7
0

Have you tried using the method Char.IsUpper method http://msdn.microsoft.com/en-us/library/9s91f3by.aspx

This is another similar question that uses that method to solve a similar problem Get the Index of Upper Case letter from a String

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0

If you just want to find the first index of an uppercase letter:

var firstUpperCharIndex = text  // <-- a string
    .Select((chr, index) => new { chr, index })
    .FirstOrDefault(x => Char.IsUpper(x.chr));
if (firstUpperCharIndex != null)
{ 
    text = text.Insert(firstUpperCharIndex.index, ",,");
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939