# Java Strings & Substrings

**URL:** <https://forum.codewithmosh.com/t/java-strings-substrings/15036>\
**Category:** Uncategorized\
**Created:** [September 9, 2022, 2:52pm UTC](https://forum.codewithmosh.com/t/java-strings-substrings/15036 "2022-09-09T14:52:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 9, 2022, 2:52pm UTC](https://forum.codewithmosh.com/t/java-strings-substrings/15036/1 "2022-09-09T14:52:06Z")

</div>

Can anyone pls help me to understand this code, for making the first letter of a word to uppercase.

code:  
Scanner sc=new Scanner(System.in);  
String A=sc.next();  
String B=sc.next();  
System.out.print(A.substring(0,1).toUpperCase()+ A.substring(1) + " "+B.substring(0,1).toUpperCase() + B.substring(1));

why they are using substring here?  
A[0].toUpperCase() why this wont work?

---

<div class="post-metadata">

**Author:** ![UniqueNospaceShort](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/uniquenospaceshort/32/1814_2.png) [@UniqueNospaceShort](https://forum.codewithmosh.com/u/UniqueNospaceShort)\
**Post date:** [September 9, 2022, 3:48pm UTC](https://forum.codewithmosh.com/t/java-strings-substrings/15036/2 "2022-09-09T15:48:47Z")

</div>

Hi.

Basically you ask the user for 2 strings.  
Then when you display those, for each of them  
you take the first character (with `substring(0,1)`) and display it uppercase and then you display the remaining characters skipping the 1st one (with `substring(1)`).

Substring returns a portion of your string.  
If there is 1 parameter it is the starting index (until the end of that string).  
If there is two the second one is the length of the string.

I did not try that last part yet with Java recently, but to me a string is a table of characters so it should work. Alternatively `charAt` could have been used to retrieve the 1st character. The question though is do we have something that would display it uppercase.

Hope it helped you understand.

Regards.

---

<div class="post-metadata">

**Author:** ![jmrunkle](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/jmrunkle/32/3765_2.png) [@jmrunkle](https://forum.codewithmosh.com/u/jmrunkle)\
**Post date:** [September 10, 2022, 3:51am UTC](https://forum.codewithmosh.com/t/java-strings-substrings/15036/3 "2022-09-10T03:51:34Z")

</div>

> [@dppraveen](#):
>
> why they are using substring here?  
> A[0].toUpperCase() why this wont work?

Because that syntax is invalid (Java does not allow you to index a String like that). There is a `charAt` method you can use to get the character at a given index, but then you would have to append the `char` returned back into a String. You could also try putting both Strings into a StringBuilder and then you would have access to methods like `setCharAt` which could replace a single character. You could combine that with the `charAt` method and `Character.toUpperCase` to change the casing of those particular characters. That would look something like this:

```java
var result = new StringBuilder()
  .append(A)
  .append(" ")
  .append(B);
result.setCharAt(0, Character.toUpperCase(A.charAt(0)));
result.setCharAt(A.length() + 1, Character.toUpperCase(B.charAt(0)));
System.out.print(result);

```

On the whole, both versions work just fine and take a similar amount of space.

* * *

> [@UniqueNospaceShort](#):
>
> Basically you ask the user for 2 strings.

Technically, the code never “asks” for anything. It just expects the user to type in two strings (separated by any whitespace). The purpose of the code is to print the two words with the first letter of each word capitalized (and separating the two words by a single space).

---

<div class="post-metadata">

**Author:** ![dppraveen](https://avatars.discourse-cdn.com/v4/letter/d/50afbb/32.png) [@dppraveen](https://forum.codewithmosh.com/u/dppraveen)\
**Post date:** [September 12, 2022, 8:50am UTC](https://forum.codewithmosh.com/t/java-strings-substrings/15036/4 "2022-09-12T08:50:16Z")

</div>

okay, thank you jason
