0% found this document useful (0 votes)
10 views5 pages

pract4

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

pract4

Uploaded by

Up ka sher king
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Java String toUpperCase()

The java string toUpperCase() method returns the string in uppercase le er. In other words, it
converts all characters of the string into upper case le er.

The toUpperCase() method works same as toUpperCase(Locale.getDefault()) method. It internally


uses the default locale.

Internal implementa on

1. public String toUpperCase(Locale locale) {

2. if (locale == null) {

3. throw new NullPointerExcep on();

4. }

5.

6. int firstLower;

7. final int len = value.length;

8.

9. /* Now check if there are any characters that need to be changed. */

10. scan: {

11. for (firstLower = 0 ; firstLower < len; ) {

12. int c = (int)value[firstLower];

13. int srcCount;

14. if ((c >= Character.MIN_HIGH_SURROGATE)

15. && (c <= Character.MAX_HIGH_SURROGATE)) {

16. c = codePointAt(firstLower);

17. srcCount = Character.charCount(c);

18. } else {

19. srcCount = 1;

20. }

21. int upperCaseChar = Character.toUpperCaseEx(c);

22. if ((upperCaseChar == Character.ERROR)

23. || (c != upperCaseChar)) {

24. break scan;

25. }
26. firstLower += srcCount;

27. }

28. return this;

29. }

30.

31. char[] result = new char[len]; /* may grow */

32. int resultOffset = 0; /* result may grow, so i+resultOffset

33. * is the write loca on in result */

34.

35. /* Just copy the first few upperCase characters. */

36. System.arraycopy(value, 0, result, 0, firstLower);

37.

38. String lang = locale.getLanguage();

39. boolean localeDependent =

40. (lang == "tr" || lang == "az" || lang == "lt");

41. char[] upperCharArray;

42. int upperChar;

43. int srcChar;

44. int srcCount;

45. for (int i = firstLower; i < len; i += srcCount) {

46. srcChar = (int)value[i];

47. if ((char)srcChar >= Character.MIN_HIGH_SURROGATE &&

48. (char)srcChar <= Character.MAX_HIGH_SURROGATE) {

49. srcChar = codePointAt(i);

50. srcCount = Character.charCount(srcChar);

51. } else {

52. srcCount = 1;

53. }

54. if (localeDependent) {

55. upperChar = Condi onalSpecialCasing.toUpperCaseEx(this, i, locale);

56. } else {
57. upperChar = Character.toUpperCaseEx(srcChar);

58. }

59. if ((upperChar == Character.ERROR)

60. || (upperChar >= Character.MIN_SUPPLEMENTARY_CODE_POINT)) {

61. if (upperChar == Character.ERROR) {

62. if (localeDependent) {

63. upperCharArray =

64. Condi onalSpecialCasing.toUpperCaseCharArray(this, i, locale);

65. } else {

66. upperCharArray = Character.toUpperCaseCharArray(srcChar);

67. }

68. } else if (srcCount == 2) {

69. resultOffset += Character.toChars(upperChar, result, i + resultOffset) - srcCount;

70. con nue;

71. } else {

72. upperCharArray = Character.toChars(upperChar);

73. }

74.

75. /* Grow result if needed */

76. int mapLen = upperCharArray.length;

77. if (mapLen > srcCount) {

78. char[] result2 = new char[result.length + mapLen - srcCount];

79. System.arraycopy(result, 0, result2, 0, i + resultOffset);

80. result = result2;

81. }

82. for (int x = 0; x < mapLen; ++x) {

83. result[i + resultOffset + x] = upperCharArray[x];

84. }

85. resultOffset += (mapLen - srcCount);

86. } else {

87. result[i + resultOffset] = (char)upperChar;


88. }

89. }

90. return new String(result, 0, len + resultOffset);

91. }

Signature

There are two variant of toUpperCase() method. The signature or syntax of string toUpperCase()
method is given below:

1. public String toUpperCase()

2. public String toUpperCase(Locale locale)

The second method variant of toUpperCase(), converts all the characters into uppercase using the
rules of given Locale.

Returns

string in uppercase le er.

Java String toUpperCase() method example

1. public class StringUpperExample{

2. public sta c void main(String args[]){

3. String s1="hello string";

4. String s1upper=s1.toUpperCase();

5. System.out.println(s1upper);

6. }}

Test it Now

Output:

HELLO STRING

Java String toUpperCase(Locale locale) Method Example 2

1. import java.u l.Locale;

2. public class StringUpperExample2 {

3. public sta c void main(String[] args) {

4. String s = "hello string";

5. String turkish = s.toUpperCase(Locale.forLanguageTag("tr"));


6. String english = s.toUpperCase(Locale.forLanguageTag("en"));

7. System.out.println(turkish);//will print I with dot on upper side

8. System.out.println(english);

9. }

10. }

Output:

HELLO STR?NG

HELLO STRING

You might also like