I am trying to count the amount of 'x' characters that are in a string, and print out the number. I end up just counting the number of characters in the string instead. Here is what I have tried:  int count = 0;  for (int j = 0; j &lt; input1.length(); j++) {   char character = input1.charAt(j);   count++; }  if (indexX != -1)   {      System.out.println("x count: "+count);   } // indexX = input1.indexOf('x');
1424
I have the string   a.b.c.d   I want to count the occurrences of '.' in an idiomatic way, preferably a one-liner.  (Previously I had expressed this constraint as "without a loop", in case you're wondering why everyone's trying to answer without using a loop).
103460
I have a text file I'm outputting to a variable in my shell script.  I only need the first 50 characters however.  I've tried using cat ${filename} cut -c1-50 but I'm getting far more than the first 50 characters?  That may be due to cut looking for lines (not 100% sure), while this text file could be one long string-- it really depends.    Is there a utility out there I can pipe into to get the first X characters from a cat command?
4153
Is there some easy way to pad Strings in Java?  Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.
6195
Using PHP, what's the fastest way to convert a string like this: "123" to an integer?  Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array?
50739
I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.
107627
Is it the same convention as those used in known programming languages such as Java and C? I'm trying to create a lot of variables and I want it to start with a number, but I'm not sure if it's okay.
63537
Given an alphabet of size $n$, how many strings of length $c$ contain every single letter of the alphabet at least once?  I first attempted to use a recurrence relation to work it out:  $$ T(c) = \left\{ \begin{array}{cr} 0 &amp;\mbox{ if $c&lt;n$} \\ n! &amp;\mbox{ if $c = n$} \\ T(c-1) \cdot n \cdot c &amp;\mbox{ if $c &gt; n$} \end{array} \right. $$  As there's no strings that contain every letter if c &lt; n, and if c = n then it's just all permutations. When c > n you can take any string of size (c-1) that contains all letters (of which there are $T(c-1)$ to choose from), you choose which letter to add (of which there are $n$ choices) and there are $c$ different positions to put it. However, this gives out results that are larger than $n^c$ (the total number of strings), so it can't be right, and I realised it was because you could count some strings multiple times, as you can make them taking different inserting steps.   Then I thought about being simpler: you choose n positions in the string, put each letter of the alphabet in one of those positions, then let the rest of the string be anything:  $$ {c\choose{n}} \cdot n! \cdot n^{c-n} $$  But again this counts strings multiple times.  I've also considered using multinomial coefficients, but as we don't know how many times each letter appears in the string it seems unlikely they would be much help. I've also tried several other methods, some complicated and some simple, but none of them seem to work.  How would you go about working out a formula for this? I'm sure there's something simple that I'm missing.
4499
I'm trying to find a good way to print leading 0, such as 01001 for a . While the number would be stored as 1001, what is a good way to do it? I thought of using either case statements or if to figure out how many digits the number is and then convert it to an char array with extra 0's for printing, but I can't help but think there may be a way to do this with the printf format syntax that is eluding me.
24750
I've just encountered a weird problem, I'm trying to printf an integer variable, but I forgot to specify the variable name, i.e.  printf("%d");   instead of  printf("%d", integerName);   Surprisingly the program compiles, there is output and it is not random. In fact, it happens to be the very integer I wanted to print in the first place, which happens to be m-1.  The errorneous printf statement will consistently output m-1 for as long as the program keeps running... In other words, it's behaving exactly as if the statement reads  printf("%d", m-1);   Anybody knows the reason behind this behaviour? I'm using g++ without any command line options.  #include &lt;iostream&gt; #define maxN 100 #define ON 1 #define OFF 0  using namespace std;  void clearArray(int* array, int n); int fillArray(int* array, int m, int n);  int main() {     int n = -1, i, m;     int array[maxN];     int found;      scanf("%d", &amp;n);      while(n!=0)     {         found=0;         m = 1;         while(found!=1)         {             if(m != 2 &amp;&amp; m != 3 &amp;&amp; m != 4 &amp;&amp; m != 6 &amp;&amp; m != 12)             {                 clearArray(array, n);                 if(fillArray(array, m, n) == 0)                 {                     found = 1;                 }             }             m++;         }          printf("%d\n");          scanf("%d", &amp;n);     }      return 0; }  void clearArray(int* array, int n) {     for(int i = 1; i &lt;= n; i++)         array[i] = ON; }  int fillArray(int* array, int m, int n) {     int i = 1, j, offCounter = 0, incrementCounter;      while(offCounter != n)     {         if(*(array+i)==ON)          {             *(array+i) = OFF;             offCounter++;                }         else          {             j = 0;             while((*array+i+j)==OFF)             {                 j++;             }             *(array+i+j) = OFF;             offCounter++;                    }         if(*(array+13) == OFF &amp;&amp; offCounter != n) return 1;         if(offCounter ==n) break;          incrementCounter = 0;                while(incrementCounter != m)         {             i++;             if(i &gt; n) i = 1;             if(*(array+i) == ON) incrementCounter++;          }            }      return 0; }
39746
I have a java string, which has a variable length.   I need to put the piece "&lt;br&gt;" into the string, say each 10 characters.   For example this is my string:  `this is my string which I need to modify...I love stackoverlow:)`   How can I obtain this string?:  `this is my&lt;br&gt; string wh&lt;br&gt;ich I nee&lt;br&gt;d to modif&lt;br&gt;y...I love&lt;br&gt; stackover&lt;br&gt;flow:)`   Thanks
75336
Someone told me it's more efficient to use StringBuffer to concatenate strings in Java than to use the + operator for Strings. What happens under the hood when you do that? What does StringBuffer do differently?
1430
I want to check whether Value1 below contains "abc" within the first X characters. How would you check this with an if statement?  var Value1 = "ddabcgghh";  if (Value1.Contains("abc")) {     found = true; }   It could be within the first 3, 4 or 5 characters.
116230
We have a user provided string that may contain unicode characters, and we want the robot to type that string.  How do you convert a string into keyCodes that the robot will use? How do you do it so it is also java version independant (1.3 -> 1.6)?  What we have working for "ascii" chars is  //char c = nextChar(); //char c = 'a'; // this works, and so does 'A' char c = 'á'; // this doesn't, and neither does 'Ă' Robot robot = new Robot(); KeyStroke key = KeyStroke.getKeyStroke("pressed " + Character.toUpperCase(c) ); if( null != key ) {   // should only have to worry about case with standard characters   if (Character.isUpperCase(c))   {     robot.keyPress(KeyEvent.VK_SHIFT);   }    robot.keyPress(key.getKeyCode());   robot.keyRelease(key.getKeyCode());    if (Character.isUpperCase(c))   {     robot.keyRelease(KeyEvent.VK_SHIFT);   } }
76187
How can I test whether a command's output is an empty string while not   interfering with buffering?  Example:  command | ??? if [ $? = 0 ] &amp;&amp; [ -n "$output_from_command"]; then    ...   I want the output from the code above to be the same as when running command.
62033
I have a question about comparing a string with the empty string in Java. Is there a difference, if I compare a string with the empty string with == or equals? For example:  String s1 = "hi";  if (s1 == "")   or   if (s1.equals(""))    I know that one should compare strings (and objects in general) with equals, and not ==, but I am wondering whether it matters for the empty string.
19898
Is there an efficient method of converting an integer into the written numbers, for example:  string Written = IntegerToWritten(21);   would return "Twenty One".  Is there any way of doing this that doesn't involve a massive look-up table?
55508
I would like to find out the number of occurrences of each of the alphabets in a word. Eg  input  aabbbddd    output  a@2 b@3 c@0 d@3   How can I perform this using shell script?